Skip to content
Newer
Older
100644 65 lines (55 sloc) 1.83 KB
6f1bf52 @rafaelfranca Extract input_checked? to a module
rafaelfranca authored Jan 17, 2012
1 require 'action_view/helpers/tags/checkable'
2
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
3 module ActionView
4 module Helpers
5 module Tags
6 class CheckBox < Base #:nodoc:
6f1bf52 @rafaelfranca Extract input_checked? to a module
rafaelfranca authored Jan 17, 2012
7 include Checkable
8
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
9 def initialize(object_name, method_name, template_object, checked_value, unchecked_value, options)
10 @checked_value = checked_value
11 @unchecked_value = unchecked_value
12 super(object_name, method_name, template_object, options)
13 end
14
15 def render
16 options = @options.stringify_keys
17 options["type"] = "checkbox"
18 options["value"] = @checked_value
675fb8d @rafaelfranca Remove code duplication between CheckBox ans RadioButton
rafaelfranca authored Jan 16, 2012
19 options["checked"] = "checked" if input_checked?(object, options)
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
20
21 if options["multiple"]
22 add_default_name_and_id_for_value(@checked_value, options)
23 options.delete("multiple")
24 else
25 add_default_name_and_id(options)
26 end
675fb8d @rafaelfranca Remove code duplication between CheckBox ans RadioButton
rafaelfranca authored Jan 17, 2012
27
9fbb176 @lest add include_hidden option to checkbox tag
lest authored Mar 13, 2012
28 include_hidden = options.delete("include_hidden") { true }
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
29 checkbox = tag("input", options)
9fbb176 @lest add include_hidden option to checkbox tag
lest authored Mar 13, 2012
30
31 if include_hidden
32 hidden = hidden_field_for_checkbox(options)
33 hidden + checkbox
34 else
35 checkbox
36 end
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
37 end
38
39 private
40
675fb8d @rafaelfranca Remove code duplication between CheckBox ans RadioButton
rafaelfranca authored Jan 17, 2012
41 def checked?(value)
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
42 case value
43 when TrueClass, FalseClass
a872ac9 @rafaelfranca Change check_box to work inverting the checked and unchecked value
rafaelfranca authored Apr 29, 2012
44 value == !!@checked_value
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
45 when NilClass
46 false
47 when String
48 value == @checked_value
49 else
3ee6bcf @nashby check_box value can be not only an object of Array class
nashby authored Oct 20, 2012
50 if value.respond_to?(:include?)
51 value.include?(@checked_value)
52 else
53 value.to_i == @checked_value.to_i
54 end
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
55 end
56 end
3d10955 @carlosantoniodasilva Replicate :form html5 attribute to hidden field for check_box
carlosantoniodasilva authored Feb 2, 2012
57
58 def hidden_field_for_checkbox(options)
59 @unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)) : "".html_safe
60 end
f42e1db @rafaelfranca Extract CheckBox
rafaelfranca authored Jan 16, 2012
61 end
62 end
63 end
64 end
Something went wrong with that request. Please try again.