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:
parent
a6c5d8f1d6
commit
fb796f0a85
|
@ -41,8 +41,12 @@ module SimpleForm
|
||||||
render_collection(
|
render_collection(
|
||||||
attribute, collection, value_method, text_method, options, html_options
|
attribute, collection, value_method, text_method, options, html_options
|
||||||
) do |value, text, default_html_options|
|
) do |value, text, default_html_options|
|
||||||
radio_button(attribute, value, default_html_options) +
|
if block_given?
|
||||||
label(sanitize_attribute_name(attribute, value), text, :class => "collection_radio")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -87,8 +91,12 @@ module SimpleForm
|
||||||
) do |value, text, default_html_options|
|
) do |value, text, default_html_options|
|
||||||
default_html_options[:multiple] = true
|
default_html_options[:multiple] = true
|
||||||
|
|
||||||
check_box(attribute, default_html_options, value, '') +
|
if block_given?
|
||||||
label(sanitize_attribute_name(attribute, value), text, :class => "collection_check_boxes")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,15 +8,15 @@ class BuilderTest < ActionView::TestCase
|
||||||
end
|
end
|
||||||
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|
|
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
|
||||||
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|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -146,9 +146,19 @@ class BuilderTest < ActionView::TestCase
|
||||||
test 'collection radio does not wrap input inside the label' do
|
test 'collection radio does not wrap input inside the label' do
|
||||||
with_collection_radio @user, :active, [true, false], :to_s, :to_s
|
with_collection_radio @user, :active, [true, false], :to_s, :to_s
|
||||||
|
|
||||||
|
assert_select 'form input[type=radio] + label'
|
||||||
assert_no_select 'form label input'
|
assert_no_select 'form label input'
|
||||||
end
|
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
|
# COLLECTION CHECK BOX
|
||||||
test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do
|
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')]
|
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
|
test 'collection check box does not wrap input inside the label' do
|
||||||
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
|
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'
|
assert_no_select 'form label input'
|
||||||
end
|
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
|
# SIMPLE FIELDS
|
||||||
test 'simple fields for is available and yields an instance of FormBuilder' do
|
test 'simple fields for is available and yields an instance of FormBuilder' do
|
||||||
with_concat_form_for(@user) do |f|
|
with_concat_form_for(@user) do |f|
|
||||||
|
|
Loading…
Reference in New Issue