Re-roll deprecation of to_time_preserves_timezone

We realised the previous deprecation hadn't been warning for all users.
This commit is contained in:
Matthew Draper 2024-06-03 00:57:09 +09:30
parent 43fcdfa4d0
commit 595c3ccc47
4 changed files with 58 additions and 2 deletions

View File

@ -117,7 +117,7 @@ module ActiveSupport
def self.to_time_preserves_timezone=(value)
unless value
ActiveSupport.deprecator.warn(
"Support for the pre-Ruby 2.4 behavior of to_time has been deprecated and will be removed in Rails 7.2."
"Support for the pre-Ruby 2.4 behavior of to_time has been deprecated and will be removed in Rails 8.0."
)
end

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/module/redefine_method"
module DateAndTime
module Compatibility
@ -11,7 +12,33 @@ module DateAndTime
# of the receiver. For backwards compatibility we're overriding
# this behavior, but new apps will have an initializer that sets
# this to true, because the new behavior is preferred.
mattr_accessor :preserve_timezone, instance_writer: false, default: false
mattr_accessor :preserve_timezone, instance_accessor: false, default: nil
singleton_class.silence_redefinition_of_method :preserve_timezone
#--
# This re-implements the behaviour of the mattr_reader, instead
# of prepending on to it, to avoid overcomplicating a module that
# is in turn included in several places. This will all go away in
# Rails 8.0 anyway.
def self.preserve_timezone # :nodoc:
if @@preserve_timezone.nil?
# Only warn once, the first time the value is used (which should
# be the first time #to_time is called).
ActiveSupport.deprecator.warn(
"to_time will always preserve the timezone offset of the receiver in Rails 8.0. " \
"To opt in to the new behavior, set `ActiveSupport.to_time_preserves_timezone = true`."
)
@@preserve_timezone = false
end
@@preserve_timezone
end
def preserve_timezone # :nodoc:
Compatibility.preserve_timezone
end
# Change the output of <tt>ActiveSupport::TimeZone.utc_to_local</tt>.
#

View File

@ -287,6 +287,20 @@ class DateAndTimeCompatibilityTest < ActiveSupport::TestCase
assert_deprecated(ActiveSupport.deprecator) do
ActiveSupport.to_time_preserves_timezone = false
end
assert_deprecated(ActiveSupport.deprecator) do
ActiveSupport.to_time_preserves_timezone = nil
end
# When set to nil, the first call will report a deprecation,
# then switch the configured value to (and return) false.
assert_deprecated(ActiveSupport.deprecator) do
assert_equal false, ActiveSupport.to_time_preserves_timezone
end
assert_not_deprecated(ActiveSupport.deprecator) do
ActiveSupport.to_time_preserves_timezone
end
ensure
ActiveSupport.deprecator.silence do
ActiveSupport.to_time_preserves_timezone = current_preserve_tz

View File

@ -543,6 +543,21 @@ class TimeWithZoneTest < ActiveSupport::TestCase
end
end
def test_to_time_without_preserve_timezone_configured
with_preserve_timezone(nil) do
with_env_tz "US/Eastern" do
time = assert_deprecated(ActiveSupport.deprecator) { @twz.to_time }
assert_equal Time, time.class
assert_equal time.object_id, @twz.to_time.object_id
assert_equal Time.local(1999, 12, 31, 19), time
assert_equal Time.local(1999, 12, 31, 19).utc_offset, time.utc_offset
assert_equal false, ActiveSupport.to_time_preserves_timezone
end
end
end
def test_to_date
# 1 sec before midnight Jan 1 EST
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone["Eastern Time (US & Canada)"]).to_date