diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 6f7075f680..9c16d713ad 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -124,4 +124,8 @@ def storage_location_for_source(source_object)
end
current_organization.default_storage_location
end
+
+ def default_location(source_object)
+ current_organization.default_storage_location || source_object.storage_location_id.presence || current_organization.intake_location
+ end
end
diff --git a/app/helpers/purchases_helper.rb b/app/helpers/purchases_helper.rb
index ea6a5b4ffc..bfa08f91fb 100644
--- a/app/helpers/purchases_helper.rb
+++ b/app/helpers/purchases_helper.rb
@@ -3,8 +3,4 @@ module PurchasesHelper
def purchased_from(purchase)
purchase.purchased_from.nil? ? "" : "(#{purchase.purchased_from})"
end
-
- def new_purchase_default_location(purchase)
- purchase.storage_location_id.presence || current_organization.intake_location
- end
end
diff --git a/app/views/donations/_donation_form.html.erb b/app/views/donations/_donation_form.html.erb
index c7de6c4921..0f19413164 100644
--- a/app/views/donations/_donation_form.html.erb
+++ b/app/views/donations/_donation_form.html.erb
@@ -39,7 +39,7 @@
<%= f.association :product_drive_participant,
collection: @product_drive_participants,
- selected: donation_form.product_drive_participant_id,
+ selected: set_default_location(@donation),
include_blank: true,
label_method: lambda { |x| "#{x.try(:business_name) }" },
label: "Product Drive Participant",
diff --git a/app/views/purchases/_purchase_form.html.erb b/app/views/purchases/_purchase_form.html.erb
index f0765e4dd2..486efd1790 100644
--- a/app/views/purchases/_purchase_form.html.erb
+++ b/app/views/purchases/_purchase_form.html.erb
@@ -18,7 +18,7 @@
collection: @storage_locations,
label: "Storage Location",
error: "Where is it being stored?",
- selected: new_purchase_default_location(@purchase),
+ selected: set_default_location(@purchase),
wrapper: :input_group %>
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index 0648985cad..cd7e87ddef 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -134,4 +134,51 @@ def current_organization; end
end
end
end
+
+ describe "#set_default_location for purchase" do
+ helper do
+ def current_organization; end
+ end
+
+ before(:each) do
+ allow(helper).to receive(:current_organization).and_return(organization)
+ end
+
+ context "returns storage_location_id if present" do
+ let(:purchase) { build(:purchase, storage_location_id: 2) }
+ subject { helper.default_location(purchase) }
+
+ it { is_expected.to eq(2) }
+ end
+
+ context "returns current_organization intake_location if storage_location_id is not present" do
+ let(:organization) { build(:organization, intake_location: 1) }
+ let(:purchase) { build(:purchase, storage_location_id: nil) }
+
+ before do
+ allow(helper).to receive(:current_organization).and_return(organization)
+ end
+
+ subject { helper.default_location(purchase) }
+
+ it { is_expected.to eq(1) }
+ end
+ end
+
+ describe "#default_location for source_object" do
+ helper do
+ def current_organization; end
+ end
+
+ before(:each) do
+ allow(helper).to receive(:current_organization).and_return(organization)
+ end
+
+ context "returns storage_location_id if present" do
+ let(:donation) { build(:donation, storage_location_id: 2) }
+ subject { helper.default_location(donation) }
+
+ it { is_expected.to eq(2) }
+ end
+ end
end
diff --git a/spec/helpers/purchases_helper_spec.rb b/spec/helpers/purchases_helper_spec.rb
index 631a7b03ed..420f1d4705 100644
--- a/spec/helpers/purchases_helper_spec.rb
+++ b/spec/helpers/purchases_helper_spec.rb
@@ -4,25 +4,5 @@
def current_organization
end
end
-
- context "returns purchase storage_location_id if present" do
- let(:purchase) { build(:purchase, storage_location_id: 2) }
- subject { helper.new_purchase_default_location(purchase) }
-
- it { is_expected.to eq(2) }
- end
-
- context "returns current_organization intake_location if purchase storage_location_id is not present" do
- let(:organization) { build(:organization, intake_location: 1) }
- let(:purchase) { build(:purchase, storage_location_id: nil) }
-
- before do
- allow(helper).to receive(:current_organization).and_return(organization)
- end
-
- subject { helper.new_purchase_default_location(purchase) }
-
- it { is_expected.to eq(1) }
- end
end
end
diff --git a/spec/requests/donations_requests_spec.rb b/spec/requests/donations_requests_spec.rb
index 5213bb28b1..cfaea749c3 100644
--- a/spec/requests/donations_requests_spec.rb
+++ b/spec/requests/donations_requests_spec.rb
@@ -1,5 +1,6 @@
RSpec.describe "Donations", type: :request do
let(:organization) { create(:organization) }
+ let(:storage_location) { create(:storage_location, name: "Pawane Location", organization: organization) }
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }
@@ -86,6 +87,19 @@
end
end
+ describe "GET #new" do
+ subject do
+ organization.update!(default_storage_location: storage_location)
+ get new_donation_path
+ response
+ end
+
+ it { is_expected.to be_successful }
+ it "should include the storage location name" do
+ expect(subject.body).to include("Pawane Location")
+ end
+ end
+
describe "GET #print" do
let(:item) { create(:item) }
let!(:donation) { create(:donation, :with_items, item: item) }
diff --git a/spec/requests/purchases_requests_spec.rb b/spec/requests/purchases_requests_spec.rb
index 478e36f961..f1167cf9b2 100644
--- a/spec/requests/purchases_requests_spec.rb
+++ b/spec/requests/purchases_requests_spec.rb
@@ -1,5 +1,6 @@
RSpec.describe "Purchases", type: :request do
let(:organization) { create(:organization) }
+ let(:storage_location) { create(:storage_location, name: "Pawane Location", organization: organization) }
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }
@@ -48,11 +49,15 @@
describe "GET #new" do
subject do
+ organization.update!(default_storage_location: storage_location)
get new_purchase_path
response
end
it { is_expected.to be_successful }
+ it "should include the storage location name" do
+ expect(subject.body).to include("Pawane Location")
+ end
end
describe "POST#create" do