aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/tags/check_box.rb
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2012-01-17 14:08:09 -0800
committerXavier Noria <fxn@hashref.com>2012-01-17 14:08:09 -0800
commit57aaaa61977e82b9de2c43c26b00e636030685c4 (patch)
tree493351f06c1e8bc9fe5131015e72a963f9f2806d /actionpack/lib/action_view/helpers/tags/check_box.rb
parentba154bd76fb481d9faca123e988924bd864c0318 (diff)
parent6f1bf526d7f1869b47f6047c4285c673bb06d0ec (diff)
downloadrails-57aaaa61977e82b9de2c43c26b00e636030685c4.tar.gz
rails-57aaaa61977e82b9de2c43c26b00e636030685c4.tar.bz2
rails-57aaaa61977e82b9de2c43c26b00e636030685c4.zip
Merge pull request #4488 from rafaelfranca/av-refactor
ActionView::Helpers::FormHelper refactoring
Diffstat (limited to 'actionpack/lib/action_view/helpers/tags/check_box.rb')
-rw-r--r--actionpack/lib/action_view/helpers/tags/check_box.rb54
1 files changed, 54 insertions, 0 deletions
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..b3bd6eb2ad
--- /dev/null
+++ b/actionpack/lib/action_view/helpers/tags/check_box.rb
@@ -0,0 +1,54 @@
+require 'action_view/helpers/tags/checkable'
+
+module ActionView
+ module Helpers
+ module Tags
+ class CheckBox < Base #:nodoc:
+ include Checkable
+
+ 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
+ options["checked"] = "checked" if input_checked?(object, options)
+
+ 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 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