@@ -48,7 +48,7 @@ def jsonapi_resource(*resources, &_block)
4848
4949 resource @resource_type , options do
5050 # :nocov:
51- if @scope . respond_to? :[]=
51+ if @scope . respond_to? ( :[]= )
5252 # Rails 4
5353 @scope [ :jsonapi_resource ] = @resource_type
5454
@@ -57,14 +57,19 @@ def jsonapi_resource(*resources, &_block)
5757 else
5858 jsonapi_relationships
5959 end
60- else
61- # Rails 5+
62- # Rails 8.1 changed SingletonResource.new to accept only 3 arguments (removed options)
63- resource_arg = if Rails ::VERSION ::MAJOR >= 8 && Rails ::VERSION ::MINOR >= 1
64- SingletonResource . new ( @resource_type , api_only? , @scope [ :shallow ] )
60+ elsif Rails ::VERSION ::MAJOR >= 8 && Rails ::VERSION ::MINOR >= 1
61+ # Rails 8.1+
62+ # Rails 8.1 changed Scope to not support []= and Resource.new signature
63+ # Use instance variable to track resource type
64+ @jsonapi_resource_type = @resource_type
65+ if block_given?
66+ yield
6567 else
66- SingletonResource . new ( @resource_type , api_only? , @scope [ :shallow ] , options )
68+ jsonapi_relationships
6769 end
70+ else
71+ # Rails 5-8.0
72+ resource_arg = SingletonResource . new ( @resource_type , api_only? , @scope [ :shallow ] , options )
6873
6974 jsonapi_resource_scope ( resource_arg , @resource_type ) do
7075 if block_given?
@@ -130,22 +135,27 @@ def jsonapi_resources(*resources, &_block)
130135
131136 resources @resource_type , options do
132137 # :nocov:
133- if @scope . respond_to? :[]=
138+ if @scope . respond_to? ( :[]= )
134139 # Rails 4
135140 @scope [ :jsonapi_resource ] = @resource_type
136141 if block_given?
137142 yield
138143 else
139144 jsonapi_relationships
140145 end
141- else
142- # Rails 5+
143- # Rails 8.1 changed Resource.new to accept only 3 arguments (removed options)
144- resource_arg = if Rails ::VERSION ::MAJOR >= 8 && Rails ::VERSION ::MINOR >= 1
145- Resource . new ( @resource_type , api_only? , @scope [ :shallow ] )
146+ elsif Rails ::VERSION ::MAJOR >= 8 && Rails ::VERSION ::MINOR >= 1
147+ # Rails 8.1+
148+ # Rails 8.1 changed Scope to not support []= and Resource.new signature
149+ # Use instance variable to track resource type
150+ @jsonapi_resource_type = @resource_type
151+ if block_given?
152+ yield
146153 else
147- Resource . new ( @resource_type , api_only? , @scope [ :shallow ] , options )
154+ jsonapi_relationships
148155 end
156+ else
157+ # Rails 5-8.0
158+ resource_arg = Resource . new ( @resource_type , api_only? , @scope [ :shallow ] , options )
149159
150160 jsonapi_resource_scope ( resource_arg , @resource_type ) do
151161 if block_given?
@@ -291,7 +301,7 @@ def jsonapi_resource_scope(resource, resource_type) #:nodoc:
291301 private
292302
293303 def resource_type_with_module_prefix ( resource = nil )
294- resource_name = resource || @scope [ :jsonapi_resource ]
304+ resource_name = resource || @scope [ :jsonapi_resource ] || @jsonapi_resource_type
295305 [ @scope [ :module ] , resource_name ] . compact . collect ( &:to_s ) . join ( '/' )
296306 end
297307 end
0 commit comments