Skip to content

Commit

Permalink
chore: address deprecations (#1436)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bf4 authored Jan 22, 2024
1 parent f193a35 commit 51c9592
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/jsonapi/acts_as_resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ def index_related_resources

def get_related_resource
# :nocov:
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resource`"\
JSONAPI.configuration.deprecate "In #{self.class.name} you exposed a `get_related_resource`"\
" action. Please use `show_related_resource` instead."
show_related_resource
# :nocov:
end

def get_related_resources
# :nocov:
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resources`"\
JSONAPI.configuration.deprecate "In #{self.class.name} you exposed a `get_related_resources`"\
" action. Please use `index_related_resources` instead."
index_related_resources
# :nocov:
Expand Down
12 changes: 10 additions & 2 deletions lib/jsonapi/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,16 @@ def exception_class_allowed?(e)
@exception_class_allowlist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
end

def deprecate(msg)
if defined?(ActiveSupport.deprecator)
ActiveSupport.deprecator.warn(msg)
else
ActiveSupport::Deprecation.warn(msg)
end
end

def default_processor_klass=(default_processor_klass)
ActiveSupport::Deprecation.warn('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
deprecate('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
@default_processor_klass = default_processor_klass
end

Expand All @@ -271,7 +279,7 @@ def default_processor_klass_name=(default_processor_klass_name)
end

def allow_include=(allow_include)
ActiveSupport::Deprecation.warn('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
deprecate('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
@default_allow_include_to_one = allow_include
@default_allow_include_to_many = allow_include
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(name, options = {})
@polymorphic = options.fetch(:polymorphic, false) == true
@polymorphic_types = options[:polymorphic_types]
if options[:polymorphic_relations]
ActiveSupport::Deprecation.warn('Use polymorphic_types instead of polymorphic_relations')
JSONAPI.configuration.deprecate('Use polymorphic_types instead of polymorphic_relations')
@polymorphic_types ||= options[:polymorphic_relations]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/jsonapi/resource_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def attribute(attribute_name, options = {})
check_reserved_attribute_name(attr)

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

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

def belongs_to(*attrs)
ActiveSupport::Deprecation.warn "In #{name} you exposed a `has_one` relationship "\
JSONAPI.configuration.deprecate "In #{name} you exposed a `has_one` relationship "\
" using the `belongs_to` class method. We think `has_one`" \
" is more appropriate. If you know what you're doing," \
" and don't want to see this warning again, override the" \
Expand Down
4 changes: 2 additions & 2 deletions test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ def test_deprecated_include_parameter_not_allowed
end

def test_deprecated_include_message
ActiveSupport::Deprecation.silenced = false
silence_deprecations! false
original_config = JSONAPI.configuration.dup
_out, err = capture_io do
eval <<-CODE
Expand All @@ -1381,7 +1381,7 @@ def test_deprecated_include_message
assert_match(/DEPRECATION WARNING: `allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options./, err)
ensure
JSONAPI.configuration = original_config
ActiveSupport::Deprecation.silenced = true
silence_deprecations! true
end


Expand Down
28 changes: 23 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
config.related_identities_set = SortedSet
end

ActiveSupport::Deprecation.silenced = true

puts "Testing With RAILS VERSION #{Rails.version}"

class TestApp < ::Rails::Application
Expand All @@ -71,6 +69,14 @@ class TestApp < ::Rails::Application
config.hosts << "www.example.com"
end

def silence_deprecations!(bool = true)
if defined?(Rails.application) && Rails.application.respond_to?(:deprecators)
Rails.application.deprecators.silenced = bool
else
ActiveSupport::Deprecation.silenced = bool
end
end

require 'rails/test_help'

DatabaseCleaner.allow_remote_database_url = true
Expand Down Expand Up @@ -463,7 +469,11 @@ def run_in_transaction?
true
end

self.fixture_path = "#{Rails.root}/fixtures"
if respond_to?(:fixture_paths=)
self.fixture_paths |= ["#{Rails.root}/fixtures"]
else
self.fixture_path = "#{Rails.root}/fixtures"
end
fixtures :all

def adapter_name
Expand Down Expand Up @@ -515,15 +525,23 @@ def response_json_for_compare(response)
end

class ActiveSupport::TestCase
self.fixture_path = "#{Rails.root}/fixtures"
if respond_to?(:fixture_paths=)
self.fixture_paths |= ["#{Rails.root}/fixtures"]
else
self.fixture_path = "#{Rails.root}/fixtures"
end
fixtures :all
setup do
@routes = TestApp.routes
end
end

class ActionDispatch::IntegrationTest
self.fixture_path = "#{Rails.root}/fixtures"
if respond_to?(:fixture_paths=)
self.fixture_paths |= ["#{Rails.root}/fixtures"]
else
self.fixture_path = "#{Rails.root}/fixtures"
end
fixtures :all

def assert_jsonapi_response(expected_status, msg = nil)
Expand Down
5 changes: 2 additions & 3 deletions test/unit/resource/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,7 @@ def test_key_type_proc
end

def test_id_attr_deprecation

ActiveSupport::Deprecation.silenced = false
silence_deprecations! false
_out, err = capture_io do
eval <<-CODE
class ProblemResource < JSONAPI::Resource
Expand All @@ -410,7 +409,7 @@ class ProblemResource < JSONAPI::Resource
end
assert_match /DEPRECATION WARNING: Id without format is no longer supported. Please remove ids from attributes, or specify a format./, err
ensure
ActiveSupport::Deprecation.silenced = true
silence_deprecations! true
end

def test_id_attr_with_format
Expand Down

0 comments on commit 51c9592

Please sign in to comment.