Add checkbox_style config, to opt for :inline or :nested
This commit is contained in:
parent
e2938a4c56
commit
32952b9132
|
@ -138,6 +138,11 @@ SimpleForm.setup do |config|
|
|||
# You can define the class to use on all labels. Default is nil.
|
||||
# config.label_class = nil
|
||||
|
||||
# Define the way to render checkboxes with labels. Defaults to :inline.
|
||||
# :inline => input + label
|
||||
# :nested => label > input
|
||||
# config.checkbox_style = :inline
|
||||
|
||||
# You can define the class to use on all forms. Default is simple_form.
|
||||
# config.form_class = :simple_form
|
||||
|
||||
|
|
|
@ -61,6 +61,12 @@ module SimpleForm
|
|||
mattr_accessor :label_class
|
||||
@@label_class = nil
|
||||
|
||||
# Define the way to render checkboxes with labels. Defaults to :inline.
|
||||
# :inline => input + label
|
||||
# :nested => label > input
|
||||
mattr_accessor :checkbox_style
|
||||
@@checkbox_style = :inline
|
||||
|
||||
# You can define the class to use on all forms. Default is simple_form.
|
||||
mattr_accessor :form_class
|
||||
@@form_class = :simple_form
|
||||
|
|
|
@ -6,11 +6,21 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def label_input
|
||||
input + (options[:label] == false ? "" : label)
|
||||
if options[:label] == false
|
||||
input
|
||||
elsif nested_style?
|
||||
@builder.label(attribute_name) { input }
|
||||
else
|
||||
input + label
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def nested_style?
|
||||
options[:checkbox_style] || SimpleForm.checkbox_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.
|
||||
|
@ -19,4 +29,4 @@ module SimpleForm
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,6 +5,40 @@ class BooleanInputTest < ActionView::TestCase
|
|||
test 'input should generate a checkbox by default for boolean attributes' do
|
||||
with_input_for @user, :active, :boolean
|
||||
assert_select 'input[type=checkbox].boolean#user_active'
|
||||
assert_select 'label.boolean.optional', 'Active'
|
||||
end
|
||||
|
||||
test 'input does not generate the label with the checkbox when label option is false' do
|
||||
with_input_for @user, :active, :boolean, :label => false
|
||||
assert_select 'input[type=checkbox].boolean#user_active'
|
||||
assert_no_select 'label'
|
||||
end
|
||||
|
||||
test 'input uses inline checkbox style by default' do
|
||||
with_input_for @user, :active, :boolean
|
||||
assert_select 'input.boolean + label.boolean.optional'
|
||||
assert_no_select 'label > input'
|
||||
end
|
||||
|
||||
test 'input allows changing default checkbox style config to nested, generating a default label' do
|
||||
swap SimpleForm, :checkbox_style => :nested do
|
||||
with_input_for @user, :active, :boolean
|
||||
assert_select 'label > input.boolean'
|
||||
assert_no_select 'label.boolean'
|
||||
end
|
||||
end
|
||||
|
||||
test 'input accepts changing checkbox style through given options' do
|
||||
with_input_for @user, :active, :boolean, :checkbox_style => :nested
|
||||
assert_select 'label > input.boolean'
|
||||
assert_no_select 'label.boolean'
|
||||
end
|
||||
|
||||
test 'input with nested style allows disabling label' do
|
||||
swap SimpleForm, :checkbox_style => :nested do
|
||||
with_input_for @user, :active, :boolean, :label => false
|
||||
assert_select 'input.boolean'
|
||||
assert_no_select 'label'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue