diff --git a/.travis.yml b/.travis.yml index cfbd07b5..35ee55bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,10 +11,6 @@ gemfile: - gemfiles/Gemfile.rails-4-0-stable - gemfiles/Gemfile.rails-4-1-stable - Gemfile -matrix: - allow_failures: - - gemfile: Gemfile - - gemfile: gemfiles/Gemfile.rails-4-1-stable notifications: email: false campfire: diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e959e5c..1158585c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Add `include_default_input_wrapper_class` config [@luizcosta](https://github.com/luizcosta) * Map `datetime`, `date` and `time` input types to their respective HTML5 input tags when the `:html5` is set to `true` [@volmer](https://github.com/volmer) + * Add `boolean_label_class` config. * Add `:html` option to include additional attributes on custom wrappers [remofritzsche](https://github.com/remofritzsche) and [@ulissesalmeida](https://github.com/ulissesalmeida) ### bug fix diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb b/lib/generators/simple_form/templates/config/initializers/simple_form.rb index 328bea0c..8265128f 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb @@ -143,6 +143,9 @@ SimpleForm.setup do |config| # Default class for inputs # config.input_class = nil + # Define the default class of the input wrapper of the boolean input. + config.boolean_label_class = 'checkbox' + # Defines if the default input wrapper class should be included in radio # collection wrappers. # config.include_default_input_wrapper_class = true diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 4f977dea..a241bf27 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -157,6 +157,10 @@ module SimpleForm mattr_accessor :include_default_input_wrapper_class @@include_default_input_wrapper_class = true + # Define the default class of the input wrapper of the boolean input. + mattr_accessor :boolean_label_class + @@boolean_label_class = 'checkbox' + ## WRAPPER CONFIGURATION # The default wrapper to be used by the FormBuilder. mattr_accessor :default_wrapper diff --git a/lib/simple_form/inputs/boolean_input.rb b/lib/simple_form/inputs/boolean_input.rb index 0ea52f19..b649685b 100644 --- a/lib/simple_form/inputs/boolean_input.rb +++ b/lib/simple_form/inputs/boolean_input.rb @@ -4,7 +4,7 @@ module SimpleForm def input if nested_boolean_style? build_hidden_field_for_checkbox + - template.label_tag(nil, class: "checkbox") { + template.label_tag(nil, class: SimpleForm.boolean_label_class) { build_check_box_without_hidden_field + inline_label } else @@ -18,7 +18,7 @@ module SimpleForm elsif nested_boolean_style? html_options = label_html_options.dup html_options[:class] ||= [] - html_options[:class].push(:checkbox) + html_options[:class].push(SimpleForm.boolean_label_class) if SimpleForm.boolean_label_class build_hidden_field_for_checkbox + @builder.label(label_target, html_options) { diff --git a/test/inputs/boolean_input_test.rb b/test/inputs/boolean_input_test.rb index ae9dcd6d..e10a1b3b 100644 --- a/test/inputs/boolean_input_test.rb +++ b/test/inputs/boolean_input_test.rb @@ -128,6 +128,16 @@ class BooleanInputTest < ActionView::TestCase end end + test 'input boolean with nested style works using :input only in wrapper config (no label_input), adding the extra label wrapper with custom class' do + swap_wrapper do + swap SimpleForm, boolean_style: :nested, boolean_label_class: 'foo' do + with_input_for @user, :active, :boolean + + assert_select 'label.boolean + input[type=hidden] + label.foo > input.boolean' + end + end + end + test 'input boolean with nested style works using :label_input in wrapper config, adding "checkbox" class to label' do swap_wrapper :default, self.custom_wrapper_without_top_level do swap SimpleForm, boolean_style: :nested do @@ -138,6 +148,16 @@ class BooleanInputTest < ActionView::TestCase end end + test 'input boolean with nested style works using :label_input in wrapper config, adding custom class to label' do + swap_wrapper :default, self.custom_wrapper_without_top_level do + swap SimpleForm, boolean_style: :nested, boolean_label_class: 'foo' do + with_input_for @user, :active, :boolean + + assert_select 'input[type=hidden] + label.boolean.foo > input.boolean' + end + end + end + test 'input boolean without additional classes should add "checkbox" class to label' do swap_wrapper :default, self.custom_wrapper_without_top_level do swap SimpleForm, boolean_style: :nested, generate_additional_classes_for: [:input] do