Allow giving a block to collection radio/check boxes

This will allow more control for the user who wants to handle how to
render label / check boxes, ie change their order or put the label
inside the check box.
This commit is contained in:
Carlos Antonio da Silva 2012-01-26 15:34:23 -02:00
parent a6c5d8f1d6
commit fb796f0a85
2 changed files with 36 additions and 8 deletions

View File

@ -41,8 +41,12 @@ module SimpleForm
render_collection(
attribute, collection, value_method, text_method, options, html_options
) do |value, text, default_html_options|
radio_button(attribute, value, default_html_options) +
label(sanitize_attribute_name(attribute, value), text, :class => "collection_radio")
if block_given?
yield sanitize_attribute_name(attribute, value), text, value, default_html_options
else
radio_button(attribute, value, default_html_options) +
label(sanitize_attribute_name(attribute, value), text, :class => "collection_radio")
end
end
end
@ -87,8 +91,12 @@ module SimpleForm
) do |value, text, default_html_options|
default_html_options[:multiple] = true
check_box(attribute, default_html_options, value, '') +
label(sanitize_attribute_name(attribute, value), text, :class => "collection_check_boxes")
if block_given?
yield sanitize_attribute_name(attribute, value), text, value, default_html_options
else
check_box(attribute, default_html_options, value, '') +
label(sanitize_attribute_name(attribute, value), text, :class => "collection_check_boxes")
end
end
end

View File

@ -8,15 +8,15 @@ class BuilderTest < ActionView::TestCase
end
end
def with_collection_radio(object, attribute, collection, value_method, text_method, options={}, html_options={})
def with_collection_radio(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
with_concat_form_for(object) do |f|
f.collection_radio attribute, collection, value_method, text_method, options, html_options
f.collection_radio attribute, collection, value_method, text_method, options, html_options, &block
end
end
def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={})
def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
with_concat_form_for(object) do |f|
f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options
f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options, &block
end
end
@ -146,9 +146,19 @@ class BuilderTest < ActionView::TestCase
test 'collection radio does not wrap input inside the label' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form input[type=radio] + label'
assert_no_select 'form label input'
end
test 'collection radio accepts a block to render the radio and label as required' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s do |label_for, text, value, html_options|
label(:user, label_for, text) { radio_button(:user, :active, value, html_options) }
end
assert_select 'form label[for=user_active_true] > input#user_active_true[type=radio]'
assert_select 'form label[for=user_active_false] > input#user_active_false[type=radio]'
end
# COLLECTION CHECK BOX
test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
@ -323,9 +333,19 @@ class BuilderTest < ActionView::TestCase
test 'collection check box does not wrap input inside the label' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_select 'form input[type=checkbox] + label'
assert_no_select 'form label input'
end
test 'collection check box accepts a block to render the radio and label as required' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s do |label_for, text, value, html_options|
label(:user, label_for, text) { check_box(:user, :active, html_options, value) }
end
assert_select 'form label[for=user_active_true] > input#user_active_true[type=checkbox]'
assert_select 'form label[for=user_active_false] > input#user_active_false[type=checkbox]'
end
# SIMPLE FIELDS
test 'simple fields for is available and yields an instance of FormBuilder' do
with_concat_form_for(@user) do |f|