From 07db0e8eccf1b9be28205daccff5a4eb88969658 Mon Sep 17 00:00:00 2001 From: adrian-gomez Date: Thu, 5 Sep 2024 15:47:12 -0300 Subject: [PATCH] Use a custom deprecator instead of ActiveSupport::Deprecation directly Fix: https://github.com/rails/activeresource/pull/403 --- lib/active_resource.rb | 10 ++++++++++ lib/active_resource/railtie.rb | 4 ++++ lib/active_resource/validations.rb | 4 ++-- test/cases/base_errors_test.rb | 4 ++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/active_resource.rb b/lib/active_resource.rb index b78617d5f3..41e7af7886 100644 --- a/lib/active_resource.rb +++ b/lib/active_resource.rb @@ -46,6 +46,16 @@ module ActiveResource autoload :InheritingHash autoload :Validations autoload :Collection + + if ActiveSupport::VERSION::STRING >= "7.2" + def self.deprecator + @deprecator ||= ActiveSupport::Deprecation.new(VERSION::STRING, "ActiveResource") + end + else + def self.deprecator + ActiveSupport::Deprecation + end + end end require "active_resource/railtie" if defined?(Rails.application) diff --git a/lib/active_resource/railtie.rb b/lib/active_resource/railtie.rb index 8733e5516a..0a0492de58 100644 --- a/lib/active_resource/railtie.rb +++ b/lib/active_resource/railtie.rb @@ -21,5 +21,9 @@ class Railtie < Rails::Railtie app.config.active_job.custom_serializers << ActiveResource::ActiveJobSerializer end end + + initializer "active_resource.deprecator" do |app| + app.deprecators[:active_resource] = ActiveResource.deprecator + end end end diff --git a/lib/active_resource/validations.rb b/lib/active_resource/validations.rb index 929631ffaa..b06925b918 100644 --- a/lib/active_resource/validations.rb +++ b/lib/active_resource/validations.rb @@ -57,7 +57,7 @@ def from_json(json, save_cache = false) errors = decoded["errors"] || {} if errors.kind_of?(Array) # 3.2.1-style with array of strings - ActiveSupport::Deprecation.warn("Returning errors as an array of strings is deprecated.") + ActiveResource.deprecator.warn("Returning errors as an array of strings is deprecated.") from_array errors, save_cache else # 3.2.2+ style @@ -65,7 +65,7 @@ def from_json(json, save_cache = false) end else # <3.2-style respond_with - lacks 'errors' key - ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.') + ActiveResource.deprecator.warn('Returning errors as a hash without a root "errors" key is deprecated.') from_hash decoded, save_cache end end diff --git a/test/cases/base_errors_test.rb b/test/cases/base_errors_test.rb index d0a3164124..96bedd69ca 100644 --- a/test/cases/base_errors_test.rb +++ b/test/cases/base_errors_test.rb @@ -116,7 +116,7 @@ def test_should_parse_json_string_errors_with_an_errors_key mock.post "/people.json", {}, %q({"errors":["Age can't be blank", "Name can't be blank", "Name must start with a letter", "Person quota full for today.", "Phone work can't be blank", "Phone is not valid"]}), 422, "Content-Type" => "application/json; charset=utf-8" end - assert_deprecated(/as an array/) do + assert_deprecated(/as an array/, ActiveResource.deprecator) do invalid_user_using_format(:json) do assert @person.errors[:name].any? assert_equal ["can't be blank"], @person.errors[:age] @@ -133,7 +133,7 @@ def test_should_parse_3_1_style_json_errors mock.post "/people.json", {}, %q({"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."],"phone_work":["can't be blank"],"phone":["is not valid"]}), 422, "Content-Type" => "application/json; charset=utf-8" end - assert_deprecated(/without a root/) do + assert_deprecated(/without a root/, ActiveResource.deprecator) do invalid_user_using_format(:json) do assert @person.errors[:name].any? assert_equal ["can't be blank"], @person.errors[:age]