mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
select tags coerce the :selected option, options to strings before comparison [#5056 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
committed by
José Valim
parent
1091a6e9b7
commit
dbe5ae488e
@@ -298,17 +298,18 @@ module ActionView
|
||||
return container if String === container
|
||||
|
||||
container = container.to_a if Hash === container
|
||||
selected, disabled = extract_selected_and_disabled(selected)
|
||||
|
||||
options_for_select = container.map do |element|
|
||||
html_attributes = option_html_attributes(element)
|
||||
text, value = option_text_and_value(element)
|
||||
selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
|
||||
disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
|
||||
%(<option value="#{html_escape(value.to_s)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{html_escape(text.to_s)}</option>)
|
||||
selected, disabled = extract_selected_and_disabled(selected).map do | r |
|
||||
Array.wrap(r).map(&:to_s)
|
||||
end
|
||||
|
||||
options_for_select.join("\n").html_safe
|
||||
container.map do |element|
|
||||
html_attributes = option_html_attributes(element)
|
||||
text, value = option_text_and_value(element).map(&:to_s)
|
||||
selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
|
||||
disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
|
||||
%(<option value="#{html_escape(value)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{html_escape(text)}</option>)
|
||||
end.join("\n").html_safe
|
||||
|
||||
end
|
||||
|
||||
# Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning the
|
||||
@@ -528,10 +529,12 @@ module ActionView
|
||||
end
|
||||
|
||||
def extract_selected_and_disabled(selected)
|
||||
if selected.is_a?(Hash)
|
||||
[selected[:selected], selected[:disabled]]
|
||||
if selected.is_a?(Proc)
|
||||
[ selected, nil ]
|
||||
else
|
||||
[selected, nil]
|
||||
selected = Array.wrap(selected)
|
||||
options = selected.extract_options!.symbolize_keys
|
||||
[ options[:selected] || selected , options[:disabled] ]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -176,6 +176,68 @@ class FormOptionsHelperTest < ActionView::TestCase
|
||||
)
|
||||
end
|
||||
|
||||
def test_collection_options_with_preselected_value_as_string_and_option_value_is_integer
|
||||
albums = [ Album.new(1, "first","rap"), Album.new(2, "second","pop")]
|
||||
assert_dom_equal(
|
||||
%(<option selected="selected" value="1">rap</option>\n<option value="2">pop</option>),
|
||||
options_from_collection_for_select(albums, "id", "genre", :selected => "1")
|
||||
)
|
||||
end
|
||||
|
||||
def test_collection_options_with_preselected_value_as_integer_and_option_value_is_string
|
||||
albums = [ Album.new("1", "first","rap"), Album.new("2", "second","pop")]
|
||||
|
||||
assert_dom_equal(
|
||||
%(<option selected="selected" value="1">rap</option>\n<option value="2">pop</option>),
|
||||
options_from_collection_for_select(albums, "id", "genre", :selected => 1)
|
||||
)
|
||||
end
|
||||
|
||||
def test_collection_options_with_preselected_value_as_string_and_option_value_is_float
|
||||
albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
|
||||
|
||||
assert_dom_equal(
|
||||
%(<option value="1.0">rap</option>\n<option value="2.0" selected="selected">pop</option>),
|
||||
options_from_collection_for_select(albums, "id", "genre", :selected => "2.0")
|
||||
)
|
||||
end
|
||||
|
||||
def test_collection_options_with_preselected_value_as_nil
|
||||
albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
|
||||
|
||||
assert_dom_equal(
|
||||
%(<option value="1.0">rap</option>\n<option value="2.0">pop</option>),
|
||||
options_from_collection_for_select(albums, "id", "genre", :selected => nil)
|
||||
)
|
||||
end
|
||||
|
||||
def test_collection_options_with_disabled_value_as_nil
|
||||
albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
|
||||
|
||||
assert_dom_equal(
|
||||
%(<option value="1.0">rap</option>\n<option value="2.0">pop</option>),
|
||||
options_from_collection_for_select(albums, "id", "genre", :disabled => nil)
|
||||
)
|
||||
end
|
||||
|
||||
def test_collection_options_with_disabled_value_as_array
|
||||
albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop")]
|
||||
|
||||
assert_dom_equal(
|
||||
%(<option disabled="disabled" value="1.0">rap</option>\n<option disabled="disabled" value="2.0">pop</option>),
|
||||
options_from_collection_for_select(albums, "id", "genre", :disabled => ["1.0", 2.0])
|
||||
)
|
||||
end
|
||||
|
||||
def test_collection_options_with_preselected_values_as_string_array_and_option_value_is_float
|
||||
albums = [ Album.new(1.0, "first","rap"), Album.new(2.0, "second","pop"), Album.new(3.0, "third","country") ]
|
||||
|
||||
assert_dom_equal(
|
||||
%(<option value="1.0" selected="selected">rap</option>\n<option value="2.0">pop</option>\n<option value="3.0" selected="selected">country</option>),
|
||||
options_from_collection_for_select(albums, "id", "genre", ["1.0","3.0"])
|
||||
)
|
||||
end
|
||||
|
||||
def test_option_groups_from_collection_for_select
|
||||
assert_dom_equal(
|
||||
"<optgroup label=\"<Africa>\"><option value=\"<sa>\"><South Africa></option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>",
|
||||
|
||||
Reference in New Issue
Block a user