@@ -7,7 +7,7 @@ module Base
77
88 included do
99 class_attribute :_jsonapi_compliable
10- attr_reader :_jsonapi_scoped
10+ attr_reader :_jsonapi_scope
1111
1212 before_action :parse_fieldsets!
1313 after_action :reset_scope_flag
@@ -36,13 +36,14 @@ def jsonapi_scope(scope,
3636 scope = JsonapiCompliable ::Scope ::ExtraFields . new ( self , scope ) . apply if extra_fields
3737 scope = JsonapiCompliable ::Scope ::Sideload . new ( self , scope ) . apply if includes
3838 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
3941 scope = JsonapiCompliable ::Scope ::Paginate . new ( self , scope ) . apply if paginate
40- @_jsonapi_scoped = true
4142 scope
4243 end
4344
4445 def reset_scope_flag
45- @_jsonapi_scoped = false
46+ @_jsonapi_scope = nil
4647 end
4748
4849 def parse_fieldsets!
@@ -51,14 +52,16 @@ def parse_fieldsets!
5152 end
5253
5354 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
5556 options = default_ams_options
5657 options [ :include ] = forced_includes || Util ::IncludeParams . scrub ( self )
57- options [ :jsonapi ] = scope
58+ options [ :jsonapi ] = JsonapiCompliable :: Util :: Pagination . zero? ( params ) ? [ ] : scoped
5859 options [ :fields ] = Util ::FieldParams . fieldset ( params , :fields ) if params [ :fields ]
5960 options [ :extra_fields ] = Util ::FieldParams . fieldset ( params , :extra_fields ) if params [ :extra_fields ]
60-
61+ options [ :meta ] ||= { }
6162 options . merge! ( opts )
63+ options [ :meta ] [ :stats ] = Stats ::Payload . new ( self , scoped ) . generate if params [ :stats ]
64+
6265 render ( options )
6366 end
6467
0 commit comments