Skip to content

Commit 7280547

Browse files
committed
send batch of related records to authorizer instead of iterating
1 parent f306f09 commit 7280547

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

lib/jsonapi/authorization/authorizing_processor.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,11 @@ def authorize_remove_to_many_relationships
208208
related_records = related_resources.map { |resource| resource._model }
209209
end
210210

211-
related_records.each do |related_record|
212-
authorizer.remove_to_many_relationship(
213-
source_record,
214-
related_record,
215-
params[:relationship_type]
216-
)
217-
end
211+
authorizer.remove_to_many_relationship(
212+
source_record,
213+
related_records,
214+
params[:relationship_type]
215+
)
218216
end
219217

220218
def authorize_remove_to_one_relationship

lib/jsonapi/authorization/default_pundit_authorizer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ def replace_to_many_relationship(source_record, new_related_records, relationshi
178178
# * +source_record+ - The record whose relationship is modified
179179
# * +related_record+ - The record which will be disassociated from +source_record+
180180
# * +relationship_type+ - The relationship type
181-
def remove_to_many_relationship(source_record, related_record, relationship_type)
181+
def remove_to_many_relationship(source_record, related_records, relationship_type)
182182
relationship_method = "remove_from_#{relationship_type}?"
183-
authorize_relationship_operation(source_record, relationship_method, related_record)
183+
authorize_relationship_operation(source_record, relationship_method, related_records)
184184
end
185185

186186
# <tt>DELETE /resources/:id/relationships/another-resource</tt>

spec/requests/relationship_operations_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282

283283
context 'unauthorized for remove_to_many_relationship' do
284284
before do
285-
disallow_operation('remove_to_many_relationship', article, kind_of(Comment), "comments")
285+
disallow_operation('remove_to_many_relationship', article, kind_of(Array), "comments")
286286
end
287287

288288
it { is_expected.to be_forbidden }
@@ -291,7 +291,7 @@
291291
context 'authorized for remove_to_many_relationship' do
292292
context 'not limited by policy scopes' do
293293
before do
294-
allow_operation('remove_to_many_relationship', article, kind_of(Comment), "comments")
294+
allow_operation('remove_to_many_relationship', article, kind_of(Array), "comments")
295295
end
296296

297297
it { is_expected.to be_successful }
@@ -306,7 +306,7 @@
306306
# behaviour in that case anyway, as it might be surprising.
307307
context 'limited by policy scope on articles' do
308308
before do
309-
allow_operation('remove_to_many_relationship', article, kind_of(Comment), :comments)
309+
allow_operation('remove_to_many_relationship', article, kind_of(Array), :comments)
310310
end
311311
let(:policy_scope) { Article.where.not(id: article.id) }
312312
it { is_expected.to be_not_found }

0 commit comments

Comments
 (0)