From 2c2023df033c85b220a1769e044c566a1b162935 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 13 Feb 2020 16:30:31 +1100 Subject: [PATCH] Show last payment method in order confirmation If we had multiple failed payments and then a successful payment, the order confirmation was displaying the payment method of the first failed payment. That was confusing and is now changed to the last payment method. --- app/helpers/orders_helper.rb | 10 +++++++++ .../spree/shared/_order_details.html.haml | 4 ++-- .../shared/_order_details.html.haml_spec.rb | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 app/helpers/orders_helper.rb diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb new file mode 100644 index 0000000000..ae94325a83 --- /dev/null +++ b/app/helpers/orders_helper.rb @@ -0,0 +1,10 @@ +module OrdersHelper + def order_paid_via(order) + # `sort_by` avoids additional database queries when payments are loaded + # already. There is usually only one payment and this shouldn't cause + # any overhead compared to `order(:updated_at)`. + # + # Using `last` without sort is not deterministic. + order.payments.sort_by(&:updated_at).last.andand.payment_method + end +end diff --git a/app/views/spree/shared/_order_details.html.haml b/app/views/spree/shared/_order_details.html.haml index 5020566c58..0a42574432 100644 --- a/app/views/spree/shared/_order_details.html.haml +++ b/app/views/spree/shared/_order_details.html.haml @@ -13,9 +13,9 @@ .pad .text-big = t :order_payment - %strong= order.payments.first.andand.payment_method.andand.name + %strong= order_paid_via(order).andand.name %p.text-small.text-skinny.pre-line - %em= order.payments.first.andand.payment_method.andand.description + %em= order_paid_via(order).andand.description .order-summary.text-small %strong diff --git a/spec/views/spree/shared/_order_details.html.haml_spec.rb b/spec/views/spree/shared/_order_details.html.haml_spec.rb index e702b64f20..a12d16b1d3 100644 --- a/spec/views/spree/shared/_order_details.html.haml_spec.rb +++ b/spec/views/spree/shared/_order_details.html.haml_spec.rb @@ -29,4 +29,26 @@ describe "spree/shared/_order_details.html.haml" do expect(rendered).to have_content("Paying via: Barter→ing") end + + it "shows the last used payment method" do + first_payment = order.payments.first + second_payment = create( + :payment, + order: order, + payment_method: create(:payment_method, name: "Cash") + ) + third_payment = create( + :payment, + order: order, + payment_method: create(:payment_method, name: "Credit") + ) + first_payment.update_column(:updated_at, 3.days.ago) + second_payment.update_column(:updated_at, 2.days.ago) + third_payment.update_column(:updated_at, 1.day.ago) + order.payments.reload + + render + + expect(rendered).to have_content("Paying via: Credit") + end end