aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-02-01 16:14:24 -0200
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-02-02 09:40:24 -0200
commit17d214a1d4c71db39d2a4cab4d18ccea9f5b8ab5 (patch)
tree77eb411483d90accbc1f3b009a4d0a48a706bdf5 /actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
parentc988aaf69086af78dcb6eebf357e1149fc45e2ac (diff)
downloadrails-17d214a1d4c71db39d2a4cab4d18ccea9f5b8ab5.tar.gz
rails-17d214a1d4c71db39d2a4cab4d18ccea9f5b8ab5.tar.bz2
rails-17d214a1d4c71db39d2a4cab4d18ccea9f5b8ab5.zip
Create a Builder factory class to use with collection helpers + block
This will make it easy for the user to handle how check box/radio and labels should be generated, abstracting any text/value/default html options required to make it work. [Carlos Antonio da Silva + Rafael Mendonça França]
Diffstat (limited to 'actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb')
-rw-r--r--actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb39
1 files changed, 31 insertions, 8 deletions
diff --git a/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
index 60d017cfe5..ed64a3cfbd 100644
--- a/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
+++ b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
@@ -2,25 +2,48 @@ module ActionView
module Helpers
module Tags
class CollectionRadioButtons < CollectionSelect
+ class Builder
+ def initialize(template_object, object_name, method_name,
+ sanitized_attribute_name, text, value, input_html_options)
+ @template_object = template_object
+ @object_name = object_name
+ @method_name = method_name
+ @sanitized_attribute_name = sanitized_attribute_name
+ @text = text
+ @value = value
+ @input_html_options = input_html_options
+ end
+
+ def label(label_html_options={}, &block)
+ @template_object.label(@object_name, @sanitized_attribute_name, @text, label_html_options, &block)
+ end
+ end
+
+ class RadioButtonBuilder < Builder
+ def radio_button(extra_html_options={})
+ html_options = extra_html_options.merge(@input_html_options)
+ @template_object.radio_button(@object_name, @method_name, @value, html_options)
+ end
+ end
+
def render
render_collection do |value, text, default_html_options|
+ builder = instantiate_builder(RadioButtonBuilder, value, text, default_html_options)
+
if block_given?
- yield sanitize_attribute_name(value), text, value, default_html_options
+ yield builder
else
- radio_button(value, default_html_options) +
- label(value, text, "collection_radio_buttons")
+ builder.radio_button + builder.label(:class => "collection_radio_buttons")
end
end
end
private
- def radio_button(value, html_options)
- @template_object.radio_button(@object_name, @method_name, value, html_options)
- end
+ def instantiate_builder(builder_class, value, text, html_options)
+ builder_class.new(@template_object, @object_name, @method_name,
+ sanitize_attribute_name(value), text, value, html_options)
- def label(value, text, css_class)
- @template_object.label(@object_name, sanitize_attribute_name(value), text, :class => css_class)
end
# Generate default options for collection helpers, such as :checked and