1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Revert grep to select since they are not the same

A previous commit swapped out a call to select for a call to grep in
time_zone_options_for_select. This behavior actually causes the
regexp priority option to stop working.

ActiveSupport::TimeZone overrides the =~ operator which is what the
select block was using previously. Enumerable#grep
checks pattern === element and in this case that would be /US/ ===
ActiveSupport::TimeZone which does not work because
ActiveSupport::TimeZone does not supply an implicit converting to_str
method, only an explicit to_s method.

It would be impossible to provide a to_str method that behaves
identically to the =~ method provided on ActiveSupport::TimeZone
so the only option is to revert back to using select with =~.
This commit is contained in:
Brian McManus 2013-03-27 14:03:49 -07:00
parent 57fbcc5247
commit 1cc991bef8
2 changed files with 3 additions and 4 deletions

View file

@ -565,7 +565,7 @@ module ActionView
if priority_zones if priority_zones
if priority_zones.is_a?(Regexp) if priority_zones.is_a?(Regexp)
priority_zones = zones.grep(priority_zones) priority_zones = zones.select { |z| z =~ priority_zones }
end end
zone_options.safe_concat options_for_select(convert_zones[priority_zones], selected) zone_options.safe_concat options_for_select(convert_zones[priority_zones], selected)

View file

@ -1095,12 +1095,11 @@ class FormOptionsHelperTest < ActionView::TestCase
def test_time_zone_select_with_priority_zones_as_regexp def test_time_zone_select_with_priority_zones_as_regexp
@firm = Firm.new("D") @firm = Firm.new("D")
priority_zones = /A|D/
@fake_timezones.each_with_index do |tz, i| @fake_timezones.each_with_index do |tz, i|
priority_zones.stubs(:===).with(tz).returns(i.zero? || i == 3) tz.stubs(:=~).returns(i.zero? || i == 3)
end end
html = time_zone_select("firm", "time_zone", priority_zones) html = time_zone_select("firm", "time_zone", /A|D/)
assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" + assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
"<option value=\"A\">A</option>\n" + "<option value=\"A\">A</option>\n" +
"<option value=\"D\" selected=\"selected\">D</option>" + "<option value=\"D\" selected=\"selected\">D</option>" +