diff --git a/app/views/donations/_donation_form.html.erb b/app/views/donations/_donation_form.html.erb index 977ef7319c..4c1bddf0e5 100644 --- a/app/views/donations/_donation_form.html.erb +++ b/app/views/donations/_donation_form.html.erb @@ -41,7 +41,7 @@ collection: @product_drive_participants, selected: donation_form.product_drive_participant_id, include_blank: true, - label_method: lambda { |x| "#{x.try(:business_name) }" }, + label_method: lambda { |x| "#{x.try(:business_name).presence || x.try(:contact_name)}" }, label: "Product Drive Participant", error: "Which product drive participant was this from?", wrapper: :input_group %> diff --git a/app/views/product_drive_participants/create.js.erb b/app/views/product_drive_participants/create.js.erb index 4fb11ddbe1..9f291950f7 100644 --- a/app/views/product_drive_participants/create.js.erb +++ b/app/views/product_drive_participants/create.js.erb @@ -2,6 +2,6 @@ $("#modal_new").modal("hide"); $("#donation_product_drive_participant_id").empty(); $("#donation_product_drive_participant_id"). -html('<%= j options_from_collection_for_select(current_organization.product_drive_participants, :id, :business_name) %>'); +html('<%= j options_from_collection_for_select(current_organization.product_drive_participants, :id, lambda { |p| p.business_name.present? ? p.business_name : p.contact_name }) %>'); $("#donation_product_drive_participant_id").append(''); $("#donation_product_drive_participant_id").val('<%= @product_drive_participant[:id] %>'); diff --git a/spec/system/donation_system_spec.rb b/spec/system/donation_system_spec.rb index e94e8789a7..25a6909e95 100644 --- a/spec/system/donation_system_spec.rb +++ b/spec/system/donation_system_spec.rb @@ -161,6 +161,7 @@ create(:donation_site, organization: organization) create(:product_drive, organization: organization) create(:product_drive_participant, organization: organization) + create(:product_drive_participant, organization: organization, contact_name: "contact without business name", business_name: "") create(:manufacturer, organization: organization) organization.reload end @@ -248,6 +249,12 @@ end.to change { Donation.count }.by(1) end + it "Displays ProductDrive Participants sources by business name then contact name" do + select Donation::SOURCES[:product_drive], from: "donation_source" + select ProductDrive.first.name, from: "donation_product_drive_id" + expect(page).to have_select('donation_product_drive_participant_id', with_options: ['contact without business name']) + end + it "Allows User to create a Product Drive from donation" do select Donation::SOURCES[:product_drive], from: "donation_source" select "---Create new Product Drive---", from: "donation_product_drive_id" @@ -273,9 +280,30 @@ fill_in "product_drive_participant_email", with: "123@mail.ru" fill_in "product_drive_participant_comment", with: "test comment" click_on "product-drive-participant-submit" + expect(page).to have_select('donation_product_drive_participant_id', with_options: ['businesstest']) + select "businesstest", from: "donation_product_drive_participant_id" end + # seems like a duplicate check but this update happens via JS, so we have to test that code works too + it "Renders ProductDrive Participants sources by business name then contact name after creating a participant" do + select Donation::SOURCES[:product_drive], from: "donation_source" + select "---Create new Participant---", from: "donation_product_drive_participant_id" + + find(".modal-content") + expect(page).to have_content("New Product Drive Participant") + + fill_in "product_drive_participant_business_name", with: "" + fill_in "product_drive_participant_contact_name", with: "2nd contact without business name" + fill_in "product_drive_participant_email", with: "1233@mail.ru" + fill_in "product_drive_participant_comment", with: "test comment" + click_on "product-drive-participant-submit" + + select ProductDrive.first.name, from: "donation_product_drive_id" + # note that I'm not explicitly testing the business name here, this is handled in the previous test + expect(page).to have_select('donation_product_drive_participant_id', with_options: ['2nd contact without business name']) + end + it "Allows User to create a donation for a Manufacturer source" do select Donation::SOURCES[:manufacturer], from: "donation_source" expect(page).to have_xpath("//select[@id='donation_manufacturer_id']")