Skip to content

Commit 6b3c5c6

Browse files
someykokucielf
andauthored
4977 add ability to delete unused item categories (#5197)
* 4978 rework donation site import * 4978 rework donation site import * 4978 rework donation site import added tests fix * 4978 corrected donation sites csv file, contact name allowed to be blank, phone is not mandatory, fixed tests * 4978 added contact name, email, phone to donation_sites csv tests * Fix CSV import logic and error handling * Fix CSV import logic and error handling * Fix CSV import logic and error handling * Fixed Donation Sites CSV * minor updates * Geocoder implemeted in tests, revert of changes in schema and other minor requested changes * 4977_add_ability_to_delete_unused_item_categories * 4977_added_minor_fix_to_delete_item_category * 4977_added_request_spec_and_removed_unnecessary_tables_from_schema * Resolve merge conflict in organizations.rb * changes in schema * minor fixes * rearranged specs order in item category spec * replaced item categories request spec to request spec file * request spec moved to spec/request * moved redirect out of conditional --------- Co-authored-by: CL Fisher <[email protected]>
1 parent 4ee62b0 commit 6b3c5c6

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

app/controllers/item_categories_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ def update
3333
end
3434
end
3535

36+
def destroy
37+
@item_category = current_organization.item_categories.find_by(id: params[:id])
38+
if @item_category.items.exists?
39+
flash[:alert] = "Cannot delete item category because it has associated items."
40+
else
41+
@item_category.destroy
42+
flash[:notice] = "#{@item_category.name} has been deleted."
43+
end
44+
redirect_to items_path
45+
end
46+
3647
private
3748

3849
def item_category_params

app/models/item_category.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,16 @@ class ItemCategory < ApplicationRecord
1616

1717
belongs_to :organization
1818
has_many :items, -> { order(name: :asc) }, inverse_of: :item_category, dependent: :nullify
19-
has_many :partner_groups, dependent: :nullify
19+
has_and_belongs_to_many :partner_groups, dependent: :nullify
20+
21+
before_destroy :ensure_no_associated_partner_groups
22+
23+
private
24+
25+
def ensure_no_associated_partner_groups
26+
if partner_groups.exists?
27+
errors.add(:base, "Cannot delete item category with associated partner groups")
28+
throw(:abort)
29+
end
30+
end
2031
end

app/views/items/_item_categories.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
<% end %>
2828
</ul>
2929
</td>
30-
<td>
30+
<td class="text-right">
3131
<%= view_button_to item_category_path(item_category) %>
3232
<%= edit_button_to edit_item_category_path(item_category.id) %>
33+
<%= delete_button_to(item_category_path(item_category.id), { confirm: confirm_delete_msg(item_category.name) }) if (item_category.items.count <= 0) %>
3334
</td>
3435
</tr>
3536
<% end %>

db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@
324324
t.bigint "old_partner_id"
325325
t.boolean "archived", default: false
326326
t.index ["partner_id"], name: "index_families_on_partner_id"
327-
end
327+
end
328328

329329
create_table "flipper_features", force: :cascade do |t|
330330
t.string "key", null: false

spec/models/item_category_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,24 @@
2828
describe "versioning" do
2929
it { is_expected.to be_versioned }
3030
end
31+
32+
describe "delete" do
33+
let(:item_category) { create(:item_category) }
34+
let(:partner_group) { create(:partner_group, item_categories: [item_category]) }
35+
36+
before do
37+
partner_group
38+
end
39+
40+
it "should not delete if associated with partner groups" do
41+
expect(item_category.partner_groups).not_to be_empty
42+
expect { item_category.destroy }.not_to change(ItemCategory, :count)
43+
expect(item_category.errors.full_messages).to include("Cannot delete item category with associated partner groups")
44+
end
45+
46+
it "should delete if not associated with partner groups" do
47+
item_category.partner_groups.destroy_all
48+
expect { item_category.destroy }.to change(ItemCategory, :count).by(-1)
49+
end
50+
end
3151
end

spec/requests/item_categories_requests_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,29 @@
9393
end
9494
end
9595
end
96+
97+
describe "DELETE #destroy" do
98+
let!(:item_category) { create(:item_category, organization: organization) }
99+
100+
context "when the item category has no associated items" do
101+
it "destroys the requested item category" do
102+
expect {
103+
delete item_category_url(id: item_category.id)
104+
}.to change(ItemCategory, :count).by(-1)
105+
expect(response).to redirect_to(items_path)
106+
end
107+
end
108+
109+
context "when the item category has associated items" do
110+
let!(:item) { create(:item, item_category: item_category) }
111+
112+
it "does not destroy the item category and shows an error" do
113+
expect {
114+
delete item_category_url(id: item_category.id)
115+
}.not_to change(ItemCategory, :count)
116+
expect(response).to redirect_to(items_path)
117+
expect(flash[:alert]).to eq("Cannot delete item category because it has associated items.")
118+
end
119+
end
120+
end
96121
end

0 commit comments

Comments
 (0)