From 85ced7c4153472b6d0e51d4bf459744a996c4ddd Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 24 Jun 2008 18:27:20 -0400 Subject: [PATCH] Added support for regexp selection of priority zones for time_zone_select --- .../lib/action_view/helpers/form_options_helper.rb | 8 +++- .../test/template/form_options_helper_test.rb | 45 ++++++++++++++++++++ .../lib/active_support/values/time_zone.rb | 10 ++++ 3 files changed, 62 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index b3f8e63..05bb5bb 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -150,7 +150,8 @@ module ActionView # 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 :default option, which selects # a default TimeZone if the object's time zone is +nil+. @@ -164,6 +165,8 @@ module ActionView # # 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) @@ -412,6 +415,9 @@ module ActionView end def to_time_zone_select_tag(priority_zones, options, html_options) + if priority_zones.is_a?(Regexp) + priority_zones = ActiveSupport::TimeZone =~ priority_zones + end html_options = html_options.stringify_keys add_default_name_and_id(html_options) value = value(object) diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 3f89a5e..bf91211 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -3,6 +3,14 @@ require 'abstract_unit' class MockTimeZone attr_reader :name + MAPPING = { + "A" => "A/A", + "B" => "B/B", + "C" => "C/C", + "D" => "D/D", + "E" => "E/E" + } + def initialize( name ) @name = name end @@ -18,6 +26,28 @@ class MockTimeZone def to_s @name end + + ZONES = [] + ZONES_MAP = {} + [ "A", "B", "C", "D", "E" ].each do |place| + zone = new place + ZONES << zone + ZONES_MAP[place] = zone + end + + class << self + def [](arg) + ZONES_MAP[arg] + end + + def =~(re) + zones = [] + MAPPING.select {|k,v| re.match(k) || re.match(v)}.each do |name, value| + zones << self[name] + end + zones + end + end end class FormOptionsHelperTest < ActionView::TestCase @@ -1300,6 +1330,21 @@ COUNTRIES html end + def test_time_zone_select_with_priority_zones_as_regexp + @firm = Firm.new("D") + zones = /A|D/ + html = time_zone_select("firm", "time_zone", zones ) + assert_dom_equal "", + html + end + def test_time_zone_select_with_default_time_zone_and_nil_value @firm = Firm.new() @firm.time_zone = nil diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 5b2d42a..5d0827d 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -386,6 +386,16 @@ module ActiveSupport end end + # Search MAPPING for keys or values matching a supplied regexp, returning + # an array with matching zones. + def =~(re) + zones = [] + MAPPING.select {|k,v| re.match(k) || re.match(v)}.each do |name, value| + zones << self[name] + end + zones + end + # A convenience method for returning a collection of TimeZone objects # for time zones in the USA. def us_zones -- 1.5.6.1062.g7b17c