@@ -7,7 +7,7 @@ module Base
7
7
8
8
included do
9
9
class_attribute :_jsonapi_compliable
10
- attr_reader :_jsonapi_scoped
10
+ attr_reader :_jsonapi_scope
11
11
12
12
before_action :parse_fieldsets!
13
13
after_action :reset_scope_flag
@@ -36,13 +36,14 @@ def jsonapi_scope(scope,
36
36
scope = JsonapiCompliable ::Scope ::ExtraFields . new ( self , scope ) . apply if extra_fields
37
37
scope = JsonapiCompliable ::Scope ::Sideload . new ( self , scope ) . apply if includes
38
38
scope = JsonapiCompliable ::Scope ::Sort . new ( self , scope ) . apply if sort
39
+ # This is set before pagination so it can be re-used for stats
40
+ @_jsonapi_scope = scope
39
41
scope = JsonapiCompliable ::Scope ::Paginate . new ( self , scope ) . apply if paginate
40
- @_jsonapi_scoped = true
41
42
scope
42
43
end
43
44
44
45
def reset_scope_flag
45
- @_jsonapi_scoped = false
46
+ @_jsonapi_scope = nil
46
47
end
47
48
48
49
def parse_fieldsets!
@@ -51,14 +52,16 @@ def parse_fieldsets!
51
52
end
52
53
53
54
def render_ams ( scope , opts = { } )
54
- scope = jsonapi_scope ( scope ) if Util ::Scoping . apply? ( self , scope , opts . delete ( :scope ) )
55
+ scoped = Util ::Scoping . apply? ( self , scope , opts . delete ( :scope ) ) ? jsonapi_scope ( scope ) : scope
55
56
options = default_ams_options
56
57
options [ :include ] = forced_includes || Util ::IncludeParams . scrub ( self )
57
- options [ :jsonapi ] = scope
58
+ options [ :jsonapi ] = JsonapiCompliable :: Util :: Pagination . zero? ( params ) ? [ ] : scoped
58
59
options [ :fields ] = Util ::FieldParams . fieldset ( params , :fields ) if params [ :fields ]
59
60
options [ :extra_fields ] = Util ::FieldParams . fieldset ( params , :extra_fields ) if params [ :extra_fields ]
60
-
61
+ options [ :meta ] ||= { }
61
62
options . merge! ( opts )
63
+ options [ :meta ] [ :stats ] = Stats ::Payload . new ( self , scoped ) . generate if params [ :stats ]
64
+
62
65
render ( options )
63
66
end
64
67
0 commit comments