File tree Expand file tree Collapse file tree 4 files changed +55
-7
lines changed Expand file tree Collapse file tree 4 files changed +55
-7
lines changed Original file line number Diff line number Diff line change @@ -28,8 +28,10 @@ def resource!
28
28
29
29
# TODO pass controller and action name here to guard
30
30
def wrap_context
31
- resource . with_context ( self , action_name . to_sym ) do
32
- yield
31
+ if self . class . _jsonapi_compliable
32
+ resource . with_context ( self , action_name . to_sym ) do
33
+ yield
34
+ end
33
35
end
34
36
end
35
37
Original file line number Diff line number Diff line change @@ -156,12 +156,13 @@ def association_names
156
156
end
157
157
end
158
158
159
- def allowed_sideloads
159
+ def allowed_sideloads ( namespace = nil )
160
160
return { } unless sideloading
161
161
162
+ namespace ||= context [ :namespace ]
162
163
sideloads = sideloading . to_hash [ :base ]
163
- if !sideload_whitelist . empty? && context [ : namespace]
164
- sideloads = Util ::IncludeParams . scrub ( sideloads , sideload_whitelist [ context [ : namespace] ] )
164
+ if !sideload_whitelist . empty? && namespace
165
+ sideloads = Util ::IncludeParams . scrub ( sideloads , sideload_whitelist [ namespace ] )
165
166
end
166
167
sideloads
167
168
end
Original file line number Diff line number Diff line change @@ -65,6 +65,38 @@ def config(obj)
65
65
end
66
66
end
67
67
68
+ describe '#wrap_context' do
69
+ before do
70
+ allow ( controller ) . to receive ( :action_name ) { 'index' }
71
+ end
72
+
73
+ it 'wraps in the resource context' do
74
+ controller . wrap_context do
75
+ expect ( controller . resource . context ) . to eq ( {
76
+ object : controller ,
77
+ namespace : :index
78
+ } )
79
+ end
80
+ end
81
+
82
+ context 'when the class does not have a resource' do
83
+ let ( :klass ) do
84
+ Class . new ( ApplicationController ) do
85
+ include JsonapiCompliable ::Base
86
+ self . _jsonapi_compliable = nil
87
+ end
88
+ end
89
+
90
+ let ( :instance ) { klass . new }
91
+
92
+ it 'does nothing' do
93
+ instance . wrap_context do
94
+ expect ( instance . resource ) . to be_nil
95
+ end
96
+ end
97
+ end
98
+ end
99
+
68
100
describe '#render_jsonapi' do
69
101
it 'is able to override options' do
70
102
author = Author . create! ( first_name : 'Stephen' , last_name : 'King' )
Original file line number Diff line number Diff line change 162
162
it { is_expected . to eq ( { } ) }
163
163
end
164
164
165
- # TODO only/as
166
165
context 'when sideloads' do
167
166
before do
168
167
klass . allow_sideload :foo do
177
176
instance . set_config ( klass . config )
178
177
end
179
178
180
- context 'and a namespace is set' do
179
+ context 'and a namespace is passed as an argument' do
180
+ around do |e |
181
+ instance . with_context ( { } , :show ) do
182
+ e . run
183
+ end
184
+ end
185
+
186
+ it 'uses that namespace' do
187
+ expect ( instance . allowed_sideloads ( :index ) ) . to eq ( {
188
+ foo : { bar : { } }
189
+ } )
190
+ end
191
+ end
192
+
193
+ context 'and a context namespace is set' do
181
194
around do |e |
182
195
instance . with_context ( { } , :show ) do
183
196
e . run
You can’t perform that action at this time.
0 commit comments