From 3fc8a15396df836141583ff9bf05a8f98047b58b Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Mon, 15 Jan 2024 13:16:10 -0600 Subject: [PATCH] Adds Order#order_email to show the order's email `spree_orders` table has the column `email` which stores the email of guest orders, this change intends to look first at this property then fallback to user's email. --- .../solidus_admin/orders/index/component.rb | 2 +- admin/spec/features/orders/index_spec.rb | 3 ++- core/app/models/spree/order.rb | 4 ++++ core/spec/models/spree/order_spec.rb | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/admin/app/components/solidus_admin/orders/index/component.rb b/admin/app/components/solidus_admin/orders/index/component.rb index 5c412db937e..08929335f19 100644 --- a/admin/app/components/solidus_admin/orders/index/component.rb +++ b/admin/app/components/solidus_admin/orders/index/component.rb @@ -143,7 +143,7 @@ def customer_column col: { class: "w-[400px]" }, header: :customer, data: ->(order) do - customer_email = order.user&.email + customer_email = order.order_email content_tag :div, String(customer_email) end } diff --git a/admin/spec/features/orders/index_spec.rb b/admin/spec/features/orders/index_spec.rb index ddc8f83f5ff..e0df6d7bb1c 100644 --- a/admin/spec/features/orders/index_spec.rb +++ b/admin/spec/features/orders/index_spec.rb @@ -6,11 +6,12 @@ before { sign_in create(:admin_user, email: 'admin@example.com') } it "lists products", :js do - create(:order, number: "R123456789", total: 19.99) + create(:order, number: "R123456789", total: 19.99, email: "customer@example.com") visit "/admin/orders" click_on "In Progress" + expect(page).to have_content("customer@example.com") expect(page).to have_content("R123456789") expect(page).to have_content("$19.99") expect(page).to be_axe_clean diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 5b04d0009c8..2cf3e5334fc 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -726,6 +726,10 @@ def validate_payments_attributes(attributes) end end + def order_email + email.presence || user&.email + end + private def process_payments_before_complete diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index eef3df40c5a..23ab7f86500 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -1832,4 +1832,26 @@ def validate(line_item) expect { subject }.to change { Spree::OrderPromotion.count }.from(1).to(0) end end + + describe "order_email" do + context "when a new order is created" do + it "returns nil" do + expect(Spree::Order.new.order_email).to eq(nil) + end + end + + context "when a logged in user creates an order" do + it "returns the customer's email" do + order = build(:order, user: build(:user, email: "customer@example.com")) + expect(order.order_email).to eq("customer@example.com") + end + end + + context "when order email is different than user's email" do + it "gives preference to order's email" do + order = build(:order, email: "buyer@example.com", user: build(:user, email: "customer@example.com")) + expect(order.order_email).to eq("buyer@example.com") + end + end + end end