Merge pull request #1722 from AndrewRadev/grouped-select

Make "select" helper handle nested collections
This commit is contained in:
José Valim 2011-07-11 10:25:04 -07:00
commit 1939fa3f16
2 changed files with 48 additions and 2 deletions

@ -105,7 +105,10 @@ module FormOptionsHelper
# Create a select tag and a series of contained option tags for the provided object and method. # Create a select tag and a series of contained option tags for the provided object and method.
# The option currently held by the object will be selected, provided that the object is available. # The option currently held by the object will be selected, provided that the object is available.
# See options_for_select for the required format of the choices parameter. #
# There are two possible formats for the choices parameter, corresponding to other helpers' output:
# * A flat collection: see options_for_select
# * A nested collection: see grouped_options_for_select
# #
# Example with @post.person_id => 1: # Example with @post.person_id => 1:
# select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true }) # select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })
@ -575,7 +578,14 @@ class InstanceTag #:nodoc:
def to_select_tag(choices, options, html_options) def to_select_tag(choices, options, html_options)
selected_value = options.has_key?(:selected) ? options[:selected] : value(object) selected_value = options.has_key?(:selected) ? options[:selected] : value(object)
select_content_tag(options_for_select(choices, :selected => selected_value, :disabled => options[:disabled]), options, html_options)
if !choices.empty? && choices.try(:first).try(:second).respond_to?(:each)
option_tags = grouped_options_for_select(choices, :selected => selected_value, :disabled => options[:disabled])
else
option_tags = options_for_select(choices, :selected => selected_value, :disabled => options[:disabled])
end
select_content_tag(option_tags, options, html_options)
end end
def to_collection_select_tag(collection, value_method, text_method, options, html_options) def to_collection_select_tag(collection, value_method, text_method, options, html_options)

@ -385,6 +385,42 @@ def test_select_without_multiple
) )
end end
def test_select_with_grouped_collection_as_nested_array
@post = Post.new
countries_by_continent = [
["<Africa>", [["<South Africa>", "<sa>"], ["Somalia", "so"]]],
["Europe", [["Denmark", "dk"], ["Ireland", "ie"]]],
]
assert_dom_equal(
[
%Q{<select id="post_origin" name="post[origin]"><optgroup label="&lt;Africa&gt;"><option value="&lt;sa&gt;">&lt;South Africa&gt;</option>},
%Q{<option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk">Denmark</option>},
%Q{<option value="ie">Ireland</option></optgroup></select>},
].join("\n"),
select("post", "origin", countries_by_continent)
)
end
def test_select_with_grouped_collection_as_hash
@post = Post.new
countries_by_continent = {
"<Africa>" => [["<South Africa>", "<sa>"], ["Somalia", "so"]],
"Europe" => [["Denmark", "dk"], ["Ireland", "ie"]],
}
assert_dom_equal(
[
%Q{<select id="post_origin" name="post[origin]"><optgroup label="&lt;Africa&gt;"><option value="&lt;sa&gt;">&lt;South Africa&gt;</option>},
%Q{<option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk">Denmark</option>},
%Q{<option value="ie">Ireland</option></optgroup></select>},
].join("\n"),
select("post", "origin", countries_by_continent)
)
end
def test_select_with_boolean_method def test_select_with_boolean_method
@post = Post.new @post = Post.new
@post.allow_comments = false @post.allow_comments = false