Files
growstuff/config/initializers/jsonapi_resources_patch.rb
T
Brenda Wallace 10bf0a4f6c Add coding agent guidelines and improve feature spec logging
- Create AGENTS.md with layout advice for coding agents.
- Update timeline feature spec to use rack_test driver and improve context descriptions.
- Enhance spec_helper to log feature spec execution time.
- Add support for Wikidata service mocking in specs.
2026-10-14 09:19:48 +13:00

68 lines
2.3 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
# The gem also calls `resources name, options` with the options as a positional hash, which Rails 8.1
# deprecates ("resources received a hash argument ..."). Pass them as keywords instead.
module ActionDispatch
module Routing
class Mapper
module Resources
module KeywordOptions
def resources(*args, **options, &)
options = args.pop.merge(options) if args.last.is_a?(Hash)
super
end
end
prepend KeywordOptions
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