From b6541b8dcc6df9c92d946e1f76ec03f448d7fba4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 18 May 2007 05:29:22 +0000 Subject: select :include_blank option can be set to a string instead of true, which just uses an empty string. Closes #7664. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6763 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + .../lib/action_view/helpers/form_options_helper.rb | 31 +++++++++--- .../test/template/form_options_helper_test.rb | 56 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 6 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 7d7ba465f1..c6f5111aae 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* select :include_blank option can be set to a string instead of true, which just uses an empty string. #7664 [Wizard] + * Added url_for usage on render :location, which allows for record identification [DHH]. Example: render :xml => person, :status => :created, :location => person diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 6fc582ff65..22db5a918f 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -10,7 +10,9 @@ module ActionView # and time_zone_select methods take an options parameter, # a hash. # - # * :include_blank - set to true if the first option element of the select element is a blank. Useful if there is not a default value required for the select element. For example, + # * :include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element. + # + # For example, # # select("post", "category", Post::CATEGORIES, {:include_blank => true}) # @@ -22,15 +24,31 @@ module ActionView # # # + # Another common case is a select tag for an belongs_to-associated object. + # + # Example with @post.person_id => 2: + # + # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}) + # + # could become: + # + # + # # * :prompt - set to true or a prompt string. When the select element doesn't have a value yet, this prepends an option with a generic prompt -- "Please select" -- or the given prompt string. # - # Another common case is a select tag for an belongs_to-associated object. For example, + # Example: # - # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }) + # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'}) # # could become: # # - # + # # # # @@ -341,8 +359,9 @@ module ActionView private def add_options(option_tags, options, value = nil) - option_tags = "\n" + option_tags if options[:include_blank] - + if options[:include_blank] + option_tags = "\n" + option_tags + end if value.blank? && options[:prompt] ("\n") + option_tags else diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 5ca1d037df..3c0a96b0de 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -252,6 +252,15 @@ class FormOptionsHelperTest < Test::Unit::TestCase ) end + def test_select_with_blank_as_string + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :include_blank => 'None') + ) + end + def test_select_with_default_prompt @post = Post.new @post.category = "" @@ -360,6 +369,22 @@ class FormOptionsHelperTest < Test::Unit::TestCase ) end + def test_collection_select_with_blank_as_string_and_style + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + @post = Post.new + @post.author_name = "Babe" + + assert_dom_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px") + ) + end + def test_collection_select_with_multiple_option_appends_array_brackets @posts = [ Post.new(" went home", "", "To a little house", "shh!"), @@ -439,6 +464,20 @@ class FormOptionsHelperTest < Test::Unit::TestCase html end + def test_time_zone_select_with_blank_as_string + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone') + assert_dom_equal "", + html + end + def test_time_zone_select_with_style @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, {}, @@ -472,6 +511,23 @@ class FormOptionsHelperTest < Test::Unit::TestCase { :include_blank => true }, :style => "color: red") end + def test_time_zone_select_with_blank_as_string_and_style + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, + { :include_blank => 'No Zone' }, "style" => "color: red") + assert_dom_equal "", + html + assert_dom_equal html, time_zone_select("firm", "time_zone", nil, + { :include_blank => 'No Zone' }, :style => "color: red") + end + def test_time_zone_select_with_priority_zones @firm = Firm.new("D") zones = [ TimeZone.new("A"), TimeZone.new("D") ] -- cgit v1.2.3