Files
growstuff/config/initializers/jsonapi_resources_patch.rb
T
4563df45d0 Upgrade to Rails 8.1 (#4685)
* Upgrade application to Rails 8.1

- Update rails gem to ~> 8.1.0
- Set config.load_defaults 8.1 in application.rb
- Add gem 'csv' for Ruby 3.4+ compatibility
- Replace deprecated 'render text:' with 'render plain:' in PagesController
- Add compatibility patch for jsonapi-resources routing in Rails 8.1
- Add compatibility patch for Faraday 2.x error constants
- Add Searchkick test stubs for environments without Elasticsearch
- Include required Active Storage update migrations

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* Remove disabled searchkick

* Apply suggestion from @CloCkWeRX

* Delete config/initializers/faraday_patch.rb

* Apply suggestions from code review

Co-authored-by: Daniel O'Connor <daniel.oconnor@gmail.com>

* Remove modifications for tests without elasticsearch

* Remove psych gem and its dependencies

Removed psych gem version 5.4.0 and its dependencies.

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-08-09 22:53:15 +09:30

49 lines
1.7 KiB
Ruby

# frozen_string_literal: true
# JSONAPI::Resources (0.10.7) is currently incompatible with Rails 8.1
# because ActionDispatch::Routing::Mapper::Resources::Resource.initialize now expects keyword arguments for only/except.
# This patch ensures that it passes keyword arguments correctly.
# Remove when https://github.com/JSONAPI-Resources/jsonapi-resources/issues/1488 is fixed
if defined?(JSONAPI)
module ActionDispatch
module Routing
class Mapper
module Resources
class Resource
alias_method :original_initialize, :initialize
def initialize(entities, api_only, shallow, options = {})
# In Rails 8.1, initialize(entities, api_only, shallow, only: nil, except: nil, **options)
# and jsonapi-resources passes them in the options hash.
if options.is_a?(Hash)
only = options.delete(:only)
except = options.delete(:except)
original_initialize(entities, api_only, shallow, only: only, except: except, **options)
else
original_initialize(entities, api_only, shallow, options)
end
end
end
end
end
end
end
module JSONAPI
module RoutingExt
module Mapper
def jsonapi_resource_scope(resource, resource_type)
# The original implementation uses Resource.new which we patched above.
# We need to make sure we don't break Rails 5/6/7/8 logic if it were to run there.
@scope = @scope.new(scope_level_resource: resource, jsonapi_resource: resource_type)
controller(resource.resource_scope) { yield }
ensure
@scope = @scope.parent
end
end
end
end
end