mirror of https://github.com/rails/rails
Avoid deprecation warning when raising
When calling + or since between two time objects we should avoid emitting two deprecation messages and avoiding any deprecation messages if the deprecated fallback raises an exception.
This commit is contained in:
parent
efd0e6cceb
commit
3df676878a
|
@ -1,6 +1,6 @@
|
|||
* Deprecate addition for `Time` instances with `ActiveSupport::TimeWithZone`.
|
||||
* Deprecate addition and since between two `Time` and `ActiveSupport::TimeWithZone`.
|
||||
|
||||
Previously adding time instances together such as `10.days.ago + 10.days.ago` produced a nonsensical future date. This behavior is deprecated and will be removed in Rails 8.0.
|
||||
Previously adding time instances together such as `10.days.ago + 10.days.ago` or `10.days.ago.since(10.days.ago)` produced a nonsensical future date. This behavior is deprecated and will be removed in Rails 8.1.
|
||||
|
||||
*Nick Schwaderer*
|
||||
|
||||
|
|
|
@ -218,11 +218,12 @@ class Time
|
|||
def since(seconds)
|
||||
self + seconds
|
||||
rescue TypeError
|
||||
result = to_datetime.since(seconds)
|
||||
ActiveSupport.deprecator.warn(
|
||||
"Passing an instance of #{seconds.class} to #{self.class}#since is deprecated. This behavior will raise " \
|
||||
"a `TypeError` in Rails 8.1."
|
||||
)
|
||||
to_datetime.since(seconds)
|
||||
result
|
||||
end
|
||||
alias :in :since
|
||||
|
||||
|
|
|
@ -303,11 +303,11 @@ module ActiveSupport
|
|||
begin
|
||||
result = utc + other
|
||||
rescue TypeError
|
||||
result = utc.to_datetime.since(other)
|
||||
ActiveSupport.deprecator.warn(
|
||||
"Adding an instance of #{other.class} to an instance of #{self.class} is deprecated. This behavior will raise " \
|
||||
"a `TypeError` in Rails 8.1."
|
||||
)
|
||||
result = utc.since(other)
|
||||
result.in_time_zone(time_zone)
|
||||
end
|
||||
result.in_time_zone(time_zone)
|
||||
|
|
|
@ -407,6 +407,15 @@ class TimeWithZoneTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_plus_with_invalid_argument
|
||||
twz = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), @time_zone)
|
||||
assert_not_deprecated(ActiveSupport.deprecator) do
|
||||
assert_raises TypeError do
|
||||
twz + Object.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_plus_with_duration
|
||||
assert_equal Time.utc(2000, 1, 5, 19, 0, 0), (@twz + 5.days).time
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue