Skip to content

Commit 07db0e8

Browse files
adrian-gomezbyroot
authored andcommitted
Use a custom deprecator instead of ActiveSupport::Deprecation directly
Fix: #403
1 parent f94238b commit 07db0e8

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

lib/active_resource.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ module ActiveResource
4646
autoload :InheritingHash
4747
autoload :Validations
4848
autoload :Collection
49+
50+
if ActiveSupport::VERSION::STRING >= "7.2"
51+
def self.deprecator
52+
@deprecator ||= ActiveSupport::Deprecation.new(VERSION::STRING, "ActiveResource")
53+
end
54+
else
55+
def self.deprecator
56+
ActiveSupport::Deprecation
57+
end
58+
end
4959
end
5060

5161
require "active_resource/railtie" if defined?(Rails.application)

lib/active_resource/railtie.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@ class Railtie < Rails::Railtie
2121
app.config.active_job.custom_serializers << ActiveResource::ActiveJobSerializer
2222
end
2323
end
24+
25+
initializer "active_resource.deprecator" do |app|
26+
app.deprecators[:active_resource] = ActiveResource.deprecator
27+
end
2428
end
2529
end

lib/active_resource/validations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ def from_json(json, save_cache = false)
5757
errors = decoded["errors"] || {}
5858
if errors.kind_of?(Array)
5959
# 3.2.1-style with array of strings
60-
ActiveSupport::Deprecation.warn("Returning errors as an array of strings is deprecated.")
60+
ActiveResource.deprecator.warn("Returning errors as an array of strings is deprecated.")
6161
from_array errors, save_cache
6262
else
6363
# 3.2.2+ style
6464
from_hash errors, save_cache
6565
end
6666
else
6767
# <3.2-style respond_with - lacks 'errors' key
68-
ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.')
68+
ActiveResource.deprecator.warn('Returning errors as a hash without a root "errors" key is deprecated.')
6969
from_hash decoded, save_cache
7070
end
7171
end

test/cases/base_errors_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_should_parse_json_string_errors_with_an_errors_key
116116
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"
117117
end
118118

119-
assert_deprecated(/as an array/) do
119+
assert_deprecated(/as an array/, ActiveResource.deprecator) do
120120
invalid_user_using_format(:json) do
121121
assert @person.errors[:name].any?
122122
assert_equal ["can't be blank"], @person.errors[:age]
@@ -133,7 +133,7 @@ def test_should_parse_3_1_style_json_errors
133133
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"
134134
end
135135

136-
assert_deprecated(/without a root/) do
136+
assert_deprecated(/without a root/, ActiveResource.deprecator) do
137137
invalid_user_using_format(:json) do
138138
assert @person.errors[:name].any?
139139
assert_equal ["can't be blank"], @person.errors[:age]

0 commit comments

Comments
 (0)