diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt index 47f07e79..c6acac46 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt @@ -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 diff --git a/lib/simple_form.rb b/lib/simple_form.rb index fd1a64b2..d8ae1673 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -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 diff --git a/lib/simple_form/inputs/boolean_input.rb b/lib/simple_form/inputs/boolean_input.rb index a806a7b8..67e32be5 100644 --- a/lib/simple_form/inputs/boolean_input.rb +++ b/lib/simple_form/inputs/boolean_input.rb @@ -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 \ No newline at end of file +end diff --git a/test/inputs/boolean_input_test.rb b/test/inputs/boolean_input_test.rb index c923bac8..0b9f5152 100644 --- a/test/inputs/boolean_input_test.rb +++ b/test/inputs/boolean_input_test.rb @@ -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