public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added support for regexp matching of priority zones in time_zone_select [#195 
state:resolved]
Ernie Miller (author)
Sat Jun 28 19:27:32 -0700 2008
gbuesing (committer)
Sun Jun 29 11:37:07 -0700 2008
commit  d0092dc44d580f4179308c7394d9023098406f79
tree    d5ec77bbd249cd8bb1c1ae0efd877f179c656494
parent  029a7455846cde2654958358a6fe354f236ade35
...
150
151
152
153
 
 
154
155
156
...
164
165
166
 
 
167
168
169
...
292
293
294
295
 
 
296
297
298
...
311
312
313
 
 
 
314
315
316
...
150
151
152
 
153
154
155
156
157
...
165
166
167
168
169
170
171
172
...
295
296
297
 
298
299
300
301
302
...
315
316
317
318
319
320
321
322
323
0
@@ -150,7 +150,8 @@ module ActionView
0
       # You can also supply an array of TimeZone objects
0
       # as +priority_zones+, so that they will be listed above the rest of the
0
       # (long) list. (You can use TimeZone.us_zones as a convenience for
0
-      # obtaining a list of the US time zones.)
0
+      # obtaining a list of the US time zones, or a Regexp to select the zones
0
+      # of your choice)
0
       #
0
       # Finally, this method supports a <tt>:default</tt> option, which selects
0
       # a default TimeZone if the object's time zone is +nil+.
0
@@ -164,6 +165,8 @@ module ActionView
0
       #
0
       #   time_zone_select( "user", 'time_zone', [ TimeZone['Alaska'], TimeZone['Hawaii'] ])
0
       #
0
+      #   time_zone_select( "user", 'time_zone', /Australia/)
0
+      #
0
       #   time_zone_select( "user", "time_zone", TZInfo::Timezone.all.sort, :model => TZInfo::Timezone)
0
       def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {})
0
         InstanceTag.new(object, method, self, nil, options.delete(:object)).to_time_zone_select_tag(priority_zones, options, html_options)
0
@@ -292,7 +295,8 @@ module ActionView
0
       # selected option tag. You can also supply an array of TimeZone objects
0
       # as +priority_zones+, so that they will be listed above the rest of the
0
       # (long) list. (You can use TimeZone.us_zones as a convenience for
0
-      # obtaining a list of the US time zones.)
0
+      # obtaining a list of the US time zones, or a Regexp to select the zones
0
+      # of your choice)
0
       #
0
       # The +selected+ parameter must be either +nil+, or a string that names
0
       # a TimeZone.
0
@@ -311,6 +315,9 @@ module ActionView
0
         convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }
0
 
0
         if priority_zones
0
+    if priority_zones.is_a?(Regexp)
0
+            priority_zones = model.all.find_all {|z| z =~ priority_zones}
0
+    end
0
           zone_options += options_for_select(convert_zones[priority_zones], selected)
0
           zone_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
0
 
...
1300
1301
1302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1303
1304
1305
...
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
0
@@ -1300,6 +1300,24 @@ COUNTRIES
0
                  html
0
   end
0
 
0
+  uses_mocha "time_zone_select_with_priority_zones_as_regexp" do
0
+    def test_time_zone_select_with_priority_zones_as_regexp
0
+      @firm = Firm.new("D")
0
+      MockTimeZone.any_instance.stubs(:=~).returns(true,false,false,true,false)
0
+
0
+      html = time_zone_select("firm", "time_zone", /A|D/)
0
+      assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
0
+                   "<option value=\"A\">A</option>\n" +
0
+                   "<option value=\"D\" selected=\"selected\">D</option>" +
0
+                   "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
0
+                   "<option value=\"B\">B</option>\n" +
0
+                   "<option value=\"C\">C</option>\n" +
0
+                   "<option value=\"E\">E</option>" +
0
+                   "</select>",
0
+                   html
0
+    end
0
+  end
0
+
0
   def test_time_zone_select_with_default_time_zone_and_nil_value
0
      @firm = Firm.new()
0
      @firm.time_zone = nil
...
201
202
203
 
 
 
 
 
 
204
205
206
...
201
202
203
204
205
206
207
208
209
210
211
212
0
@@ -201,6 +201,12 @@ module ActiveSupport
0
       result
0
     end
0
 
0
+    # Compare #name and TZInfo identifier to a supplied regexp, returning true
0
+    # if a match is found.
0
+    def =~(re)
0
+      return true if name =~ re || MAPPING[name] =~ re
0
+    end
0
+
0
     # Returns a textual representation of this time zone.
0
     def to_s
0
       "(GMT#{formatted_offset}) #{name}"
...
250
251
252
 
 
 
 
 
 
 
253
254
255
...
250
251
252
253
254
255
256
257
258
259
260
261
262
0
@@ -250,6 +250,13 @@ class TimeZoneTest < Test::Unit::TestCase
0
     assert zone1 == zone1
0
   end
0
 
0
+  def test_zone_match
0
+    zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
0
+    assert zone =~ /Eastern/
0
+    assert zone =~ /New_York/
0
+    assert zone !~ /Nonexistent_Place/
0
+  end
0
+
0
   def test_to_s
0
     assert_equal "(GMT+03:00) Moscow", ActiveSupport::TimeZone['Moscow'].to_s
0
   end

Comments