Skip to content

Commit e1e3d11

Browse files
authored
Merge pull request #4837 from MichaScant/no_allow_deletion_of_kit_items
No longer allowing deletion of kit items -- only deactivation
2 parents de827ae + 27dc2c5 commit e1e3d11

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

app/models/item.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def is_in_kit?(kits = nil)
143143
end
144144

145145
def can_delete?(inventory = nil, kits = nil)
146-
can_deactivate_or_delete?(inventory, kits) && line_items.none? && !barcode_count&.positive? && !in_request?
146+
can_deactivate_or_delete?(inventory, kits) && line_items.none? && !barcode_count&.positive? && !in_request? && kit.blank?
147147
end
148148

149149
# @return [Boolean]

spec/models/item_spec.rb

+43
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,47 @@
481481
describe "versioning" do
482482
it { is_expected.to be_versioned }
483483
end
484+
485+
describe "kit items" do
486+
context "with kit and regular items" do
487+
let(:organization) { create(:organization) }
488+
let(:kit) { create(:kit, organization: organization) }
489+
let(:kit_item) { create(:item, kit: kit, organization: organization) }
490+
let(:regular_item) { create(:item, organization: organization) }
491+
492+
describe "#can_delete?" do
493+
it "returns false for kit items" do
494+
expect(kit_item.can_delete?).to be false
495+
end
496+
497+
it "returns true for regular items" do
498+
expect(regular_item.can_delete?).to be true
499+
end
500+
end
501+
502+
describe "#deactivate!" do
503+
it "deactivates both the kit item and its associated kit" do
504+
kit_item.deactivate!
505+
expect(kit_item.reload.active).to be false
506+
expect(kit.reload.active).to be false
507+
end
508+
509+
it "only deactivates regular items" do
510+
regular_item.deactivate!
511+
expect(regular_item.reload.active).to be false
512+
end
513+
end
514+
515+
describe "#validate_destroy" do
516+
it "prevents deletion of kit items" do
517+
expect { kit_item.destroy! }.to raise_error(ActiveRecord::RecordNotDestroyed)
518+
expect(kit_item.errors[:base]).to include("Cannot delete item - it has already been used!")
519+
end
520+
521+
it "allows deletion of regular items" do
522+
expect { regular_item.destroy! }.not_to raise_error
523+
end
524+
end
525+
end
526+
end
484527
end

0 commit comments

Comments
 (0)