From e8e08d69c203050634c427d7978d8e1af36455b0 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Mon, 23 Sep 2013 17:48:23 +0300 Subject: [PATCH] Fix some edge cases for AV `select` helper with `:selected` option --- actionview/CHANGELOG.md | 4 ++++ actionview/lib/action_view/helpers/tags/base.rb | 3 ++- .../test/template/form_options_helper_test.rb | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 95136b5e0e..61a6233430 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,7 @@ +* Fix some edge cases for AV `select` helper with `:selected` option + + *Bogdan Gusiev* + * Ability to pass block to `select` helper <%= select(report, "campaign_ids") do %> diff --git a/actionview/lib/action_view/helpers/tags/base.rb b/actionview/lib/action_view/helpers/tags/base.rb index 3fe3f4e9df..8607da301c 100644 --- a/actionview/lib/action_view/helpers/tags/base.rb +++ b/actionview/lib/action_view/helpers/tags/base.rb @@ -119,7 +119,8 @@ def select_content_tag(option_tags, options, html_options) html_options = html_options.stringify_keys add_default_name_and_id(html_options) options[:include_blank] ||= true unless options[:prompt] || select_not_required?(html_options) - select = content_tag("select", add_options(option_tags, options, value(object)), html_options) + value = options.fetch(:selected) { value(object) } + select = content_tag("select", add_options(option_tags, options, value), html_options) if html_options["multiple"] && options.fetch(:include_hidden, true) tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select diff --git a/actionview/test/template/form_options_helper_test.rb b/actionview/test/template/form_options_helper_test.rb index 801f1607aa..50e9d132a7 100644 --- a/actionview/test/template/form_options_helper_test.rb +++ b/actionview/test/template/form_options_helper_test.rb @@ -798,6 +798,22 @@ def test_select_with_disabled_value ) end + def test_select_not_existing_method_with_selected_value + @post = Post.new + assert_dom_equal( + "", + select("post", "locale", %w( en ru ), :selected => 'ru') + ) + end + + def test_select_with_prompt_and_selected_value + @post = Post.new + assert_dom_equal( + "", + select("post", "category", %w( one two ), :selected => 'two', :prompt => true) + ) + end + def test_select_with_disabled_array @post = Post.new @post.category = "" -- GitLab