Skip to content

Commit

Permalink
Adds Order#order_email to show the order's email
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
softr8 committed Jan 15, 2024
1 parent be710f3 commit 3fc8a15
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion admin/spec/features/orders/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
before { sign_in create(:admin_user, email: '[email protected]') }

it "lists products", :js do
create(:order, number: "R123456789", total: 19.99)
create(:order, number: "R123456789", total: 19.99, email: "[email protected]")

visit "/admin/orders"
click_on "In Progress"

expect(page).to have_content("[email protected]")
expect(page).to have_content("R123456789")
expect(page).to have_content("$19.99")
expect(page).to be_axe_clean
Expand Down
4 changes: 4 additions & 0 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]"))
expect(order.order_email).to eq("[email protected]")
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: "[email protected]", user: build(:user, email: "[email protected]"))
expect(order.order_email).to eq("[email protected]")
end
end
end
end

0 comments on commit 3fc8a15

Please sign in to comment.