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

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 default_location(source_object)
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: 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: default_location(@purchase),
wrapper: :input_group %>
</div>
</div>
Expand Down
47 changes: 47 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
16 changes: 15 additions & 1 deletion 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 Expand Up @@ -254,7 +268,7 @@
expect(response.body).to include("<li class=\"breadcrumb-item\">\n <a href=\"#\">Editing #{original_source}")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_source}\">#{edited_source}</option>")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_source_drive.id}\">#{edited_source_drive_name}</option>")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_source_drive_participant.id}\">#{edited_source_drive_participant_business_name}</option>")
expect(response.body).to include("<option value=\"#{edited_source_drive_participant.id}\">#{edited_source_drive_participant_business_name}</option>")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_storage_location.id}\">#{edited_storage_location_name}</option>")
expect(response.body).to include(edited_comment)
expect(response.body).to include("value=\"#{edited_money}\" type=\"text\" name=\"donation[money_raised_in_dollars]")
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
Loading