diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index f3131c6bea..f67ae68c39 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,4 +1,22 @@ -* Datatime form helpers (`time_field`, `date_field`, `datetime_field`, `week_field`, `month_field`) now accept an instance of Time/Date/DateTime as `:value` option. +* `select` can now be called with a single hash containing options and some HTML options + + Previously this would not work as expected: + + ```erb + <%= select :post, :author, authors, required: true %> + ``` + + Instead you needed to do this: + + ```erb + <%= select :post, :author, authors, {}, required: true %> + ``` + + Now, either form is accepted, for the following HTML attributes: `required`, `multiple`, `size`. + + *Alex Ghiculescu* + +* Datetime form helpers (`time_field`, `date_field`, `datetime_field`, `week_field`, `month_field`) now accept an instance of Time/Date/DateTime as `:value` option. Before: ```erb diff --git a/actionview/lib/action_view/helpers/tags/base.rb b/actionview/lib/action_view/helpers/tags/base.rb index 04ee963132..e0c9eea987 100644 --- a/actionview/lib/action_view/helpers/tags/base.rb +++ b/actionview/lib/action_view/helpers/tags/base.rb @@ -122,6 +122,10 @@ def sanitized_value(value) def select_content_tag(option_tags, options, html_options) html_options = html_options.stringify_keys + [:required, :multiple, :size].each do |prop| + html_options[prop.to_s] = options.delete(prop) if options.key?(prop) && !html_options.key?(prop.to_s) + end + add_default_name_and_id(html_options) if placeholder_required?(html_options) diff --git a/actionview/test/template/form_options_helper_test.rb b/actionview/test/template/form_options_helper_test.rb index 5059ffd53b..d48c3bea8c 100644 --- a/actionview/test/template/form_options_helper_test.rb +++ b/actionview/test/template/form_options_helper_test.rb @@ -873,52 +873,56 @@ def test_select_with_nil_and_selected_option_as_nil def test_select_with_array @continent = Continent.new @continent.countries = ["Africa", "Europe"] - assert_dom_equal( - %(), - select("continent", "countries", %W(Africa Europe America), { multiple: true }) - ) + + expected_with_hidden_field_and_multiple = %() + expected_without_hidden_field = %() + + assert_dom_equal(expected_with_hidden_field_and_multiple, select("continent", "countries", %W(Africa Europe America), { multiple: true })) + assert_dom_equal(expected_with_hidden_field_and_multiple, select("continent", "countries", %W(Africa Europe America), multiple: true)) + assert_dom_equal(expected_with_hidden_field_and_multiple, select("continent", "countries", %W(Africa Europe America), {}, { multiple: true })) + assert_dom_equal(expected_without_hidden_field, select("continent", "countries", %W(Africa Europe America))) end def test_required_select - assert_dom_equal( - %(), - select("post", "category", %w(abe mus hest), {}, { required: true }) - ) + expected = %() + + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), {}, { required: true })) + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), required: true)) end def test_required_select_with_include_blank_prompt - assert_dom_equal( - %(), - select("post", "category", %w(abe mus hest), { include_blank: "Select one" }, { required: true }) - ) + expected = %() + + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), { include_blank: "Select one" }, { required: true })) + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), include_blank: "Select one", required: true)) end def test_required_select_with_prompt - assert_dom_equal( - %(), - select("post", "category", %w(abe mus hest), { prompt: "Select one" }, { required: true }) - ) + expected = %() + + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), { prompt: "Select one" }, { required: true })) + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), prompt: "Select one", required: true)) end def test_required_select_display_size_equals_to_one - assert_dom_equal( - %(), - select("post", "category", %w(abe mus hest), {}, { required: true, size: 1 }) - ) + expected = %() + + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), {}, { required: true, size: 1 })) + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), required: true, size: 1)) end def test_required_select_with_display_size_bigger_than_one - assert_dom_equal( - %(), - select("post", "category", %w(abe mus hest), {}, { required: true, size: 2 }) - ) + expected = %() + + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), {}, { required: true, size: 2 })) + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), required: true, size: 2)) end def test_required_select_with_multiple_option - assert_dom_equal( - %(), - select("post", "category", %w(abe mus hest), {}, { required: true, multiple: true }) - ) + expected = %() + + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), {}, { required: true, multiple: true })) + assert_dom_equal(expected, select("post", "category", %w(abe mus hest), required: true, multiple: true)) end def test_select_with_integer