Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a custom deprecator instead of ActiveSupport::Deprecation directly #403

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ jobs:
- ruby: "3.3"
env:
BRANCH: 7-1-stable
- ruby: "3.1"
env:
BRANCH: 7-2-stable
experimental: false
- ruby: "3.2"
env:
BRANCH: 7-2-stable
experimental: false
- ruby: "3.3"
env:
BRANCH: 7-2-stable
- ruby: "3.1"
env:
BRANCH: main
Expand Down
8 changes: 8 additions & 0 deletions lib/active_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ module ActiveResource
autoload :InheritingHash
autoload :Validations
autoload :Collection

def self.deprecator
@deprecator ||= if ActiveSupport::VERSION::STRING >= "7.2"
ActiveSupport::Deprecation.new(VERSION::STRING, "ActiveResource")
else
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
Loading