Skip to content

Commit

Permalink
Allow collection radio_buttons/check_boxes to access current text/value
Browse files Browse the repository at this point in the history
[Carlos Antonio da Silva + Rafael Mendonça França]
  • Loading branch information
carlosantoniodasilva authored and rafaelfranca committed Feb 2, 2012
1 parent f506c80 commit 0f23426
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions actionpack/lib/action_view/helpers/form_options_helper.rb
Expand Up @@ -573,6 +573,13 @@ def time_zone_options_for_select(selected = nil, priority_zones = nil, model = :
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b| # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
# b.label(:class => "radio_button") { b.radio_button(:class => "radio_button") } # b.label(:class => "radio_button") { b.radio_button(:class => "radio_button") }
# end # end
#
# There are also two special methods available: <tt>text</tt> and
# <tt>value</tt>, which are the current text and value methods for the
# item being rendered, respectively. You can use them like this:
# collection_radio_buttons(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
# b.label(:"data-value" => b.value) { b.radio_button + b.text }
# end
def collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block) def collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Tags::CollectionRadioButtons.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block) Tags::CollectionRadioButtons.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
end end
Expand Down Expand Up @@ -629,6 +636,13 @@ def collection_radio_buttons(object, method, collection, value_method, text_meth
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b| # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
# b.label(:class => "check_box") { b.check_box(:class => "check_box") } # b.label(:class => "check_box") { b.check_box(:class => "check_box") }
# end # end
#
# There are also two special methods available: <tt>text</tt> and
# <tt>value</tt>, which are the current text and value methods for the
# item being rendered, respectively. You can use them like this:
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
# b.label(:"data-value" => b.value) { b.check_box + b.text }
# end
def collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block) def collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Tags::CollectionCheckBoxes.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block) Tags::CollectionCheckBoxes.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
end end
Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_view/helpers/tags/collection_helpers.rb
Expand Up @@ -3,6 +3,8 @@ module Helpers
module Tags module Tags
module CollectionHelpers module CollectionHelpers
class Builder class Builder
attr_reader :text, :value

def initialize(template_object, object_name, method_name, def initialize(template_object, object_name, method_name,
sanitized_attribute_name, text, value, input_html_options) sanitized_attribute_name, text, value, input_html_options)
@template_object = template_object @template_object = template_object
Expand Down
44 changes: 44 additions & 0 deletions actionpack/test/template/form_collections_helper_test.rb
Expand Up @@ -101,6 +101,28 @@ def with_collection_check_boxes(*args, &block)
assert_select 'label[for=user_active_false] + input#user_active_false[type=radio]' assert_select 'label[for=user_active_false] + input#user_active_false[type=radio]'
end end


test 'collection radio with block helpers accept extra html options' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
b.label(:class => "radio_button") + b.radio_button(:class => "radio_button")
end

assert_select 'label.radio_button[for=user_active_true] + input#user_active_true.radio_button[type=radio]'
assert_select 'label.radio_button[for=user_active_false] + input#user_active_false.radio_button[type=radio]'
end

test 'collection radio with block helpers allows access to current text and value' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
b.label(:"data-value" => b.value) { b.radio_button + b.text }
end

assert_select 'label[for=user_active_true][data-value=true]', 'true' do
assert_select 'input#user_active_true[type=radio]'
end
assert_select 'label[for=user_active_false][data-value=false]', 'false' do
assert_select 'input#user_active_false[type=radio]'
end
end

test 'collection radio buttons with fields for' do test 'collection radio buttons with fields for' do
collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')] collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')]
@output_buffer = fields_for(:post) do |p| @output_buffer = fields_for(:post) do |p|
Expand Down Expand Up @@ -254,4 +276,26 @@ def with_collection_check_boxes(*args, &block)
assert_select 'label[for=user_active_true] + input#user_active_true[type=checkbox]' assert_select 'label[for=user_active_true] + input#user_active_true[type=checkbox]'
assert_select 'label[for=user_active_false] + input#user_active_false[type=checkbox]' assert_select 'label[for=user_active_false] + input#user_active_false[type=checkbox]'
end end

test 'collection check boxes with block helpers accept extra html options' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
b.label(:class => "check_box") + b.check_box(:class => "check_box")
end

assert_select 'label.check_box[for=user_active_true] + input#user_active_true.check_box[type=checkbox]'
assert_select 'label.check_box[for=user_active_false] + input#user_active_false.check_box[type=checkbox]'
end

test 'collection check boxes with block helpers allows access to current text and value' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
b.label(:"data-value" => b.value) { b.check_box + b.text }
end

assert_select 'label[for=user_active_true][data-value=true]', 'true' do
assert_select 'input#user_active_true[type=checkbox]'
end
assert_select 'label[for=user_active_false][data-value=false]', 'false' do
assert_select 'input#user_active_false[type=checkbox]'
end
end
end end

0 comments on commit 0f23426

Please sign in to comment.