From ad150a73c424b9f942cc32d98c67324785b6d041 Mon Sep 17 00:00:00 2001 From: Daniel Lopes Date: Fri, 15 Apr 2011 22:35:27 -0300 Subject: [PATCH] fix select_tag to have the same behavior of select --- .../lib/action_view/helpers/form_tag_helper.rb | 22 ++++++++++++++----- actionpack/test/template/form_tag_helper_test.rb | 10 +++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 49aa434..9e0f8f3 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -74,6 +74,8 @@ module ActionView # ==== Options # * :multiple - If set to true the selection will allow multiple choices. # * :disabled - If set to true, the user will not be able to use this input. + # * :include_blank - If set to true, an empty option will be create + # * :prompt - Create a prompt option with blank value and the text asking user to select something # * Any other key creates standard HTML attributes for the tag. # # ==== Examples @@ -99,18 +101,26 @@ module ActionView # # => # + # select_tag "people", options_from_collection_for_select(@people, "id", "name"), :include_blank => true + # # => + # + # select_tag "people", options_from_collection_for_select(@people, "id", "name"), :prompt => "Select something" + # # => + # # select_tag "destination", "", :disabled => true # # => def select_tag(name, option_tags = nil, options = {}) html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name - if blank = options.delete(:include_blank) - if blank.kind_of?(String) - option_tags = "".html_safe + option_tags - else - option_tags = "".html_safe + option_tags - end + + if options.delete(:include_blank) + option_tags = "".html_safe + option_tags + end + + if prompt = options.delete(:prompt) + option_tags = "".html_safe + option_tags end + content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys) end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 656fa03..f95308b 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -200,12 +200,18 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal expected, actual end - def test_select_tag_with_include_blank_with_string - actual = select_tag "places", "".html_safe, :include_blank => "string" + def test_select_tag_with_prompt + actual = select_tag "places", "".html_safe, :prompt => "string" expected = %() assert_dom_equal expected, actual end + def test_select_tag_with_prompt_and_include_blank + actual = select_tag "places", "".html_safe, :prompt => "string", :include_blank => true + expected = %() + assert_dom_equal expected, actual + end + def test_text_area_tag_size_string actual = text_area_tag "body", "hello world", "size" => "20x40" expected = %() -- 1.7.3.4