aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-01-16 20:48:32 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-01-17 00:05:29 -0300
commitf42e1db35ab3a0300b8e02ac55e0ae82873b5986 (patch)
tree98ac46c277e1091f6efbd01a323dd511315c8304 /actionpack/lib/action_view
parent8e8c6c35832e6a9d9ee09b645e9f34b3594128c5 (diff)
downloadrails-f42e1db35ab3a0300b8e02ac55e0ae82873b5986.tar.gz
rails-f42e1db35ab3a0300b8e02ac55e0ae82873b5986.tar.bz2
rails-f42e1db35ab3a0300b8e02ac55e0ae82873b5986.zip
Extract CheckBox
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb41
-rw-r--r--actionpack/lib/action_view/helpers/tags.rb1
-rw-r--r--actionpack/lib/action_view/helpers/tags/check_box.rb60
3 files changed, 62 insertions, 40 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 837b1a8d9a..c804c2646c 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -828,7 +828,7 @@ module ActionView
# # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
#
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
- InstanceTag.new(object_name, method, self, options.delete(:object)).to_check_box_tag(options, checked_value, unchecked_value)
+ ActionView::Helpers::Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
end
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
@@ -1009,28 +1009,6 @@ module ActionView
tag("input", options)
end
- def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
- options = options.stringify_keys
- options["type"] = "checkbox"
- options["value"] = checked_value
- if options.has_key?("checked")
- cv = options.delete "checked"
- checked = cv == true || cv == "checked"
- else
- checked = self.class.check_box_checked?(value(object), checked_value)
- end
- options["checked"] = "checked" if checked
- if options["multiple"]
- add_default_name_and_id_for_value(checked_value, options)
- options.delete("multiple")
- else
- add_default_name_and_id(options)
- end
- hidden = unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value, "disabled" => options["disabled"]) : ""
- checkbox = tag("input", options)
- hidden + checkbox
- end
-
def to_boolean_select_tag(options = {})
options = options.stringify_keys
add_default_name_and_id(options)
@@ -1089,23 +1067,6 @@ module ActionView
end
end
- def check_box_checked?(value, checked_value)
- case value
- when TrueClass, FalseClass
- value
- when NilClass
- false
- when Integer
- value != 0
- when String
- value == checked_value
- when Array
- value.include?(checked_value)
- else
- value.to_i != 0
- end
- end
-
def radio_button_checked?(value, checked_value)
value.to_s == checked_value.to_s
end
diff --git a/actionpack/lib/action_view/helpers/tags.rb b/actionpack/lib/action_view/helpers/tags.rb
index 76e1927b80..175bc5eb64 100644
--- a/actionpack/lib/action_view/helpers/tags.rb
+++ b/actionpack/lib/action_view/helpers/tags.rb
@@ -8,6 +8,7 @@ module ActionView
autoload :HiddenField, 'action_view/helpers/tags/hidden_field'
autoload :FileField, 'action_view/helpers/tags/file_field'
autoload :TextArea, 'action_view/helpers/tags/text_area'
+ autoload :CheckBox, 'action_view/helpers/tags/check_box'
end
end
end
diff --git a/actionpack/lib/action_view/helpers/tags/check_box.rb b/actionpack/lib/action_view/helpers/tags/check_box.rb
new file mode 100644
index 0000000000..89aaad632c
--- /dev/null
+++ b/actionpack/lib/action_view/helpers/tags/check_box.rb
@@ -0,0 +1,60 @@
+module ActionView
+ module Helpers
+ module Tags
+ class CheckBox < Base #:nodoc:
+ def initialize(object_name, method_name, template_object, checked_value, unchecked_value, options)
+ @checked_value = checked_value
+ @unchecked_value = unchecked_value
+ super(object_name, method_name, template_object, options)
+ end
+
+ def render
+ options = @options.stringify_keys
+ options["type"] = "checkbox"
+ options["value"] = @checked_value
+
+ if options.has_key?("checked")
+ cv = options.delete "checked"
+ checked = cv == true || cv == "checked"
+ else
+ checked = check_box_checked?(value(object))
+ end
+
+ options["checked"] = "checked" if checked
+ if options["multiple"]
+ add_default_name_and_id_for_value(@checked_value, options)
+ options.delete("multiple")
+ else
+ add_default_name_and_id(options)
+ end
+ hidden = @unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => @unchecked_value, "disabled" => options["disabled"]) : ""
+ checkbox = tag("input", options)
+ hidden + checkbox
+ end
+
+ private
+
+ def value(object)
+ object.send @method_name if object
+ end
+
+ def check_box_checked?(value)
+ case value
+ when TrueClass, FalseClass
+ value
+ when NilClass
+ false
+ when Integer
+ value != 0
+ when String
+ value == @checked_value
+ when Array
+ value.include?(@checked_value)
+ else
+ value.to_i != 0
+ end
+ end
+ end
+ end
+ end
+end