From 3457a4676d91836ac3dfd6765f069eee752c598c Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 12 Apr 2021 22:03:31 +0200 Subject: [PATCH] Allow to opt-in to the new TimeWithZone.name and fix XmlMini serialization --- activesupport/lib/active_support/railtie.rb | 7 +++++++ activesupport/lib/active_support/time_with_zone.rb | 2 ++ activesupport/lib/active_support/xml_mini.rb | 1 + activesupport/test/xml_mini_test.rb | 8 ++++++++ railties/lib/rails/application/configuration.rb | 1 + 5 files changed, 19 insertions(+) diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index de5ce6c33d8..735277ad978 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -9,6 +9,13 @@ module ActiveSupport config.eager_load_namespaces << ActiveSupport + initializer "active_support.remove_deprecated_time_with_zone_name" do |app| + if app.config.active_support.remove_deprecated_time_with_zone_name + require "active_support/time_with_zone" + TimeWithZone.singleton_class.remove_method(:name) + end + end + initializer "active_support.set_authenticated_message_encryption" do |app| config.after_initialize do unless app.config.active_support.use_authenticated_message_encryption.nil? diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index c9b5d0d3fca..47a969ef4a8 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -45,6 +45,8 @@ module ActiveSupport ActiveSupport::Deprecation.warn(<<~EOM) ActiveSupport::TimeWithZone.name has been deprecated and from Rails 7.1 will use the default Ruby implementation. + You can set `config.active_support.remove_deprecated_time_with_zone_name = true` + to enable thew new behavior now. EOM "Time" diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index f6ae08bb5db..0e38c79503e 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -50,6 +50,7 @@ module ActiveSupport "Hash" => "hash" } end + TYPE_NAMES["ActiveSupport::TimeWithZone"] = TYPE_NAMES["Time"] FORMATTING = { "symbol" => Proc.new { |symbol| symbol.to_s }, diff --git a/activesupport/test/xml_mini_test.rb b/activesupport/test/xml_mini_test.rb index 46ea0c87974..022df2955f2 100644 --- a/activesupport/test/xml_mini_test.rb +++ b/activesupport/test/xml_mini_test.rb @@ -134,6 +134,14 @@ module XmlMiniTest assert_xml("1993-02-24T12:00:00+09:00") end + test "#to_tag accepts ActiveSupport::TimeWithZone types" do + time = ActiveSupport::TimeWithZone.new(Time.new(1993, 02, 24, 12, 0, 0, "+09:00"), ActiveSupport::TimeZone["Europe/Paris"]) + ActiveSupport::TimeWithZone.stub(:name, "ActiveSupport::TimeWithZone") do + @xml.to_tag(:b, time, @options) + assert_xml("1993-02-24T13:00:00+01:00") + end + end + test "#to_tag accepts array types" do @xml.to_tag(:b, ["first_name", "last_name"], @options) assert_xml("first_namelast_name") diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 3f8f58c41e1..4e7c4c9d83f 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -209,6 +209,7 @@ module Rails if respond_to?(:active_support) active_support.hash_digest_class = OpenSSL::Digest::SHA256 active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA256 + active_support.remove_deprecated_time_with_zone_name = true end else raise "Unknown version #{target_version.to_s.inspect}"