From 106e7751505fe6ba6bc5a767084f87c30b886fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 25 Nov 2014 17:59:32 -0200 Subject: [PATCH] Generate right for attribute for labels in collection inputs It is wrong if namespace or index option is being used. Fixes #1161 --- CHANGELOG.md | 1 + lib/simple_form/tags.rb | 6 ++--- .../collection_check_boxes_input_test.rb | 22 +++++++++++++++++ .../collection_radio_buttons_input_test.rb | 24 ++++++++++++++++++- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b101c17..54923780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### bug fix * Fix `full_error` when the attribute is an association. [@mvdamme](https://github.com/jorge-d) + * Fix suppport to `:namespace` and `:index` options for nested check boxes and radio buttons when the attribute is an association. ## 3.1.0.rc2 diff --git a/lib/simple_form/tags.rb b/lib/simple_form/tags.rb index fde30313..4bf67ad8 100644 --- a/lib/simple_form/tags.rb +++ b/lib/simple_form/tags.rb @@ -16,11 +16,9 @@ module SimpleForm rendered_item = yield item, value, text, default_html_options.merge(additional_html_options) if @options.fetch(:boolean_style, SimpleForm.boolean_style) == :nested - label_options = {} - add_default_name_and_id_for_value(value, label_options) - label_options['for'] = label_options.delete('id') + label_options = default_html_options.slice(:index, :namespace) label_options['class'] = @options[:item_label_class] - rendered_item = content_tag(:label, rendered_item, label_options) + rendered_item = @template_object.label(@object_name, sanitize_attribute_name(value), rendered_item, label_options) end item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, class: item_wrapper_class) : rendered_item diff --git a/test/inputs/collection_check_boxes_input_test.rb b/test/inputs/collection_check_boxes_input_test.rb index 0425ae9f..adb64198 100644 --- a/test/inputs/collection_check_boxes_input_test.rb +++ b/test/inputs/collection_check_boxes_input_test.rb @@ -278,4 +278,26 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase assert_select 'span.custom' end end + + test 'input check boxes with nested style and namespace uses the right for attribute' do + swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do + with_concat_form_for @user, namespace: :foo do |f| + concat f.input :gender, as: :check_boxes, collection: [:male, :female] + end + + assert_select 'label[for=foo_user_gender_male]' + assert_select 'label[for=foo_user_gender_female]' + end + end + + test 'input check boxes with nested style and index uses the right for attribute' do + swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do + with_concat_form_for @user, index: 1 do |f| + concat f.input :gender, as: :check_boxes, collection: [:male, :female] + end + + assert_select 'label[for=user_1_gender_male]' + assert_select 'label[for=user_1_gender_female]' + end + end end diff --git a/test/inputs/collection_radio_buttons_input_test.rb b/test/inputs/collection_radio_buttons_input_test.rb index 21e8fe44..2b301d9d 100644 --- a/test/inputs/collection_radio_buttons_input_test.rb +++ b/test/inputs/collection_radio_buttons_input_test.rb @@ -393,7 +393,7 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase end end - test 'input check boxes custom wrapper class is included when include input wrapper class is falsey' do + test 'input radio custom wrapper class is included when include input wrapper class is falsey' do swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do with_input_for @user, :gender, :radio_buttons, collection: [:male, :female], item_wrapper_class: 'custom' @@ -401,4 +401,26 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase assert_select 'span.custom' end end + + test 'input radio with nested style and namespace uses the right for attribute' do + swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do + with_concat_form_for @user, namespace: :foo do |f| + concat f.input :gender, as: :radio_buttons, collection: [:male, :female] + end + + assert_select 'label[for=foo_user_gender_male]' + assert_select 'label[for=foo_user_gender_female]' + end + end + + test 'input radio with nested style and index uses the right for attribute' do + swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do + with_concat_form_for @user, index: 1 do |f| + concat f.input :gender, as: :radio_buttons, collection: [:male, :female] + end + + assert_select 'label[for=user_1_gender_male]' + assert_select 'label[for=user_1_gender_female]' + end + end end