diff --git a/app/models/item.rb b/app/models/item.rb index 8f6b1eaec1..688bd34512 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -143,7 +143,7 @@ def is_in_kit?(kits = nil) end def can_delete?(inventory = nil, kits = nil) - can_deactivate_or_delete?(inventory, kits) && line_items.none? && !barcode_count&.positive? && !in_request? + can_deactivate_or_delete?(inventory, kits) && line_items.none? && !barcode_count&.positive? && !in_request? && kit.blank? end # @return [Boolean] diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index d726bed05e..787c668703 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -481,4 +481,47 @@ describe "versioning" do it { is_expected.to be_versioned } end + + describe "kit items" do + context "with kit and regular items" do + let(:organization) { create(:organization) } + let(:kit) { create(:kit, organization: organization) } + let(:kit_item) { create(:item, kit: kit, organization: organization) } + let(:regular_item) { create(:item, organization: organization) } + + describe "#can_delete?" do + it "returns false for kit items" do + expect(kit_item.can_delete?).to be false + end + + it "returns true for regular items" do + expect(regular_item.can_delete?).to be true + end + end + + describe "#deactivate!" do + it "deactivates both the kit item and its associated kit" do + kit_item.deactivate! + expect(kit_item.reload.active).to be false + expect(kit.reload.active).to be false + end + + it "only deactivates regular items" do + regular_item.deactivate! + expect(regular_item.reload.active).to be false + end + end + + describe "#validate_destroy" do + it "prevents deletion of kit items" do + expect { kit_item.destroy! }.to raise_error(ActiveRecord::RecordNotDestroyed) + expect(kit_item.errors[:base]).to include("Cannot delete item - it has already been used!") + end + + it "allows deletion of regular items" do + expect { regular_item.destroy! }.not_to raise_error + end + end + end + end end