diff --git a/lib/jsonapi/acts_as_resource_controller.rb b/lib/jsonapi/acts_as_resource_controller.rb index 86b39e6c..1ef1707c 100644 --- a/lib/jsonapi/acts_as_resource_controller.rb +++ b/lib/jsonapi/acts_as_resource_controller.rb @@ -63,7 +63,7 @@ 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: @@ -71,7 +71,7 @@ def get_related_resource 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: diff --git a/lib/jsonapi/configuration.rb b/lib/jsonapi/configuration.rb index b9f4b772..e739b722 100644 --- a/lib/jsonapi/configuration.rb +++ b/lib/jsonapi/configuration.rb @@ -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 @@ -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 diff --git a/lib/jsonapi/relationship.rb b/lib/jsonapi/relationship.rb index da706523..9d7ffc46 100644 --- a/lib/jsonapi/relationship.rb +++ b/lib/jsonapi/relationship.rb @@ -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 diff --git a/lib/jsonapi/resource_common.rb b/lib/jsonapi/resource_common.rb index 85c81361..b841b732 100644 --- a/lib/jsonapi/resource_common.rb +++ b/lib/jsonapi/resource_common.rb @@ -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? @@ -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" \ diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index 52531514..aa9989c6 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -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 @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index b92d8428..9f8f03b2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 @@ -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 @@ -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 @@ -515,7 +525,11 @@ 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 @@ -523,7 +537,11 @@ class ActiveSupport::TestCase 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) diff --git a/test/unit/resource/resource_test.rb b/test/unit/resource/resource_test.rb index 62518309..351dbb99 100644 --- a/test/unit/resource/resource_test.rb +++ b/test/unit/resource/resource_test.rb @@ -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 @@ -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