Collection radio / check boxes respect :nested boolean style config
This commit is contained in:
parent
1b2a4b043d
commit
ec10a9669e
|
@ -86,6 +86,10 @@ module SimpleForm
|
|||
column && column.limit
|
||||
end
|
||||
|
||||
def nested_boolean_style?
|
||||
options.fetch(:boolean_style, SimpleForm.boolean_style) == :nested
|
||||
end
|
||||
|
||||
# Find reflection name when available, otherwise use attribute
|
||||
def reflection_or_attribute_name
|
||||
reflection ? reflection.name : attribute_name
|
||||
|
|
|
@ -8,7 +8,7 @@ module SimpleForm
|
|||
def label_input
|
||||
if options[:label] == false
|
||||
input
|
||||
elsif nested_style?
|
||||
elsif nested_boolean_style?
|
||||
@builder.label(attribute_name) { input }
|
||||
else
|
||||
input + label
|
||||
|
@ -17,10 +17,6 @@ module SimpleForm
|
|||
|
||||
private
|
||||
|
||||
def nested_style?
|
||||
options.fetch(:boolean_style, SimpleForm.boolean_style) == :nested
|
||||
end
|
||||
|
||||
# Booleans are not required by default because in most of the cases
|
||||
# it makes no sense marking them as required. The only exception is
|
||||
# Terms of Use usually presented at most sites sign up screen.
|
||||
|
|
|
@ -15,7 +15,8 @@ module SimpleForm
|
|||
def input
|
||||
label_method, value_method = detect_collection_methods
|
||||
@builder.send(:"collection_#{input_type}", attribute_name, collection,
|
||||
value_method, label_method, input_options, input_html_options)
|
||||
value_method, label_method, input_options, input_html_options,
|
||||
&collection_block_for_nested_boolean_style)
|
||||
end
|
||||
|
||||
def input_options
|
||||
|
@ -69,6 +70,20 @@ module SimpleForm
|
|||
].compact.presence
|
||||
end
|
||||
|
||||
def collection_block_for_nested_boolean_style
|
||||
return unless nested_boolean_style?
|
||||
|
||||
if check_boxes?
|
||||
proc do |label_for, text, value, html_options|
|
||||
@builder.label(label_for, text) { @builder.check_box(attribute_name, html_options, value) }
|
||||
end
|
||||
elsif radio?
|
||||
proc do |label_for, text, value, html_options|
|
||||
@builder.label(label_for, text) { @builder.radio_button(attribute_name, value, html_options) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Detect the right method to find the label and value for a collection.
|
||||
# If no label or value method are defined, will attempt to find them based
|
||||
# on default label and value methods that can be configured through
|
||||
|
|
|
@ -499,6 +499,16 @@ class CollectionInputTest < ActionView::TestCase
|
|||
assert_select 'form span input[type=radio]', :count => 2
|
||||
end
|
||||
|
||||
test 'input radio respects the nested boolean style config, generating nested label > input' do
|
||||
swap SimpleForm, :boolean_style => :nested do
|
||||
with_input_for @user, :active, :radio
|
||||
|
||||
assert_select 'label[for=user_active_true] > input#user_active_true[type=radio]'
|
||||
assert_select 'label[for=user_active_false] > input#user_active_false[type=radio]'
|
||||
assert_no_select 'label.collection_radio'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input check boxes does not wrap the collection by default' do
|
||||
with_input_for @user, :active, :check_boxes
|
||||
|
||||
|
@ -605,4 +615,14 @@ class CollectionInputTest < ActionView::TestCase
|
|||
|
||||
assert_select 'form span input[type=checkbox]', :count => 2
|
||||
end
|
||||
|
||||
test 'input check boxes respects the nested boolean style config, generating nested label > input' do
|
||||
swap SimpleForm, :boolean_style => :nested do
|
||||
with_input_for @user, :active, :check_boxes
|
||||
|
||||
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_no_select 'label.collection_radio'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue