Skip to content

Commit

Permalink
check zone presence in find_zone
Browse files Browse the repository at this point in the history
  • Loading branch information
matiassalles99 committed Nov 26, 2023
1 parent 9f18c04 commit 504d350
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions activesupport/lib/active_support/core_ext/time/zones.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ def use_zone(time_zone)
# Time.find_zone! "EST" # => #<ActiveSupport::TimeZone @name="EST" ...>
# Time.find_zone! -5.hours # => #<ActiveSupport::TimeZone @name="Bogota" ...>
# Time.find_zone! nil # => nil
# Time.find_zone! false # => false
# Time.find_zone! false # => nil
# Time.find_zone! "" # => nil
# Time.find_zone! "NOT-A-TIMEZONE" # => ArgumentError: Invalid Timezone: NOT-A-TIMEZONE
def find_zone!(time_zone)
return time_zone unless time_zone
return unless time_zone.present?

ActiveSupport::TimeZone[time_zone] || raise(ArgumentError, "Invalid Timezone: #{time_zone}")
end
Expand Down
5 changes: 3 additions & 2 deletions activesupport/test/core_ext/time_with_zone_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1255,9 +1255,10 @@ def test_find_zone_with_bang_raises_if_time_zone_can_not_be_found
assert_match "invalid argument to TimeZone[]", error.message
end

def test_find_zone_with_bang_doesnt_raises_with_nil_and_false
def test_find_zone_with_bang_doesnt_raises_with_non_present_argument
assert_nil Time.find_zone!(nil)
assert_equal false, Time.find_zone!(false)
assert_nil Time.find_zone!(false)
assert_nil Time.find_zone!("")
end

def test_time_zone_setter_with_find_zone_without_bang
Expand Down

0 comments on commit 504d350

Please sign in to comment.