mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow proc for value/text method in collection_select
And options_from_collection_for_select as well. [Carlos Antonio da Silva + Rafael Mendonça França]
This commit is contained in:
parent
b161956061
commit
9035324367
3 changed files with 48 additions and 16 deletions
|
@ -360,7 +360,7 @@ module ActionView
|
||||||
# should produce the desired results.
|
# should produce the desired results.
|
||||||
def options_from_collection_for_select(collection, value_method, text_method, selected = nil)
|
def options_from_collection_for_select(collection, value_method, text_method, selected = nil)
|
||||||
options = collection.map do |element|
|
options = collection.map do |element|
|
||||||
[element.send(text_method), element.send(value_method)]
|
[value_for_collection(element, text_method), value_for_collection(element, value_method)]
|
||||||
end
|
end
|
||||||
selected, disabled = extract_selected_and_disabled(selected)
|
selected, disabled = extract_selected_and_disabled(selected)
|
||||||
select_deselect = {
|
select_deselect = {
|
||||||
|
@ -622,7 +622,7 @@ module ActionView
|
||||||
[selected, nil]
|
[selected, nil]
|
||||||
else
|
else
|
||||||
selected = Array.wrap(selected)
|
selected = Array.wrap(selected)
|
||||||
options = selected.extract_options!.symbolize_keys
|
options = selected.extract_options!.symbolize_keys
|
||||||
selected_items = options.fetch(:selected, selected)
|
selected_items = options.fetch(:selected, selected)
|
||||||
[selected_items, options[:disabled]]
|
[selected_items, options[:disabled]]
|
||||||
end
|
end
|
||||||
|
@ -637,6 +637,10 @@ module ActionView
|
||||||
selected
|
selected
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def value_for_collection(item, value)
|
||||||
|
value.respond_to?(:call) ? value.call(item) : item.send(value)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class FormBuilder
|
class FormBuilder
|
||||||
|
|
|
@ -72,10 +72,6 @@ module ActionView
|
||||||
yield value, text, default_html_options
|
yield value, text, default_html_options
|
||||||
end.join.html_safe
|
end.join.html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
def value_for_collection(item, value) #:nodoc:
|
|
||||||
value.respond_to?(:call) ? value.call(item) : item.send(value)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -87,6 +87,20 @@ class FormOptionsHelperTest < ActionView::TestCase
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_collection_options_with_proc_for_value_method
|
||||||
|
assert_dom_equal(
|
||||||
|
"<option value=\"<Abe>\"><Abe> went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
|
||||||
|
options_from_collection_for_select(dummy_posts, lambda { |p| p.author_name }, "title")
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_collection_options_with_proc_for_text_method
|
||||||
|
assert_dom_equal(
|
||||||
|
"<option value=\"<Abe>\"><Abe> went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
|
||||||
|
options_from_collection_for_select(dummy_posts, "author_name", lambda { |p| p.title })
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def test_string_options_for_select
|
def test_string_options_for_select
|
||||||
options = "<option value=\"Denmark\">Denmark</option><option value=\"USA\">USA</option><option value=\"Sweden\">Sweden</option>"
|
options = "<option value=\"Denmark\">Denmark</option><option value=\"USA\">USA</option><option value=\"Sweden\">Sweden</option>"
|
||||||
assert_dom_equal(
|
assert_dom_equal(
|
||||||
|
@ -790,7 +804,25 @@ class FormOptionsHelperTest < ActionView::TestCase
|
||||||
assert_dom_equal(
|
assert_dom_equal(
|
||||||
"<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"<Abe>\"><Abe></option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe</option></select>",
|
"<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"<Abe>\"><Abe></option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe</option></select>",
|
||||||
collection_select("post", "author_name", dummy_posts, "author_name", "author_name", :disabled => 'Cabe')
|
collection_select("post", "author_name", dummy_posts, "author_name", "author_name", :disabled => 'Cabe')
|
||||||
)
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_collection_select_with_proc_for_value_method
|
||||||
|
@post = Post.new
|
||||||
|
|
||||||
|
assert_dom_equal(
|
||||||
|
"<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"<Abe>\"><Abe> went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option></select>",
|
||||||
|
collection_select("post", "author_name", dummy_posts, lambda { |p| p.author_name }, "title")
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_collection_select_with_proc_for_text_method
|
||||||
|
@post = Post.new
|
||||||
|
|
||||||
|
assert_dom_equal(
|
||||||
|
"<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"<Abe>\"><Abe> went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option></select>",
|
||||||
|
collection_select("post", "author_name", dummy_posts, "author_name", lambda { |p| p.title })
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_time_zone_select
|
def test_time_zone_select
|
||||||
|
@ -1084,14 +1116,14 @@ class FormOptionsHelperTest < ActionView::TestCase
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def dummy_posts
|
def dummy_posts
|
||||||
[ Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
|
[ Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
|
||||||
Post.new("Babe went home", "Babe", "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.new("Cabe went home", "Cabe", "To a little house", "shh!") ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def dummy_continents
|
def dummy_continents
|
||||||
[ Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ),
|
[ Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")]),
|
||||||
Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) ]
|
Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")]) ]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue