From ec19c77ca570919a78efcf2a801863e0eefe98c3 Mon Sep 17 00:00:00 2001 From: Andriel Nuernberg Date: Sun, 1 Dec 2013 04:50:21 -0200 Subject: Label only accepts `:index` and `:namespace` attributes from the input --- actionview/CHANGELOG.md | 25 +++++++++++++++ .../action_view/helpers/tags/collection_helpers.rb | 2 +- actionview/lib/action_view/helpers/tags/label.rb | 1 - .../test/template/form_collections_helper_test.rb | 36 ++++++++++++++++++++++ actionview/test/template/form_helper_test.rb | 36 ++++++++++++++++++++++ 5 files changed, 98 insertions(+), 2 deletions(-) (limited to 'actionview') diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 9e58c193b1..b95ef0e478 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,28 @@ +* Label tags generated by collection helpers only inherit the `:index` and + `:namespace` from the input, because only these attributes modifies the + `for` attribute of the label. Also, the input attributes don't have + precedence over the label attributes anymore. + + Before: + + collection = [[1, true, { class: 'foo' }]] + f.collection_check_boxes :options, collection, :second, :first do |b| + b.label(class: 'my_custom_class') + end + + # => + + After: + + collection = [[1, true, { class: 'foo' }]] + f.collection_check_boxes :options, collection, :second, :first do |b| + b.label(class: 'my_custom_class') + end + + # => + + *Andriel Nuernberg* + * Fixed a long-standing bug in `json_escape` that causes quotation marks to be stripped. This method also escapes the \u2028 and \u2029 unicode newline characters which are treated as \n in JavaScript. This matches the behaviour of the AS::JSON encoder. (The diff --git a/actionview/lib/action_view/helpers/tags/collection_helpers.rb b/actionview/lib/action_view/helpers/tags/collection_helpers.rb index 787039c82e..991f32cea2 100644 --- a/actionview/lib/action_view/helpers/tags/collection_helpers.rb +++ b/actionview/lib/action_view/helpers/tags/collection_helpers.rb @@ -18,7 +18,7 @@ module ActionView end def label(label_html_options={}, &block) - html_options = label_html_options.merge(@input_html_options) + html_options = @input_html_options.slice(:index, :namespace).merge(label_html_options) @template_object.label(@object_name, @sanitized_attribute_name, @text, html_options, &block) end end diff --git a/actionview/lib/action_view/helpers/tags/label.rb b/actionview/lib/action_view/helpers/tags/label.rb index 180aa9ac27..35d3ba8434 100644 --- a/actionview/lib/action_view/helpers/tags/label.rb +++ b/actionview/lib/action_view/helpers/tags/label.rb @@ -30,7 +30,6 @@ module ActionView add_default_name_and_id_for_value(tag_value, name_and_id) options.delete("index") options.delete("namespace") - options.delete("multiple") options["for"] = name_and_id["id"] unless options.key?("for") if block_given? diff --git a/actionview/test/template/form_collections_helper_test.rb b/actionview/test/template/form_collections_helper_test.rb index d28e4aeb48..68c83f2059 100644 --- a/actionview/test/template/form_collections_helper_test.rb +++ b/actionview/test/template/form_collections_helper_test.rb @@ -84,6 +84,24 @@ class FormCollectionsHelperTest < ActionView::TestCase assert_select 'input[type=radio][value=false].bar#user_active_false' end + test 'collection radio sets the label class defined inside the block' do + collection = [[1, true, {class: 'foo'}], [0, false, {class: 'bar'}]] + with_collection_radio_buttons :user, :active, collection, :second, :first do |b| + b.label(class: "collection_radio_buttons") + end + + assert_select 'label.collection_radio_buttons[for=user_active_true]' + assert_select 'label.collection_radio_buttons[for=user_active_false]' + end + + test 'collection radio does not include the input class in the respective label' do + collection = [[1, true, {class: 'foo'}], [0, false, {class: 'bar'}]] + with_collection_radio_buttons :user, :active, collection, :second, :first + + assert_no_select 'label.foo[for=user_active_true]' + assert_no_select 'label.bar[for=user_active_false]' + end + test 'collection radio does not wrap input inside the label' do with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s @@ -215,6 +233,24 @@ class FormCollectionsHelperTest < ActionView::TestCase assert_select 'input[type=checkbox][value=2].bar' end + test 'collection check boxes sets the label class defined inside the block' do + collection = [[1, 'Category 1', {class: 'foo'}], [2, 'Category 2', {class: 'bar'}]] + with_collection_check_boxes :user, :active, collection, :second, :first do |b| + b.label(class: 'collection_check_boxes') + end + + assert_select 'label.collection_check_boxes[for=user_active_category_1]' + assert_select 'label.collection_check_boxes[for=user_active_category_2]' + end + + test 'collection check boxes does not include the input class in the respective label' do + collection = [[1, 'Category 1', {class: 'foo'}], [2, 'Category 2', {class: 'bar'}]] + with_collection_check_boxes :user, :active, collection, :second, :first + + assert_no_select 'label.foo[for=user_active_category_1]' + assert_no_select 'label.bar[for=user_active_category_2]' + end + test 'collection check boxes accepts selected values as :checked option' do collection = (1..3).map{|i| [i, "Category #{i}"] } with_collection_check_boxes :user, :category_ids, collection, :first, :last, :checked => [1, 3] diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index 3e8a2468ed..90f7a412fb 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -1300,6 +1300,24 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_with_index_and_with_collection_radio_buttons + post = Post.new + def post.active; false; end + + form_for(post, index: '1') do |f| + concat f.collection_radio_buttons(:active, [true, false], :to_s, :to_s) + end + + expected = whole_form("/posts", "new_post", "new_post") do + "" + + "" + + "" + + "" + end + + assert_dom_equal expected, output_buffer + end + def test_form_for_with_collection_check_boxes post = Post.new def post.tag_ids; [1, 3]; end @@ -1397,6 +1415,24 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_with_index_and_with_collection_check_boxes + post = Post.new + def post.tag_ids; [1]; end + collection = [[1, "Tag 1"]] + + form_for(post, index: '1') do |f| + concat f.collection_check_boxes(:tag_ids, collection, :first, :last) + end + + expected = whole_form("/posts", "new_post", "new_post") do + "" + + "" + + "" + end + + assert_dom_equal expected, output_buffer + end + def test_form_for_with_file_field_generate_multipart Post.send :attr_accessor, :file -- cgit v1.2.3