From ba6bc460ac0ed8da14a0370a13578e7ee5e6d43b Mon Sep 17 00:00:00 2001 From: Megan Bowra-Dean Date: Wed, 7 May 2014 18:35:07 +1200 Subject: [PATCH] Add item label class option for collection inputs. This allows checkbox and radio collections to be styled with the checkbox-inline and radio-inline styles from boostrap 3 when paired with setting the item wrapper tag to false. --- lib/simple_form/tags.rb | 1 + .../inputs/collection_check_boxes_input_test.rb | 17 +++++++++++++++++ .../collection_radio_buttons_input_test.rb | 17 +++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/lib/simple_form/tags.rb b/lib/simple_form/tags.rb index e38c1774..cc0e08f1 100644 --- a/lib/simple_form/tags.rb +++ b/lib/simple_form/tags.rb @@ -19,6 +19,7 @@ module SimpleForm label_options = {} add_default_name_and_id_for_value(value, label_options) label_options['for'] = label_options.delete('id') + label_options['class'] = @options.fetch(:item_label_class, '') rendered_item = content_tag(:label, rendered_item, label_options) end diff --git a/test/inputs/collection_check_boxes_input_test.rb b/test/inputs/collection_check_boxes_input_test.rb index dfafc384..95c25a14 100644 --- a/test/inputs/collection_check_boxes_input_test.rb +++ b/test/inputs/collection_check_boxes_input_test.rb @@ -245,6 +245,23 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase end end + test 'input check boxes with nested style renders item labels with specified class' do + swap SimpleForm, boolean_style: :nested do + with_input_for @user, :active, :check_boxes, item_label_class: "test" + + assert_select 'span.checkbox > label.test > input' + end + end + + test 'input check boxes with nested style and falsey input wrapper renders item labels with specified class' do + swap SimpleForm, boolean_style: :nested, item_wrapper_tag: false do + with_input_for @user, :active, :check_boxes, item_label_class: "checkbox-inline" + + assert_select 'label.checkbox-inline > input' + assert_no_select 'span.checkbox' + end + end + test 'input check boxes wrapper class are not included when set to falsey' do swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do with_input_for @user, :gender, :check_boxes, collection: [:male, :female] diff --git a/test/inputs/collection_radio_buttons_input_test.rb b/test/inputs/collection_radio_buttons_input_test.rb index 70fc8a2a..948318d4 100644 --- a/test/inputs/collection_radio_buttons_input_test.rb +++ b/test/inputs/collection_radio_buttons_input_test.rb @@ -368,6 +368,23 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase end end + test 'input radio with nested style renders item labels with specified class' do + swap SimpleForm, boolean_style: :nested do + with_input_for @user, :active, :radio_buttons, item_label_class: "test" + + assert_select 'span.radio > label.test > input' + end + end + + test 'input radio with nested style and falsey input wrapper renders item labels with specified class' do + swap SimpleForm, boolean_style: :nested, item_wrapper_tag: false do + with_input_for @user, :active, :radio_buttons, item_label_class: "radio-inline" + + assert_select 'label.radio-inline > input' + assert_no_select 'span.radio' + end + end + test 'input radio wrapper class are not included when set to falsey' do swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do with_input_for @user, :gender, :radio_buttons, collection: [:male, :female]