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!
2828
2929 # TODO pass controller and action name here to guard
3030 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
3335 end
3436 end
3537
Original file line number Diff line number Diff line change @@ -156,12 +156,13 @@ def association_names
156156 end
157157 end
158158
159- def allowed_sideloads
159+ def allowed_sideloads ( namespace = nil )
160160 return { } unless sideloading
161161
162+ namespace ||= context [ :namespace ]
162163 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 ] )
165166 end
166167 sideloads
167168 end
Original file line number Diff line number Diff line change @@ -65,6 +65,38 @@ def config(obj)
6565 end
6666 end
6767
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+
68100 describe '#render_jsonapi' do
69101 it 'is able to override options' do
70102 author = Author . create! ( first_name : 'Stephen' , last_name : 'King' )
Original file line number Diff line number Diff line change 162162 it { is_expected . to eq ( { } ) }
163163 end
164164
165- # TODO only/as
166165 context 'when sideloads' do
167166 before do
168167 klass . allow_sideload :foo do
177176 instance . set_config ( klass . config )
178177 end
179178
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
181194 around do |e |
182195 instance . with_context ( { } , :show ) do
183196 e . run
You can’t perform that action at this time.
0 commit comments