File tree Expand file tree Collapse file tree 4 files changed +73
-2
lines changed Expand file tree Collapse file tree 4 files changed +73
-2
lines changed Original file line number Diff line number Diff line change 15
15
require "jsonapi_compliable/scope/filter"
16
16
require "jsonapi_compliable/util/include_params"
17
17
require "jsonapi_compliable/util/field_params"
18
+ require "jsonapi_compliable/util/scoping"
18
19
19
20
module JsonapiCompliable
20
21
autoload :Base , 'jsonapi_compliable/base'
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ module Base
7
7
8
8
included do
9
9
class_attribute :_jsonapi_compliable
10
+ attr_reader :_jsonapi_scoped
11
+
10
12
before_action :parse_fieldsets!
11
13
after_action :reset_scope_flag
12
14
end
@@ -45,8 +47,7 @@ def parse_fieldsets!
45
47
end
46
48
47
49
def render_ams ( scope , opts = { } )
48
- opts [ :scope ] = true unless opts [ :scope ] == false
49
- scope = jsonapi_scope ( scope ) if !@_jsonapi_scoped && opts . delete ( :scope )
50
+ scope = jsonapi_scope ( scope ) if Util ::Scoping . apply? ( self , scope , opts . delete ( :scope ) )
50
51
options = default_ams_options
51
52
options [ :include ] = forced_includes || Util ::IncludeParams . scrub ( self )
52
53
options [ :json ] = scope
Original file line number Diff line number Diff line change
1
+ module JsonapiCompliable
2
+ module Util
3
+ class Scoping
4
+ def self . apply? ( controller , object , force )
5
+ return false if force == false
6
+ return true if !controller . _jsonapi_scoped && object . is_a? ( ActiveRecord ::Relation )
7
+
8
+ already_scoped = !!controller . _jsonapi_scoped
9
+ is_activerecord = object . is_a? ( ActiveRecord ::Base )
10
+ is_activerecord_array = object . is_a? ( Array ) && object [ 0 ] . is_a? ( ActiveRecord ::Base )
11
+
12
+ if [ already_scoped , is_activerecord , is_activerecord_array ] . any?
13
+ false
14
+ else
15
+ true
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ RSpec . describe JsonapiCompliable ::Util ::Scoping do
4
+ describe '.apply?' do
5
+ let ( :force ) { true }
6
+ let ( :controller ) { double . as_null_object }
7
+ let ( :object ) { Author . all }
8
+
9
+ subject { described_class . apply? ( controller , object , force ) }
10
+
11
+ before do
12
+ allow ( controller ) . to receive ( :_jsonapi_scoped ) { nil }
13
+ end
14
+
15
+ it { is_expected . to be ( true ) }
16
+
17
+ context 'when forcing no scope' do
18
+ let ( :force ) { false }
19
+
20
+ it { is_expected . to be ( false ) }
21
+ end
22
+
23
+ context 'when controller has already scoped' do
24
+ before do
25
+ allow ( controller ) . to receive ( :_jsonapi_scoped ) { true }
26
+ end
27
+
28
+ it { is_expected . to be ( false ) }
29
+ end
30
+
31
+ context 'when a PORO' do
32
+ let ( :object ) { Class . new }
33
+
34
+ it { is_expected . to be ( true ) }
35
+ end
36
+
37
+ context 'when object is an ActiveRecord instance' do
38
+ let ( :object ) { Author . new }
39
+
40
+ it { is_expected . to be ( false ) }
41
+ end
42
+
43
+ context 'when object is an array of ActiveRecord instances' do
44
+ let ( :object ) { [ Author . new ] }
45
+
46
+ it { is_expected . to be ( false ) }
47
+ end
48
+ end
49
+ end
You can’t perform that action at this time.
0 commit comments