Skip to content

Commit 7791629

Browse files
author
Lee Richmond
committed
Enhance/Extract auto-scoping logic
1 parent e99c542 commit 7791629

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

lib/jsonapi_compliable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require "jsonapi_compliable/scope/filter"
1616
require "jsonapi_compliable/util/include_params"
1717
require "jsonapi_compliable/util/field_params"
18+
require "jsonapi_compliable/util/scoping"
1819

1920
module JsonapiCompliable
2021
autoload :Base, 'jsonapi_compliable/base'

lib/jsonapi_compliable/base.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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

spec/util/scoping_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

0 commit comments

Comments
 (0)