Skip to content

Commit f9ba10a

Browse files
authored
Adds a delete button to the purchase show page, hooked to the existing destroy action in the Purchase controller. Restricts deletion of a purchase to organization and super admin. (#1646)
1 parent 3ffef40 commit f9ba10a

File tree

4 files changed

+261
-191
lines changed

4 files changed

+261
-191
lines changed

app/controllers/purchases_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Provides full CRUD for Purchases, which are a way for Diaperbanks to track inventory that they purchase from vendors
22
class PurchasesController < ApplicationController
3+
before_action :authorize_admin, only: [:destroy]
4+
35
def index
46
setup_date_range_picker
57
@purchases = current_organization.purchases

app/views/purchases/show.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
</div>
7070
<div class="card-footer">
7171
<%= edit_button_to edit_purchase_path(@purchase), { text: "Make a correction", size: "md" } %>
72+
<%= delete_button_to purchase_path(@purchase), { size: "md", confirm: "Are you sure you want to permanently remove this purchase?" } if current_user.organization_admin? %>
7273
</div>
7374
</div>
7475
</div>

spec/requests/purchases_requests_spec.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{ organization_id: @organization.to_param }
66
end
77

8-
context "While signed in >" do
8+
context "While signed in as a user >" do
99
before do
1010
sign_in(@user)
1111
end
@@ -184,11 +184,47 @@
184184
end
185185
end
186186

187+
describe "DELETE #destroy" do
188+
# normal users are not authorized
189+
it "redirects to the dashboard" do
190+
delete purchase_path(default_params.merge(id: create(:purchase, organization: @organization)))
191+
expect(response).to redirect_to(dashboard_path)
192+
end
193+
194+
it "does not delete a purchase" do
195+
purchase = create(:purchase, purchased_from: "Google")
196+
expect { delete purchase_path(default_params.merge(id: purchase.id)) }.to_not change(Purchase, :count)
197+
end
198+
end
199+
end
200+
201+
context "While signed in as an organizational admin" do
202+
before do
203+
sign_in(@organization_admin)
204+
end
205+
187206
describe "DELETE #destroy" do
188207
it "redirects to the index" do
189208
delete purchase_path(default_params.merge(id: create(:purchase, organization: @organization)))
190209
expect(response).to redirect_to(purchases_path)
191210
end
211+
212+
it "decreases storage location inventory" do
213+
purchase = create(:purchase, :with_items, item_quantity: 10)
214+
storage_location = purchase.storage_location
215+
expect { delete purchase_path(default_params.merge(id: purchase.id)) }.to change { storage_location.size }.by(-10)
216+
end
217+
218+
it "deletes a purchase" do
219+
purchase = create(:purchase, purchased_from: "Google")
220+
expect { delete purchase_path(default_params.merge(id: purchase.id)) }.to change(Purchase, :count).by(-1)
221+
end
222+
223+
it "displays the proper flash notice" do
224+
purchase_id = create(:purchase, purchased_from: "Google").id.to_s
225+
delete purchase_path(default_params.merge(id: purchase_id))
226+
expect(response).to have_notice "Purchase #{purchase_id} has been removed!"
227+
end
192228
end
193229
end
194-
end
230+
end

0 commit comments

Comments
 (0)