Skip to content

Commit 17c0160

Browse files
authored
Merge pull request #19 from speee/fix/rails72-activesupport-deprecation-1465
Fix Issue JSONAPI-Resources#1465: Rails 7.2 ActiveSupport::Deprecation compatibility
2 parents 4734642 + 33e2dad commit 17c0160

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

lib/jsonapi/configuration.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,18 @@ def self.configure
328328
# Rails 7.2+ made ActiveSupport::Deprecation.warn a private method
329329
# This helper provides backward-compatible deprecation warnings
330330
def self.warn_deprecated(message)
331-
if defined?(ActiveSupport::Deprecation) && ActiveSupport::Deprecation.respond_to?(:warn)
332-
# Rails < 7.2
333-
ActiveSupport::Deprecation.warn(message)
331+
if defined?(ActiveSupport::Deprecation)
332+
begin
333+
# Try to call warn as a class method (Rails < 7.2)
334+
ActiveSupport::Deprecation.warn(message)
335+
rescue NoMethodError
336+
# Rails 7.2+: warn is now private, use instance method instead
337+
version = defined?(JSONAPI::Resources::VERSION) ? JSONAPI::Resources::VERSION : '0.11.0'
338+
deprecation = ActiveSupport::Deprecation.new(version, 'jsonapi-resources')
339+
deprecation.warn(message)
340+
end
334341
else
335-
# Rails 7.2+ or fallback - use standard warning with deprecation formatting
336-
# Rails 7.2 doesn't provide a public API for custom deprecation warnings
337-
# So we use Kernel#warn with a deprecation prefix
342+
# Fallback for environments without ActiveSupport
338343
warn "[DEPRECATION] #{message}"
339344
end
340345
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require File.expand_path('../../../test_helper', __FILE__)
2+
3+
# Test for Issue #1465: ActiveSupport::Deprecation private method error in Rails 7.2
4+
# https://github.com/cerebris/jsonapi-resources/issues/1465
5+
#
6+
# Rails 7.2 made ActiveSupport::Deprecation.warn a private method
7+
# This test ensures our warn_deprecated helper handles both old and new Rails versions
8+
9+
class DeprecationTest < ActiveSupport::TestCase
10+
def test_warn_deprecated_does_not_raise_error
11+
# This should not raise NoMethodError: private method `warn' called
12+
assert_nothing_raised do
13+
JSONAPI.warn_deprecated('Test deprecation warning')
14+
end
15+
end
16+
17+
def test_warn_deprecated_with_activerecord_present
18+
# Ensure the warning works when ActiveSupport::Deprecation is available
19+
skip 'ActiveSupport::Deprecation not available' unless defined?(ActiveSupport::Deprecation)
20+
21+
assert_nothing_raised do
22+
JSONAPI.warn_deprecated('Test deprecation with ActiveSupport')
23+
end
24+
end
25+
26+
def test_warn_deprecated_with_multiple_calls
27+
# Test that multiple calls don't cause issues
28+
# This is especially important for Rails 7.2 compatibility
29+
assert_nothing_raised do
30+
3.times do |i|
31+
JSONAPI.warn_deprecated("Test deprecation warning #{i}")
32+
end
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)