@@ -9,60 +9,45 @@ module Base
9
9
class_attribute :_jsonapi_compliable
10
10
attr_reader :_jsonapi_scope
11
11
12
- before_action :parse_fieldsets!
13
- after_action :reset_scope_flag
12
+ around_action :wrap_context
14
13
end
15
14
16
- def default_page_number
17
- 1
18
- end
19
-
20
- def default_page_size
21
- 20
22
- end
23
-
24
- def default_sort
25
- 'id'
15
+ # TODO pass controller and action name here to guard
16
+ def wrap_context
17
+ _jsonapi_compliable . with_context ( self , action_name . to_sym ) do
18
+ yield
19
+ end
26
20
end
27
21
28
- def jsonapi_scope ( scope ,
29
- filter : true ,
30
- includes : true ,
31
- paginate : true ,
32
- extra_fields : true ,
33
- sort : true )
34
- scope = JsonapiCompliable ::Scope ::DefaultFilter . new ( self , scope ) . apply
35
- scope = JsonapiCompliable ::Scope ::Filter . new ( self , scope ) . apply if filter
36
- scope = JsonapiCompliable ::Scope ::ExtraFields . new ( self , scope ) . apply if extra_fields
37
- scope = JsonapiCompliable ::Scope ::Sideload . new ( self , scope ) . apply if includes
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
41
- scope = JsonapiCompliable ::Scope ::Paginate . new ( self , scope ) . apply if paginate
42
- scope
22
+ def jsonapi_scope ( scope , opts = { } )
23
+ query = Query . new ( _jsonapi_compliable , params )
24
+ _jsonapi_compliable . build_scope ( scope , query , opts )
43
25
end
44
26
45
- def reset_scope_flag
46
- @_jsonapi_scope = nil
47
- end
27
+ # TODO: refactor
28
+ def render_jsonapi ( scope , opts = { } )
29
+ query = Query . new ( _jsonapi_compliable , params )
30
+ query_hash = query . to_hash [ _jsonapi_compliable . type ]
48
31
49
- def parse_fieldsets!
50
- Util ::FieldParams . parse! ( params , :fields )
51
- Util ::FieldParams . parse! ( params , :extra_fields )
52
- end
32
+ scoped = scope
33
+ scoped = jsonapi_scope ( scoped ) unless opts [ :scope ] == false || scoped . is_a? ( JsonapiCompliable ::Scope )
34
+ resolved = scoped . respond_to? ( :resolve ) ? scoped . resolve : scoped
53
35
54
- def render_jsonapi ( scope , opts = { } )
55
- scoped = Util ::Scoping . apply? ( self , scope , opts . delete ( :scope ) ) ? jsonapi_scope ( scope ) : scope
56
36
options = default_jsonapi_render_options
57
- options [ :include ] = forced_includes || Util ::IncludeParams . scrub ( self )
58
- options [ :jsonapi ] = JsonapiCompliable :: Util :: Pagination . zero? ( params ) ? [ ] : scoped
59
- options [ :fields ] = Util :: FieldParams . fieldset ( params , :fields ) if params [ :fields ]
37
+ options [ :include ] = forced_includes || Util ::IncludeParams . scrub ( query_hash [ :include ] , _jsonapi_compliable . allowed_sideloads )
38
+ options [ :jsonapi ] = resolved
39
+ options [ :fields ] = query . fieldsets
60
40
options [ :meta ] ||= { }
61
41
options . merge! ( opts )
62
- options [ :meta ] [ :stats ] = Stats ::Payload . new ( self , scoped ) . generate if params [ :stats ]
42
+
43
+ if scoped . respond_to? ( :resolve_stats )
44
+ stats = scoped . resolve_stats
45
+ options [ :meta ] [ :stats ] = stats unless stats . empty?
46
+ end
47
+
63
48
options [ :expose ] ||= { }
64
49
options [ :expose ] [ :context ] = self
65
- options [ :expose ] [ :extra_fields ] = Util :: FieldParams . fieldset ( params , :extra_fields ) if params [ :extra_fields ]
50
+ options [ :expose ] [ :extra_fields ] = query_hash [ :extra_fields ]
66
51
67
52
render ( options )
68
53
end
@@ -74,6 +59,7 @@ def default_jsonapi_render_options
74
59
end
75
60
end
76
61
62
+ # Legacy
77
63
# TODO: This nastiness likely goes away once jsonapi standardizes
78
64
# a spec for nested relationships.
79
65
# See: https://github.com/json-api/json-api/issues/1089
@@ -95,6 +81,7 @@ def forced_includes(data = nil)
95
81
end
96
82
end
97
83
84
+ # Legacy
98
85
def force_includes?
99
86
%w( PUT PATCH POST ) . include? ( request . method ) and
100
87
raw_params . try ( :[] , :data ) . try ( :[] , :relationships ) . present?
0 commit comments