Skip to content

Commit 51c9592

Browse files
authored
chore: address deprecations (#1436)
* chore(deprecation): test_fixture= has been deprecated in favor of tests_fixtures= * chore(deprecations): ActiveSupport::Deprecation.silenced ``` ActiveSupport::Deprecation is deprecated and will be removed from Rails (use Rails.application.deprecators.silenced= instead) ``` * chore(deprecation): prefer ActiveSupport.deprectator or our own deprecator. since https://github.com/rails/rails/pull/47354/files
1 parent f193a35 commit 51c9592

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ def index_related_resources
6363

6464
def get_related_resource
6565
# :nocov:
66-
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resource`"\
66+
JSONAPI.configuration.deprecate "In #{self.class.name} you exposed a `get_related_resource`"\
6767
" action. Please use `show_related_resource` instead."
6868
show_related_resource
6969
# :nocov:
7070
end
7171

7272
def get_related_resources
7373
# :nocov:
74-
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resources`"\
74+
JSONAPI.configuration.deprecate "In #{self.class.name} you exposed a `get_related_resources`"\
7575
" action. Please use `index_related_resources` instead."
7676
index_related_resources
7777
# :nocov:

lib/jsonapi/configuration.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,16 @@ def exception_class_allowed?(e)
256256
@exception_class_allowlist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
257257
end
258258

259+
def deprecate(msg)
260+
if defined?(ActiveSupport.deprecator)
261+
ActiveSupport.deprecator.warn(msg)
262+
else
263+
ActiveSupport::Deprecation.warn(msg)
264+
end
265+
end
266+
259267
def default_processor_klass=(default_processor_klass)
260-
ActiveSupport::Deprecation.warn('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
268+
deprecate('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
261269
@default_processor_klass = default_processor_klass
262270
end
263271

@@ -271,7 +279,7 @@ def default_processor_klass_name=(default_processor_klass_name)
271279
end
272280

273281
def allow_include=(allow_include)
274-
ActiveSupport::Deprecation.warn('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
282+
deprecate('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
275283
@default_allow_include_to_one = allow_include
276284
@default_allow_include_to_many = allow_include
277285
end

lib/jsonapi/relationship.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def initialize(name, options = {})
2121
@polymorphic = options.fetch(:polymorphic, false) == true
2222
@polymorphic_types = options[:polymorphic_types]
2323
if options[:polymorphic_relations]
24-
ActiveSupport::Deprecation.warn('Use polymorphic_types instead of polymorphic_relations')
24+
JSONAPI.configuration.deprecate('Use polymorphic_types instead of polymorphic_relations')
2525
@polymorphic_types ||= options[:polymorphic_relations]
2626
end
2727

lib/jsonapi/resource_common.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def attribute(attribute_name, options = {})
626626
check_reserved_attribute_name(attr)
627627

628628
if (attr == :id) && (options[:format].nil?)
629-
ActiveSupport::Deprecation.warn('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
629+
JSONAPI.configuration.deprecate('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
630630
end
631631

632632
check_duplicate_attribute_name(attr) if options[:format].nil?
@@ -688,7 +688,7 @@ def has_one(*attrs)
688688
end
689689

690690
def belongs_to(*attrs)
691-
ActiveSupport::Deprecation.warn "In #{name} you exposed a `has_one` relationship "\
691+
JSONAPI.configuration.deprecate "In #{name} you exposed a `has_one` relationship "\
692692
" using the `belongs_to` class method. We think `has_one`" \
693693
" is more appropriate. If you know what you're doing," \
694694
" and don't want to see this warning again, override the" \

test/integration/requests/request_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ def test_deprecated_include_parameter_not_allowed
13711371
end
13721372

13731373
def test_deprecated_include_message
1374-
ActiveSupport::Deprecation.silenced = false
1374+
silence_deprecations! false
13751375
original_config = JSONAPI.configuration.dup
13761376
_out, err = capture_io do
13771377
eval <<-CODE
@@ -1381,7 +1381,7 @@ def test_deprecated_include_message
13811381
assert_match(/DEPRECATION WARNING: `allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options./, err)
13821382
ensure
13831383
JSONAPI.configuration = original_config
1384-
ActiveSupport::Deprecation.silenced = true
1384+
silence_deprecations! true
13851385
end
13861386

13871387

test/test_helper.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
config.related_identities_set = SortedSet
4545
end
4646

47-
ActiveSupport::Deprecation.silenced = true
48-
4947
puts "Testing With RAILS VERSION #{Rails.version}"
5048

5149
class TestApp < ::Rails::Application
@@ -71,6 +69,14 @@ class TestApp < ::Rails::Application
7169
config.hosts << "www.example.com"
7270
end
7371

72+
def silence_deprecations!(bool = true)
73+
if defined?(Rails.application) && Rails.application.respond_to?(:deprecators)
74+
Rails.application.deprecators.silenced = bool
75+
else
76+
ActiveSupport::Deprecation.silenced = bool
77+
end
78+
end
79+
7480
require 'rails/test_help'
7581

7682
DatabaseCleaner.allow_remote_database_url = true
@@ -463,7 +469,11 @@ def run_in_transaction?
463469
true
464470
end
465471

466-
self.fixture_path = "#{Rails.root}/fixtures"
472+
if respond_to?(:fixture_paths=)
473+
self.fixture_paths |= ["#{Rails.root}/fixtures"]
474+
else
475+
self.fixture_path = "#{Rails.root}/fixtures"
476+
end
467477
fixtures :all
468478

469479
def adapter_name
@@ -515,15 +525,23 @@ def response_json_for_compare(response)
515525
end
516526

517527
class ActiveSupport::TestCase
518-
self.fixture_path = "#{Rails.root}/fixtures"
528+
if respond_to?(:fixture_paths=)
529+
self.fixture_paths |= ["#{Rails.root}/fixtures"]
530+
else
531+
self.fixture_path = "#{Rails.root}/fixtures"
532+
end
519533
fixtures :all
520534
setup do
521535
@routes = TestApp.routes
522536
end
523537
end
524538

525539
class ActionDispatch::IntegrationTest
526-
self.fixture_path = "#{Rails.root}/fixtures"
540+
if respond_to?(:fixture_paths=)
541+
self.fixture_paths |= ["#{Rails.root}/fixtures"]
542+
else
543+
self.fixture_path = "#{Rails.root}/fixtures"
544+
end
527545
fixtures :all
528546

529547
def assert_jsonapi_response(expected_status, msg = nil)

test/unit/resource/resource_test.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@ def test_key_type_proc
399399
end
400400

401401
def test_id_attr_deprecation
402-
403-
ActiveSupport::Deprecation.silenced = false
402+
silence_deprecations! false
404403
_out, err = capture_io do
405404
eval <<-CODE
406405
class ProblemResource < JSONAPI::Resource
@@ -410,7 +409,7 @@ class ProblemResource < JSONAPI::Resource
410409
end
411410
assert_match /DEPRECATION WARNING: Id without format is no longer supported. Please remove ids from attributes, or specify a format./, err
412411
ensure
413-
ActiveSupport::Deprecation.silenced = true
412+
silence_deprecations! true
414413
end
415414

416415
def test_id_attr_with_format

0 commit comments

Comments
 (0)