Skip to content

Commit

Permalink
Merge pull request #4837 from MichaScant/no_allow_deletion_of_kit_items
Browse files Browse the repository at this point in the history
No longer allowing deletion of kit items -- only deactivation
  • Loading branch information
cielf authored Dec 12, 2024
2 parents de827ae + 27dc2c5 commit e1e3d11
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
43 changes: 43 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e1e3d11

Please sign in to comment.