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 1515require "jsonapi_compliable/scope/filter"
1616require "jsonapi_compliable/util/include_params"
1717require "jsonapi_compliable/util/field_params"
18+ require "jsonapi_compliable/util/scoping"
1819
1920module JsonapiCompliable
2021 autoload :Base , 'jsonapi_compliable/base'
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ module Base
77
88 included do
99 class_attribute :_jsonapi_compliable
10+ attr_reader :_jsonapi_scoped
11+
1012 before_action :parse_fieldsets!
1113 after_action :reset_scope_flag
1214 end
@@ -45,8 +47,7 @@ def parse_fieldsets!
4547 end
4648
4749 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 ) )
5051 options = default_ams_options
5152 options [ :include ] = forced_includes || Util ::IncludeParams . scrub ( self )
5253 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