Skip to content

Commit

Permalink
Use a custom deprecator instead of ActiveSupport::Deprecation directly
Browse files Browse the repository at this point in the history
Fix: #403
  • Loading branch information
adrian-gomez authored and byroot committed Sep 26, 2024
1 parent f94238b commit 07db0e8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/active_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 4 additions & 0 deletions lib/active_resource/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/active_resource/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ 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
from_hash errors, save_cache
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
Expand Down
4 changes: 2 additions & 2 deletions test/cases/base_errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit 07db0e8

Please sign in to comment.