mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
f7bb3ddce8
commit
b6541b8dcc
3 changed files with 84 additions and 7 deletions
|
@ -1,5 +1,7 @@
|
||||||
*SVN*
|
*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:
|
* Added url_for usage on render :location, which allows for record identification [DHH]. Example:
|
||||||
|
|
||||||
render :xml => person, :status => :created, :location => person
|
render :xml => person, :status => :created, :location => person
|
||||||
|
|
|
@ -10,7 +10,9 @@ module ActionView
|
||||||
# and <tt>time_zone_select</tt> methods take an <tt>options</tt> parameter,
|
# and <tt>time_zone_select</tt> methods take an <tt>options</tt> parameter,
|
||||||
# a hash.
|
# a hash.
|
||||||
#
|
#
|
||||||
# * <tt>:include_blank</tt> - 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,
|
# * <tt>:include_blank</tt> - 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})
|
# select("post", "category", Post::CATEGORIES, {:include_blank => true})
|
||||||
#
|
#
|
||||||
|
@ -22,15 +24,31 @@ module ActionView
|
||||||
# <option>poem</option>
|
# <option>poem</option>
|
||||||
# </select>
|
# </select>
|
||||||
#
|
#
|
||||||
# * <tt>:prompt</tt> - 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 <tt>belongs_to</tt>-associated object.
|
||||||
#
|
#
|
||||||
# Another common case is a select tag for an <tt>belongs_to</tt>-associated object. For example,
|
# Example with @post.person_id => 2:
|
||||||
#
|
#
|
||||||
# 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 ] }, {:include_blank => 'None'})
|
||||||
#
|
#
|
||||||
# could become:
|
# could become:
|
||||||
#
|
#
|
||||||
# <select name="post[person_id]">
|
# <select name="post[person_id]">
|
||||||
|
# <option value="">None</option>
|
||||||
|
# <option value="1">David</option>
|
||||||
|
# <option value="2" selected="selected">Sam</option>
|
||||||
|
# <option value="3">Tobias</option>
|
||||||
|
# </select>
|
||||||
|
#
|
||||||
|
# * <tt>:prompt</tt> - 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.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
|
||||||
|
#
|
||||||
|
# could become:
|
||||||
|
#
|
||||||
|
# <select name="post[person_id]">
|
||||||
|
# <option value="">Select Person</option>
|
||||||
# <option value="1">David</option>
|
# <option value="1">David</option>
|
||||||
# <option value="2">Sam</option>
|
# <option value="2">Sam</option>
|
||||||
# <option value="3">Tobias</option>
|
# <option value="3">Tobias</option>
|
||||||
|
@ -48,7 +66,7 @@ module ActionView
|
||||||
# could become:
|
# could become:
|
||||||
#
|
#
|
||||||
# <select name="post[person_id]">
|
# <select name="post[person_id]">
|
||||||
# <option></option>
|
# <option value=""></option>
|
||||||
# <option value="1" selected="selected">David</option>
|
# <option value="1" selected="selected">David</option>
|
||||||
# <option value="2">Sam</option>
|
# <option value="2">Sam</option>
|
||||||
# <option value="3">Tobias</option>
|
# <option value="3">Tobias</option>
|
||||||
|
@ -341,8 +359,9 @@ module ActionView
|
||||||
|
|
||||||
private
|
private
|
||||||
def add_options(option_tags, options, value = nil)
|
def add_options(option_tags, options, value = nil)
|
||||||
option_tags = "<option value=\"\"></option>\n" + option_tags if options[:include_blank]
|
if options[:include_blank]
|
||||||
|
option_tags = "<option value=\"\">#{options[:include_blank] if options[:include_blank].kind_of?(String)}</option>\n" + option_tags
|
||||||
|
end
|
||||||
if value.blank? && options[:prompt]
|
if value.blank? && options[:prompt]
|
||||||
("<option value=\"\">#{options[:prompt].kind_of?(String) ? options[:prompt] : 'Please select'}</option>\n") + option_tags
|
("<option value=\"\">#{options[:prompt].kind_of?(String) ? options[:prompt] : 'Please select'}</option>\n") + option_tags
|
||||||
else
|
else
|
||||||
|
|
|
@ -252,6 +252,15 @@ class FormOptionsHelperTest < Test::Unit::TestCase
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_select_with_blank_as_string
|
||||||
|
@post = Post.new
|
||||||
|
@post.category = "<mus>"
|
||||||
|
assert_dom_equal(
|
||||||
|
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">None</option>\n<option value=\"abe\">abe</option>\n<option value=\"<mus>\" selected=\"selected\"><mus></option>\n<option value=\"hest\">hest</option></select>",
|
||||||
|
select("post", "category", %w( abe <mus> hest), :include_blank => 'None')
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def test_select_with_default_prompt
|
def test_select_with_default_prompt
|
||||||
@post = Post.new
|
@post = Post.new
|
||||||
@post.category = ""
|
@post.category = ""
|
||||||
|
@ -360,6 +369,22 @@ class FormOptionsHelperTest < Test::Unit::TestCase
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_collection_select_with_blank_as_string_and_style
|
||||||
|
@posts = [
|
||||||
|
Post.new("<Abe> went home", "<Abe>", "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(
|
||||||
|
"<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\">No Selection</option>\n<option value=\"<Abe>\"><Abe></option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
|
||||||
|
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
|
def test_collection_select_with_multiple_option_appends_array_brackets
|
||||||
@posts = [
|
@posts = [
|
||||||
Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
|
Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
|
||||||
|
@ -439,6 +464,20 @@ class FormOptionsHelperTest < Test::Unit::TestCase
|
||||||
html
|
html
|
||||||
end
|
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 "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
|
||||||
|
"<option value=\"\">No Zone</option>\n" +
|
||||||
|
"<option value=\"A\">A</option>\n" +
|
||||||
|
"<option value=\"B\">B</option>\n" +
|
||||||
|
"<option value=\"C\">C</option>\n" +
|
||||||
|
"<option value=\"D\" selected=\"selected\">D</option>\n" +
|
||||||
|
"<option value=\"E\">E</option>" +
|
||||||
|
"</select>",
|
||||||
|
html
|
||||||
|
end
|
||||||
|
|
||||||
def test_time_zone_select_with_style
|
def test_time_zone_select_with_style
|
||||||
@firm = Firm.new("D")
|
@firm = Firm.new("D")
|
||||||
html = time_zone_select("firm", "time_zone", nil, {},
|
html = time_zone_select("firm", "time_zone", nil, {},
|
||||||
|
@ -472,6 +511,23 @@ class FormOptionsHelperTest < Test::Unit::TestCase
|
||||||
{ :include_blank => true }, :style => "color: red")
|
{ :include_blank => true }, :style => "color: red")
|
||||||
end
|
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 "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
|
||||||
|
"<option value=\"\">No Zone</option>\n" +
|
||||||
|
"<option value=\"A\">A</option>\n" +
|
||||||
|
"<option value=\"B\">B</option>\n" +
|
||||||
|
"<option value=\"C\">C</option>\n" +
|
||||||
|
"<option value=\"D\" selected=\"selected\">D</option>\n" +
|
||||||
|
"<option value=\"E\">E</option>" +
|
||||||
|
"</select>",
|
||||||
|
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
|
def test_time_zone_select_with_priority_zones
|
||||||
@firm = Firm.new("D")
|
@firm = Firm.new("D")
|
||||||
zones = [ TimeZone.new("A"), TimeZone.new("D") ]
|
zones = [ TimeZone.new("A"), TimeZone.new("D") ]
|
||||||
|
|
Loading…
Reference in a new issue