Skip to content

Commit a2a0ae1

Browse files
authored
4949-Delete vendors if no purchases (#5051)
* adds delete button and route * delete vendors and specs * apply pr suggestions * fix spec * changes expectation to be more meaningful
1 parent 80d6cf7 commit a2a0ae1

File tree

6 files changed

+83
-13
lines changed

6 files changed

+83
-13
lines changed

app/controllers/vendors_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ def reactivate
8888
redirect_to vendors_path, notice: "#{vendor.business_name} has been reactivated."
8989
end
9090

91+
def destroy
92+
vendor = current_organization.vendors.find(params[:id])
93+
vendor.destroy
94+
if vendor.errors.any?
95+
errors = vendor.errors.full_messages.join("\n")
96+
redirect_to vendors_path, error: "#{vendor.business_name} could not be removed. \n#{errors}"
97+
return
98+
end
99+
100+
redirect_to vendors_path, notice: "#{vendor.business_name} has been removed."
101+
end
102+
91103
private
92104

93105
def vendor_params

app/models/vendor.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Vendor < ApplicationRecord
3535
.group(:id)
3636
}
3737

38+
before_destroy :check_for_purchases, prepend: true
39+
3840
def volume
3941
LineItem.where(
4042
itemizable_type: "Purchase",
@@ -49,4 +51,13 @@ def deactivate!
4951
def reactivate!
5052
update!(active: true)
5153
end
54+
55+
private
56+
57+
def check_for_purchases
58+
return if purchases.empty?
59+
60+
errors.add(:base, "Vendor has purchase items")
61+
throw(:abort)
62+
end
5263
end

app/views/vendors/_vendor_row.html.erb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
<%= view_button_to vendor_row %>
99
<%= edit_button_to edit_vendor_path(vendor_row) %>
1010
<% if vendor_row.active? %>
11-
<%= deactivate_button_to deactivate_vendor_path(vendor_row),
12-
text: 'Deactivate',
13-
confirm: confirm_deactivate_msg(vendor_row.business_name) %>
11+
<% if vendor_row&.purchases&.none? %>
12+
<%= delete_button_to vendor_path(vendor_row),
13+
text: 'Delete',
14+
confirm: confirm_delete_msg(vendor_row.business_name) %>
15+
<% else %>
16+
<%= deactivate_button_to deactivate_vendor_path(vendor_row),
17+
text: 'Deactivate',
18+
confirm: confirm_deactivate_msg(vendor_row.business_name) %>
19+
<% end %>
1420
<% else %>
1521
<%= reactivate_button_to reactivate_vendor_path(vendor_row),
1622
text: 'Reactivate',

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def set_up_flipper
172172
end
173173
end
174174

175-
resources :vendors, except: [:destroy] do
175+
resources :vendors do
176176
collection do
177177
post :import_csv
178178
end

spec/requests/vendors_requests_spec.rb

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@
1616
before { create(:vendor) }
1717

1818
context "html" do
19-
let!(:first_vendor) { create(:vendor, business_name: "Abc", organization: organization) }
19+
let!(:no_purchases_vendor) { create(:vendor, business_name: "Abc", organization: organization) }
20+
let(:purchase_vendor) { create(:vendor, business_name: "Xyz", organization: organization) }
2021
let!(:deactivated_vendor) { create(:vendor, business_name: "Deactivated", organization: organization, active: false) }
2122

2223
let(:response_format) { 'html' }
2324

2425
it { is_expected.to be_successful }
2526

27+
before do
28+
create(:purchase, :with_items, vendor: purchase_vendor)
29+
end
30+
2631
it "should have only activated vendor names" do
2732
subject
28-
expect(response.body).to include(first_vendor.business_name)
33+
expect(response.body).to include(no_purchases_vendor.business_name)
2934
expect(response.body).not_to include(deactivated_vendor.business_name)
3035
end
3136

32-
it "should have a deactivate button for each active vendor" do
33-
expect(subject.body.scan("Deactivate").count).to eq(2)
37+
it "should have a delete button for no_purchases_vendor and a deactivate button for purchase_vendor" do
38+
subject
39+
parsed_body = Nokogiri::HTML(response.body)
40+
no_purchases_vendor_row = parsed_body.css("tr").find { |row| row.text.include?(no_purchases_vendor.business_name) }
41+
purchase_vendor_row = parsed_body.css("tr").find { |row| row.text.include?(purchase_vendor.business_name) }
42+
43+
expect(no_purchases_vendor_row.at_css("a", text: "Delete")).to be_present
44+
expect(purchase_vendor_row.at_css("a", text: "Deactivate")).to be_present
3445
end
3546
end
3647

@@ -115,10 +126,28 @@
115126
end
116127

117128
describe "DELETE #destroy" do
118-
subject { delete vendor_path(id: create(:vendor)) }
119-
it "does not have a route for this" do
120-
subject
121-
expect(response.code).to eq('404')
129+
let!(:vendor) { create(:vendor, organization: organization) }
130+
131+
subject { delete vendor_path(id: vendor.id) }
132+
133+
context 'when vendor does not have purchase items' do
134+
it 'shoud delete the vendor' do
135+
expect { subject }.to change(Vendor, :count)
136+
expect(response).to redirect_to(vendors_path)
137+
follow_redirect!
138+
expect(response.body).to include("#{vendor.business_name} has been removed.")
139+
end
140+
end
141+
142+
context 'when vendor has purchase items' do
143+
before do
144+
create(:purchase, :with_items, vendor: vendor)
145+
end
146+
147+
it 'shoud not delete the vendor' do
148+
expect { subject }.not_to change(Vendor, :count)
149+
expect(response).to have_error(/ could not be removed/)
150+
end
122151
end
123152
end
124153

spec/system/vendor_system_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99
context "When a user views the index page" do
1010
before(:each) do
1111
@second = create(:vendor, business_name: "Bcd")
12+
create(:purchase, :with_items, vendor: @second)
1213
@first = create(:vendor, business_name: "Abc")
14+
create(:purchase, :with_items, vendor: @first)
15+
1316
@third = create(:vendor, business_name: "Cde")
17+
create(:purchase, :with_items, vendor: @third)
18+
19+
@last = create(:vendor, business_name: "Zzz")
20+
1421
visit vendors_path
1522
end
1623

1724
it "should have the vendor names in alphabetical order" do
18-
expect(page).to have_xpath("//table//tr", count: 4)
25+
expect(page).to have_xpath("//table//tr", count: 5)
1926
expect(page.find(:xpath, "//table/tbody/tr[1]/td[1]")).to have_content(@first.business_name)
2027
expect(page.find(:xpath, "//table/tbody/tr[3]/td[1]")).to have_content(@third.business_name)
2128
end
@@ -33,6 +40,11 @@
3340
expect { click_link "Reactivate", match: :first }.to change { @first.reload.active }.to(true)
3441
end
3542

43+
it "should delete when vendor does not have purchases items" do
44+
expect { click_link "Delete" }.to change(Vendor, :count).by(-1)
45+
expect(page).to have_content("#{@last.business_name} has been removed.")
46+
end
47+
3648
context "When using the include_inactive_vendors filter" do
3749
before(:each) do
3850
@active_vendor = create(:vendor, business_name: "Active Vendor", active: true)

0 commit comments

Comments
 (0)