Skip to content

Commit b6a05fb

Browse files
committed
Add support for lambda and callables for allow_include?
1 parent cffb8c1 commit b6a05fb

3 files changed

Lines changed: 61 additions & 9 deletions

File tree

lib/jsonapi/relationship.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,19 @@ def polymorphic_type
102102
"#{name}_type" if polymorphic?
103103
end
104104

105-
def allow_include?
106-
if @allow_include.nil?
107-
JSONAPI.configuration.default_allow_include_to_one
105+
def allow_include?(context = nil)
106+
strategy = if @allow_include.nil?
107+
JSONAPI.configuration.default_allow_include_to_one
108+
else
109+
@allow_include
110+
end
111+
112+
if !!strategy == strategy #check for boolean
113+
return strategy
114+
elsif strategy.is_a?(Symbol) || strategy.is_a?(String)
115+
parent_resource.send(strategy, context)
108116
else
109-
@allow_include
117+
strategy.call(context)
110118
end
111119
end
112120
end
@@ -124,12 +132,21 @@ def initialize(name, options = {})
124132
end
125133
end
126134

127-
def allow_include?
128-
if @allow_include.nil?
129-
JSONAPI.configuration.default_allow_include_to_one
135+
def allow_include?(context = nil)
136+
strategy = if @allow_include.nil?
137+
JSONAPI.configuration.default_allow_include_to_one
138+
else
139+
@allow_include
140+
end
141+
142+
if !!strategy == strategy #check for boolean
143+
return strategy
144+
elsif strategy.is_a?(Symbol) || strategy.is_a?(String)
145+
parent_resource.send(strategy, context)
130146
else
131-
@allow_include
147+
strategy.call(context)
132148
end
149+
133150
end
134151
end
135152
end

lib/jsonapi/request_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def check_include(resource_klass, include_parts)
330330

331331
relationship = resource_klass._relationship(relationship_name)
332332
if relationship && format_key(relationship_name) == include_parts.first
333-
unless relationship.allow_include?
333+
unless relationship.allow_include?(context)
334334
fail JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type), include_parts.first)
335335
end
336336

test/unit/resource/relationship_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
require File.expand_path('../../../test_helper', __FILE__)
22

3+
class LambdaBlogPostsResource < JSONAPI::Resource
4+
model_name 'Post'
5+
6+
has_one :author, allow_include: -> (context) { context[:admin] }
7+
has_many :comments, allow_include: -> (context) { context[:admin] }
8+
end
9+
10+
class CallableBlogPostsResource < JSONAPI::Resource
11+
model_name 'Post'
12+
13+
has_one :author, allow_include: :is_admin
14+
has_many :comments, allow_include: :is_admin
15+
16+
def self.is_admin(context)
17+
context[:admin]
18+
end
19+
end
20+
321
class HasOneRelationshipTest < ActiveSupport::TestCase
422

523
def test_polymorphic_type
@@ -72,4 +90,21 @@ def test_allow_include_set_overrides_to_config_to_many
7290
ensure
7391
JSONAPI.configuration = original_config
7492
end
93+
94+
def test_allow_include_set_by_lambda
95+
assert LambdaBlogPostsResource._relationship(:author).allow_include?(admin: true)
96+
refute LambdaBlogPostsResource._relationship(:author).allow_include?(admin: false)
97+
98+
assert LambdaBlogPostsResource._relationship(:comments).allow_include?(admin: true)
99+
refute LambdaBlogPostsResource._relationship(:comments).allow_include?(admin: false)
100+
end
101+
102+
def test_allow_include_set_by_callable
103+
assert CallableBlogPostsResource._relationship(:author).allow_include?(admin: true)
104+
refute CallableBlogPostsResource._relationship(:author).allow_include?(admin: false)
105+
106+
assert CallableBlogPostsResource._relationship(:comments).allow_include?(admin: true)
107+
refute CallableBlogPostsResource._relationship(:comments).allow_include?(admin: false)
108+
end
109+
75110
end

0 commit comments

Comments
 (0)