diff options
author | Akira Matsuda <ronnie@dio.jp> | 2019-07-29 14:05:18 +0900 |
---|---|---|
committer | Akira Matsuda <ronnie@dio.jp> | 2019-07-29 14:17:36 +0900 |
commit | 8f90ac7827787a84b38055e1011ef9aedd89fe91 (patch) | |
tree | da113b79ba98d60e5c4ee0705d03044254c22c84 | |
parent | 381e8cb67abec7d2ceb812fde587d924667de122 (diff) | |
download | rails-8f90ac7827787a84b38055e1011ef9aedd89fe91.tar.gz rails-8f90ac7827787a84b38055e1011ef9aedd89fe91.tar.bz2 rails-8f90ac7827787a84b38055e1011ef9aedd89fe91.zip |
Add AS::TimeZone#match?
5 files changed, 19 insertions, 3 deletions
diff --git a/actionview/lib/action_view/helpers/form_options_helper.rb b/actionview/lib/action_view/helpers/form_options_helper.rb index ad19426fff..cf8f7de931 100644 --- a/actionview/lib/action_view/helpers/form_options_helper.rb +++ b/actionview/lib/action_view/helpers/form_options_helper.rb @@ -569,7 +569,7 @@ module ActionView # be obtained in Active Record as a value object). The +model+ parameter # must respond to +all+ and return an array of objects that represent time # zones; each object must respond to +name+. If a Regexp is given it will - # attempt to match the zones using the <code>=~<code> operator. + # attempt to match the zones using <code>match?</code> method. # # NOTE: Only the option tags are returned, you have to wrap this call in # a regular HTML select tag. @@ -581,7 +581,7 @@ module ActionView if priority_zones if priority_zones.is_a?(Regexp) - priority_zones = zones.select { |z| z =~ priority_zones } + priority_zones = zones.select { |z| z.match?(priority_zones) } end zone_options.safe_concat options_for_select(convert_zones[priority_zones], selected) diff --git a/actionview/test/template/form_options_helper_test.rb b/actionview/test/template/form_options_helper_test.rb index d7a7b95ab3..273646b077 100644 --- a/actionview/test/template/form_options_helper_test.rb +++ b/actionview/test/template/form_options_helper_test.rb @@ -47,6 +47,7 @@ class FormOptionsHelperTest < ActionView::TestCase FakeZone = Struct.new(:name) do def to_s; name; end def =~(_re); end + def match?(_re); end end module ClassMethods @@ -1266,6 +1267,7 @@ class FormOptionsHelperTest < ActionView::TestCase @fake_timezones.each do |tz| def tz.=~(re); %(A D).include?(name) end + def tz.match?(re); %(A D).include?(name) end end html = time_zone_select("firm", "time_zone", /A|D/) diff --git a/activemodel/lib/active_model/type/helpers/timezone.rb b/activemodel/lib/active_model/type/helpers/timezone.rb index cf87b9715b..b0477aec32 100644 --- a/activemodel/lib/active_model/type/helpers/timezone.rb +++ b/activemodel/lib/active_model/type/helpers/timezone.rb @@ -7,7 +7,7 @@ module ActiveModel module Helpers # :nodoc: all module Timezone def is_utc? - ::Time.zone_default.nil? || ::Time.zone_default =~ "UTC" + ::Time.zone_default.nil? || ::Time.zone_default.match?("UTC") end def default_timezone diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index d9e033e23b..90830b89bd 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -334,6 +334,13 @@ module ActiveSupport re === name || re === MAPPING[name] end + # Compare #name and TZInfo identifier to a supplied regexp, returning +true+ + # if a match is found. + def match?(re) + (re == name) || (re == MAPPING[name]) || + ((Regexp === re) && (re.match?(name) || re.match?(MAPPING[name]))) + end + # Returns a textual representation of this time zone. def to_s "(GMT#{formatted_offset}) #{name}" diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index 0c3a10995c..eccacce32a 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -717,6 +717,13 @@ class TimeZoneTest < ActiveSupport::TestCase assert_no_match zone, /Nonexistent_Place/ end + def test_zone_match? + zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"] + assert zone.match?(/Eastern/) + assert zone.match?(/New_York/) + assert_not zone.match?(/Nonexistent_Place/) + end + def test_to_s assert_equal "(GMT+05:30) New Delhi", ActiveSupport::TimeZone["New Delhi"].to_s end |