Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make purchase helper pick default storage location #4722

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ def storage_location_for_source(source_object)
end
current_organization.default_storage_location
end

def set_default_location(source_object)
lenikadali marked this conversation as resolved.
Show resolved Hide resolved
current_organization.default_storage_location || source_object.storage_location_id.presence || current_organization.intake_location
end
end
4 changes: 0 additions & 4 deletions app/helpers/purchases_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/views/donations/_donation_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<div class="col-xs-8 col-md-6 col-lg-3">
<%= 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",
Expand Down
2 changes: 1 addition & 1 deletion app/views/purchases/_purchase_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</div>
Expand Down
60 changes: 60 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,64 @@ 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.set_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.set_default_location(purchase) }

it { is_expected.to eq(1) }
end
end

describe "#set_default_location for donation" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to copy the same test for all things that have storage locations - all we're doing is referencing the storage_location_id of that thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opted to keep one test as per commit 2716706 so that we guarantee the functionality of the method and any subsequent changes to it.

Let me know if that is in keeping with what you were suggesting or your proposal was to remove the test altogether.

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.set_default_location(donation) }

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(:donation) { build(:donation, storage_location_id: nil) }

before do
allow(helper).to receive(:current_organization).and_return(organization)
end

subject { helper.set_default_location(donation) }

it { is_expected.to eq(1) }
end
end
end
20 changes: 0 additions & 20 deletions spec/helpers/purchases_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/requests/donations_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }

Expand Down Expand Up @@ -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) }
Expand Down
5 changes: 5 additions & 0 deletions spec/requests/purchases_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }

Expand Down Expand Up @@ -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
lenikadali marked this conversation as resolved.
Show resolved Hide resolved

describe "POST#create" do
Expand Down