Skip to content

Commit 005a426

Browse files
takaokoujiclaude
andcommitted
Fix Rails 8.2 deprecation warnings and improve Rack compatibility
This commit addresses two issues: 1. Rails 8.2 Deprecation Warning: Update routing_ext.rb to use keyword arguments instead of hash arguments when calling `resource` and `resources` methods. The hash argument support will be removed in Rails 8.2. 2. Rack Compatibility: Update status_code_for method to prefer Rack::Utils.status_code (Rack 3.0+) while maintaining backward compatibility with Rack 2.x using SYMBOL_TO_STATUS_CODE. Addresses feedback from speee#1 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 37bad60 commit 005a426

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

lib/jsonapi/error.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,28 @@ class Error
1313
def self.status_code_for(status_symbol)
1414
return nil if status_symbol.nil?
1515

16-
# Try the symbol directly first
16+
# Use Rack::Utils.status_code if available (Rack 3.0+)
17+
if Rack::Utils.respond_to?(:status_code)
18+
begin
19+
# status_code returns integer, convert to string
20+
code = Rack::Utils.status_code(status_symbol)
21+
return code&.to_s
22+
rescue ArgumentError
23+
# If the symbol is not recognized, try deprecated symbols
24+
if DEPRECATED_STATUS_SYMBOLS.key?(status_symbol)
25+
begin
26+
code = Rack::Utils.status_code(DEPRECATED_STATUS_SYMBOLS[status_symbol])
27+
return code&.to_s
28+
rescue ArgumentError
29+
# Symbol not found even after trying deprecated mapping
30+
return nil
31+
end
32+
end
33+
return nil
34+
end
35+
end
36+
37+
# Fallback to SYMBOL_TO_STATUS_CODE for Rack 2.x
1738
code = Rack::Utils::SYMBOL_TO_STATUS_CODE[status_symbol]
1839

1940
# If not found and it's a deprecated symbol, try the new symbol

lib/jsonapi/routing_ext.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def jsonapi_resource(*resources, &_block)
4646
options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy')
4747
end
4848

49-
resource @resource_type, options do
49+
resource @resource_type, **options do
5050
# :nocov:
5151
if @scope.respond_to?(:[]=)
5252
# Rails 4
@@ -133,7 +133,7 @@ def jsonapi_resources(*resources, &_block)
133133
options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy')
134134
end
135135

136-
resources @resource_type, options do
136+
resources @resource_type, **options do
137137
# :nocov:
138138
if @scope.respond_to?(:[]=)
139139
# Rails 4

0 commit comments

Comments
 (0)