Skip to content
Merged
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
17 changes: 11 additions & 6 deletions lib/jsonapi/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/unit/configuration/deprecation_test.rb
Original file line number Diff line number Diff line change
@@ -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