diff --git a/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb index 5f1e9ec02634f90a123216eee0562bdaf5944f7c..e23f5113fb8669640dba8e0d55ab9733c53adbd6 100644 --- a/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb +++ b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb @@ -14,9 +14,9 @@ def check_box(extra_html_options={}) end def render - rendered_collection = render_collection do |value, text, default_html_options| + rendered_collection = render_collection do |item, value, text, default_html_options| default_html_options[:multiple] = true - builder = instantiate_builder(CheckBoxBuilder, value, text, default_html_options) + builder = instantiate_builder(CheckBoxBuilder, item, value, text, default_html_options) if block_given? yield builder diff --git a/actionpack/lib/action_view/helpers/tags/collection_helpers.rb b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb index 1e2e77dde1fe1d66401b2d40f308945577298c51..6f950e552a74e8fb5831f1f3ca98895dd8d3514c 100644 --- a/actionpack/lib/action_view/helpers/tags/collection_helpers.rb +++ b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb @@ -3,13 +3,14 @@ module Helpers module Tags module CollectionHelpers class Builder - attr_reader :text, :value + attr_reader :object, :text, :value - def initialize(template_object, object_name, method_name, + def initialize(template_object, object_name, method_name, object, sanitized_attribute_name, text, value, input_html_options) @template_object = template_object @object_name = object_name @method_name = method_name + @object = object @sanitized_attribute_name = sanitized_attribute_name @text = text @value = value @@ -32,8 +33,8 @@ def initialize(object_name, method_name, template_object, collection, value_meth private - def instantiate_builder(builder_class, value, text, html_options) - builder_class.new(@template_object, @object_name, @method_name, + def instantiate_builder(builder_class, item, value, text, html_options) + builder_class.new(@template_object, @object_name, @method_name, item, sanitize_attribute_name(value), text, value, html_options) end @@ -71,7 +72,7 @@ def render_collection #:nodoc: text = value_for_collection(item, @text_method) default_html_options = default_html_options_for_collection(item, value) - yield value, text, default_html_options + yield item, value, text, default_html_options end.join.html_safe end end 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 8e7aeeed6314e182029a93a95ff3cec0609183a6..ba2035f0746e466e950ccf933816e1e946483b9a 100644 --- a/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb +++ b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb @@ -14,8 +14,8 @@ def radio_button(extra_html_options={}) end def render - render_collection do |value, text, default_html_options| - builder = instantiate_builder(RadioButtonBuilder, value, text, default_html_options) + render_collection do |item, value, text, default_html_options| + builder = instantiate_builder(RadioButtonBuilder, item, value, text, default_html_options) if block_given? yield builder diff --git a/actionpack/test/template/form_collections_helper_test.rb b/actionpack/test/template/form_collections_helper_test.rb index a4aea8ca569700743997a166efaf4747b655055e..4d878635eff35053c86027d9f501d24474b6430c 100644 --- a/actionpack/test/template/form_collections_helper_test.rb +++ b/actionpack/test/template/form_collections_helper_test.rb @@ -123,6 +123,19 @@ def with_collection_check_boxes(*args, &block) end end + test 'collection radio with block helpers allows access to the current object item in the collection to access extra properties' do + with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b| + b.label(:class => b.object) { b.radio_button + b.text } + end + + assert_select 'label.true[for=user_active_true]', 'true' do + assert_select 'input#user_active_true[type=radio]' + end + assert_select 'label.false[for=user_active_false]', 'false' do + assert_select 'input#user_active_false[type=radio]' + end + end + test 'collection radio buttons with fields for' do collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] @output_buffer = fields_for(:post) do |p| @@ -298,4 +311,17 @@ def with_collection_check_boxes(*args, &block) assert_select 'input#user_active_false[type=checkbox]' end end + + test 'collection check boxes with block helpers allows access to the current object item in the collection to access extra properties' do + with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b| + b.label(:class => b.object) { b.check_box + b.text } + end + + assert_select 'label.true[for=user_active_true]', 'true' do + assert_select 'input#user_active_true[type=checkbox]' + end + assert_select 'label.false[for=user_active_false]', 'false' do + assert_select 'input#user_active_false[type=checkbox]' + end + end end