Permalink
Browse files

Extract RadioButton

  • Loading branch information...
1 parent f42e1db commit d6b9eb9b4417f1f8705284e520b796466a690ed6 @rafaelfranca rafaelfranca committed Jan 17, 2012
@@ -850,7 +850,7 @@ def check_box(object_name, method, options = {}, checked_value = "1", unchecked_
# # => <input type="radio" id="user_receive_newsletter_yes" name="user[receive_newsletter]" value="yes" />
# # <input type="radio" id="user_receive_newsletter_no" name="user[receive_newsletter]" value="no" checked="checked" />
def radio_button(object_name, method, tag_value, options = {})
- InstanceTag.new(object_name, method, self, options.delete(:object)).to_radio_button_tag(tag_value, options)
+ ActionView::Helpers::Tags::RadioButton.new(object_name, method, self, tag_value, options).render
end
# Returns an input of type "search" for accessing a specified attribute (identified by +method+) on an object
@@ -959,7 +959,6 @@ class InstanceTag
attr_reader :object, :method_name, :object_name
DEFAULT_FIELD_OPTIONS = { "size" => 30 }
- DEFAULT_RADIO_OPTIONS = { }
def initialize(object_name, method_name, template_object, object = nil)
@object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
@@ -994,21 +993,6 @@ def to_number_field_tag(field_type, options = {})
to_input_field_tag(field_type, options)
end
- def to_radio_button_tag(tag_value, options = {})
- options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
- options["type"] = "radio"
- options["value"] = tag_value
- if options.has_key?("checked")
- cv = options.delete "checked"
- checked = cv == true || cv == "checked"
- else
- checked = self.class.radio_button_checked?(value(object), tag_value)
- end
- options["checked"] = "checked" if checked
- add_default_name_and_id_for_value(tag_value, options)
- tag("input", options)
- end
-
def to_boolean_select_tag(options = {})
options = options.stringify_keys
add_default_name_and_id(options)
@@ -1066,10 +1050,6 @@ def value_before_type_cast(object, method_name)
object.send(method_name)
end
end
-
- def radio_button_checked?(value, checked_value)
- value.to_s == checked_value.to_s
- end
end
private
@@ -9,6 +9,7 @@ module Tags
autoload :FileField, 'action_view/helpers/tags/file_field'
autoload :TextArea, 'action_view/helpers/tags/text_area'
autoload :CheckBox, 'action_view/helpers/tags/check_box'
+ autoload :RadioButton, 'action_view/helpers/tags/radio_button'
end
end
end
@@ -24,6 +24,10 @@ def render(&block)
private
+ def value(object)
+ object.send @method_name if object
+ end
+
def value_before_type_cast(object)
unless object.nil?
object.respond_to?(@method_name + "_before_type_cast") ?
@@ -34,10 +34,6 @@ def render
private
- def value(object)
- object.send @method_name if object
- end
-
def check_box_checked?(value)
case value
when TrueClass, FalseClass
@@ -0,0 +1,35 @@
+module ActionView
+ module Helpers
+ module Tags
+ class RadioButton < Base #:nodoc:
+ def initialize(object_name, method_name, template_object, tag_value, options)
+ @tag_value = tag_value
+ super(object_name, method_name, template_object, options)
+ end
+
+ def render
+ options = @options.stringify_keys
+ options["type"] = "radio"
+ options["value"] = @tag_value
+
+ if options.has_key?("checked")
+ cv = options.delete "checked"
+ checked = cv == true || cv == "checked"
+ else
+ checked = radio_button_checked?(value(object))
+ end
+
+ options["checked"] = "checked" if checked
+ add_default_name_and_id_for_value(@tag_value, options)
+ tag("input", options)
+ end
+
+ private
+
+ def radio_button_checked?(value)
+ value.to_s == @tag_value.to_s
+ end
+ end
+ end
+ end
+end

0 comments on commit d6b9eb9

Please sign in to comment.