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