diff --git a/lib/jsonapi/configuration.rb b/lib/jsonapi/configuration.rb index d5d24f32..3d20f1fb 100644 --- a/lib/jsonapi/configuration.rb +++ b/lib/jsonapi/configuration.rb @@ -328,13 +328,18 @@ def self.configure # Rails 7.2+ made ActiveSupport::Deprecation.warn a private method # This helper provides backward-compatible deprecation warnings def self.warn_deprecated(message) - if defined?(ActiveSupport::Deprecation) && ActiveSupport::Deprecation.respond_to?(:warn) - # Rails < 7.2 - ActiveSupport::Deprecation.warn(message) + if defined?(ActiveSupport::Deprecation) + begin + # Try to call warn as a class method (Rails < 7.2) + ActiveSupport::Deprecation.warn(message) + rescue NoMethodError + # Rails 7.2+: warn is now private, use instance method instead + version = defined?(JSONAPI::Resources::VERSION) ? JSONAPI::Resources::VERSION : '0.11.0' + deprecation = ActiveSupport::Deprecation.new(version, 'jsonapi-resources') + deprecation.warn(message) + end else - # Rails 7.2+ or fallback - use standard warning with deprecation formatting - # Rails 7.2 doesn't provide a public API for custom deprecation warnings - # So we use Kernel#warn with a deprecation prefix + # Fallback for environments without ActiveSupport warn "[DEPRECATION] #{message}" end end diff --git a/test/unit/configuration/deprecation_test.rb b/test/unit/configuration/deprecation_test.rb new file mode 100644 index 00000000..115ab22c --- /dev/null +++ b/test/unit/configuration/deprecation_test.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../test_helper', __FILE__) + +# Test for Issue #1465: ActiveSupport::Deprecation private method error in Rails 7.2 +# https://github.com/cerebris/jsonapi-resources/issues/1465 +# +# Rails 7.2 made ActiveSupport::Deprecation.warn a private method +# This test ensures our warn_deprecated helper handles both old and new Rails versions + +class DeprecationTest < ActiveSupport::TestCase + def test_warn_deprecated_does_not_raise_error + # This should not raise NoMethodError: private method `warn' called + assert_nothing_raised do + JSONAPI.warn_deprecated('Test deprecation warning') + end + end + + def test_warn_deprecated_with_activerecord_present + # Ensure the warning works when ActiveSupport::Deprecation is available + skip 'ActiveSupport::Deprecation not available' unless defined?(ActiveSupport::Deprecation) + + assert_nothing_raised do + JSONAPI.warn_deprecated('Test deprecation with ActiveSupport') + end + end + + def test_warn_deprecated_with_multiple_calls + # Test that multiple calls don't cause issues + # This is especially important for Rails 7.2 compatibility + assert_nothing_raised do + 3.times do |i| + JSONAPI.warn_deprecated("Test deprecation warning #{i}") + end + end + end +end