Skip to content

Commit

Permalink
Added support for regexp matching of priority zones in time_zone_sele…
Browse files Browse the repository at this point in the history
…ct [#195 state:resolved]
  • Loading branch information
Ernie Miller authored and gbuesing committed Jun 29, 2008
1 parent 029a745 commit d0092dc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
11 changes: 9 additions & 2 deletions actionpack/lib/action_view/helpers/form_options_helper.rb
Expand Up @@ -150,7 +150,8 @@ def country_select(object, method, priority_countries = nil, options = {}, html_
# You can also supply an array of TimeZone objects
# as +priority_zones+, so that they will be listed above the rest of the
# (long) list. (You can use TimeZone.us_zones as a convenience for
# obtaining a list of the US time zones.)
# obtaining a list of the US time zones, or a Regexp to select the zones
# of your choice)
#
# Finally, this method supports a <tt>:default</tt> option, which selects
# a default TimeZone if the object's time zone is +nil+.
Expand All @@ -164,6 +165,8 @@ def country_select(object, method, priority_countries = nil, options = {}, html_
#
# time_zone_select( "user", 'time_zone', [ TimeZone['Alaska'], TimeZone['Hawaii'] ])
#
# time_zone_select( "user", 'time_zone', /Australia/)
#
# time_zone_select( "user", "time_zone", TZInfo::Timezone.all.sort, :model => TZInfo::Timezone)
def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {})
InstanceTag.new(object, method, self, nil, options.delete(:object)).to_time_zone_select_tag(priority_zones, options, html_options)
Expand Down Expand Up @@ -292,7 +295,8 @@ def country_options_for_select(selected = nil, priority_countries = nil)
# selected option tag. You can also supply an array of TimeZone objects
# as +priority_zones+, so that they will be listed above the rest of the
# (long) list. (You can use TimeZone.us_zones as a convenience for
# obtaining a list of the US time zones.)
# obtaining a list of the US time zones, or a Regexp to select the zones
# of your choice)
#
# The +selected+ parameter must be either +nil+, or a string that names
# a TimeZone.
Expand All @@ -311,6 +315,9 @@ def time_zone_options_for_select(selected = nil, priority_zones = nil, model = :
convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }

if priority_zones
if priority_zones.is_a?(Regexp)
priority_zones = model.all.find_all {|z| z =~ priority_zones}
end
zone_options += options_for_select(convert_zones[priority_zones], selected)
zone_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"

Expand Down
18 changes: 18 additions & 0 deletions actionpack/test/template/form_options_helper_test.rb
Expand Up @@ -1300,6 +1300,24 @@ def test_time_zone_select_with_priority_zones
html
end

uses_mocha "time_zone_select_with_priority_zones_as_regexp" do
def test_time_zone_select_with_priority_zones_as_regexp
@firm = Firm.new("D")
MockTimeZone.any_instance.stubs(:=~).returns(true,false,false,true,false)

html = time_zone_select("firm", "time_zone", /A|D/)
assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
"<option value=\"A\">A</option>\n" +
"<option value=\"D\" selected=\"selected\">D</option>" +
"<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
"<option value=\"B\">B</option>\n" +
"<option value=\"C\">C</option>\n" +
"<option value=\"E\">E</option>" +
"</select>",
html
end
end

def test_time_zone_select_with_default_time_zone_and_nil_value
@firm = Firm.new()
@firm.time_zone = nil
Expand Down
6 changes: 6 additions & 0 deletions activesupport/lib/active_support/values/time_zone.rb
Expand Up @@ -201,6 +201,12 @@ def <=>(zone)
result
end

# Compare #name and TZInfo identifier to a supplied regexp, returning true
# if a match is found.
def =~(re)
return true if name =~ re || MAPPING[name] =~ re
end

# Returns a textual representation of this time zone.
def to_s
"(GMT#{formatted_offset}) #{name}"
Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/time_zone_test.rb
Expand Up @@ -250,6 +250,13 @@ def test_zone_compare
assert zone1 == zone1
end

def test_zone_match
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
assert zone =~ /Eastern/
assert zone =~ /New_York/
assert zone !~ /Nonexistent_Place/
end

def test_to_s
assert_equal "(GMT+03:00) Moscow", ActiveSupport::TimeZone['Moscow'].to_s
end
Expand Down

0 comments on commit d0092dc

Please sign in to comment.