Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer allowing deletion of kit items -- only deactivation #4837

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading