Skip to content

Commit 6de15f3

Browse files
authored
Merge pull request #5382 from jonny5/jk-purchase-from-inactive-vendor
Allow saving on purchase form with deactivated vendor
2 parents 520add0 + 7495cf6 commit 6de15f3

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

app/controllers/purchases_controller.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def edit
6565
@purchase.line_items.build
6666
@audit_performed_and_finalized = Audit.finalized_since?(@purchase, @purchase.storage_location_id)
6767

68-
load_form_collections
68+
load_form_collections(@purchase)
6969
end
7070

7171
def show
@@ -78,9 +78,11 @@ def update
7878
ItemizableUpdateService.call(itemizable: @purchase,
7979
params: purchase_params,
8080
event_class: PurchaseEvent)
81+
82+
flash[:success] = "Purchase updated successfully"
8183
redirect_to purchases_path
8284
rescue => e
83-
load_form_collections
85+
load_form_collections(@purchase)
8486
flash.now[:alert] = "Error updating purchase: #{e.message}"
8587
render "edit"
8688
end
@@ -100,10 +102,13 @@ def destroy
100102

101103
private
102104

103-
def load_form_collections
105+
def load_form_collections(purchase = nil)
104106
@storage_locations = current_organization.storage_locations.active.alphabetized
105107
@items = current_organization.items.active.alphabetized
106-
@vendors = current_organization.vendors.active.alphabetized
108+
109+
@vendors = current_organization.vendors.active
110+
@vendors = @vendors.or(Vendor.where(id: purchase.vendor_id)) if purchase
111+
@vendors = @vendors.alphabetized
107112
end
108113

109114
def purchase_params

spec/system/purchase_system_spec.rb

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,66 @@
218218
# Bug fix -- Issue #378
219219
# A user can view another organizations purchase
220220
context "Editing purchase" do
221-
it "A user can see purchased_from value" do
222-
purchase = create(:purchase, purchased_from: "Old Vendor", organization: organization)
223-
visit edit_purchase_path(purchase)
224-
expect(page).to have_content("Vendor (Old Vendor)")
221+
context "with an eligible purchase" do
222+
let(:purchase) { create(:purchase, purchased_from: "Old Vendor", organization: organization) }
223+
224+
it "can view the edit page and vendor is present" do
225+
visit edit_purchase_path(purchase)
226+
227+
expect(page).to have_content("Vendor (Old Vendor)")
228+
end
229+
230+
it "can save the purchase" do
231+
visit edit_purchase_path(purchase)
232+
click_button "Save"
233+
234+
expect(page).to have_content("Purchase updated successfully")
235+
expect(page.current_path).to eq(purchases_path)
236+
end
237+
238+
context "with an error saving the purchase" do
239+
it "can save the purchase from the update page " do
240+
visit edit_purchase_path(purchase)
241+
242+
fill_in "purchase[amount_spent]", with: nil
243+
244+
click_button "Save"
245+
246+
expect(page).to have_content("Error updating purchase")
247+
expect(page.current_path).to eq(purchase_path(purchase))
248+
249+
fill_in "purchase[amount_spent]", with: "10"
250+
251+
click_button "Save"
252+
253+
expect(page).to have_content("Purchase updated successfully")
254+
expect(page.current_path).to eq(purchases_path)
255+
end
256+
end
257+
258+
context "with a deactivated vendor" do
259+
let(:deactivated_vendor) { create(:vendor, active: false) }
260+
let(:purchase) { create(:purchase, vendor: deactivated_vendor) }
261+
262+
it "can save the purchase" do
263+
visit edit_purchase_path(purchase)
264+
click_button "Save"
265+
266+
expect(page).to have_content("Purchase updated successfully")
267+
expect(page.current_path).to eq(purchases_path)
268+
end
269+
end
225270
end
226271

227-
it "A user can view another organizations purchase" do
228-
purchase = create(:purchase, organization: create(:organization))
229-
visit edit_purchase_path(purchase)
230-
expect(page).to have_content("Still haven't found what you're looking for")
272+
context "with another organization's purchase" do
273+
let(:another_organization) { create(:organization) }
274+
let(:purchase) { create(:purchase, organization: another_organization) }
275+
276+
it "cannot view the edit page" do
277+
visit edit_purchase_path(purchase)
278+
279+
expect(page).to have_content("Still haven't found what you're looking for")
280+
end
231281
end
232282
end
233283

0 commit comments

Comments
 (0)