@@ -33,12 +33,24 @@ def jsonapi_resource(*resources, &_block)
3333 end
3434
3535 resource @resource_type , options do
36- @scope [ :jsonapi_resource ] = @resource_type
37-
38- if block_given?
39- yield
36+ if @scope . respond_to? :[]=
37+ # Rails 4
38+ @scope [ :jsonapi_resource ] = @resource_type
39+
40+ if block_given?
41+ yield
42+ else
43+ jsonapi_relationships
44+ end
4045 else
41- jsonapi_relationships
46+ # Rails 5
47+ jsonapi_resource_scope ( SingletonResource . new ( @resource_type , api_only? , @scope [ :shallow ] , options ) , @resource_type ) do
48+ if block_given?
49+ yield
50+ else
51+ jsonapi_relationships
52+ end
53+ end
4254 end
4355 end
4456 end
@@ -87,12 +99,23 @@ def jsonapi_resources(*resources, &_block)
8799 end
88100
89101 resources @resource_type , options do
90- @scope [ :jsonapi_resource ] = @resource_type
91-
92- if block_given?
93- yield
102+ if @scope . respond_to? :[]=
103+ # Rails 4
104+ @scope [ :jsonapi_resource ] = @resource_type
105+ if block_given?
106+ yield
107+ else
108+ jsonapi_relationships
109+ end
94110 else
95- jsonapi_relationships
111+ # Rails 5
112+ jsonapi_resource_scope ( Resource . new ( @resource_type , api_only? , @scope [ :shallow ] , options ) , @resource_type ) do
113+ if block_given?
114+ yield
115+ else
116+ jsonapi_relationships
117+ end
118+ end
96119 end
97120 end
98121 end
@@ -206,6 +229,16 @@ def jsonapi_related_resources(*relationship)
206229 action : 'get_related_resources' , via : [ :get ]
207230 end
208231
232+ protected
233+
234+ def jsonapi_resource_scope ( resource , resource_type ) #:nodoc:
235+ @scope = @scope . new ( scope_level_resource : resource , jsonapi_resource : resource_type )
236+
237+ controller ( resource . resource_scope ) { yield }
238+ ensure
239+ @scope = @scope . parent
240+ end
241+
209242 private
210243
211244 def resource_type_with_module_prefix ( resource = nil )
0 commit comments