Skip to content

Commit 558e28a

Browse files
Prevent deletion of items that are part of a request (#4734)
1 parent b77c297 commit 558e28a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

app/models/item.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def has_inventory?(inventory = nil)
135135
end
136136
end
137137

138+
def in_request?
139+
Request.by_request_item_id(id).exists?
140+
end
141+
138142
def is_in_kit?(kits = nil)
139143
if kits
140144
kits.any? { |k| k.line_items.map(&:item_id).include?(id) }
@@ -147,7 +151,7 @@ def is_in_kit?(kits = nil)
147151
end
148152

149153
def can_delete?(inventory = nil, kits = nil)
150-
can_deactivate_or_delete?(inventory, kits) && line_items.none? && !barcode_count&.positive?
154+
can_deactivate_or_delete?(inventory, kits) && line_items.none? && !barcode_count&.positive? && !in_request?
151155
end
152156

153157
# @return [Boolean]

spec/models/item_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,16 @@
312312
expect(item.can_delete?).to eq(false)
313313
end
314314
end
315+
316+
context "in a request" do
317+
before do
318+
create(:request, request_items: [{"item_id" => item.id, "quantity" => 5}])
319+
end
320+
321+
it "should return false" do
322+
expect(item.can_delete?).to eq(false)
323+
end
324+
end
315325
end
316326

317327
describe '#deactivate!' do

0 commit comments

Comments
 (0)