diff --git a/app/helpers/admin/orders_helper.rb b/app/helpers/admin/orders_helper.rb index 9e16d27ed5..cbc31d6ba7 100644 --- a/app/helpers/admin/orders_helper.rb +++ b/app/helpers/admin/orders_helper.rb @@ -9,5 +9,9 @@ module Admin adjustment.originator_type == "Spree::ShippingMethod" end end + + def last_payment_method(order) + OrderPaymentFinder.new(order).last_payment&.payment_method + end end end diff --git a/app/services/order_payment_finder.rb b/app/services/order_payment_finder.rb index c28d0e139a..754be34a0f 100644 --- a/app/services/order_payment_finder.rb +++ b/app/services/order_payment_finder.rb @@ -1,7 +1,11 @@ # frozen_string_literal: true -module OrderPaymentFinder - def self.last_payment_method(order) +class OrderPaymentFinder + def initialize(order) + @order = order + end + + def last_payment # `max_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(:created_at).last`. Using `last` @@ -9,6 +13,6 @@ module OrderPaymentFinder # # We are not using `updated_at` because all payments are touched when the # order is updated and then all payments have the same `updated_at` value. - order.payments.max_by(&:created_at)&.payment_method + @order.payments.max_by(&:created_at) end end diff --git a/app/views/spree/order_mailer/_payment.html.haml b/app/views/spree/order_mailer/_payment.html.haml index 60fc1056f4..85ad234cda 100644 --- a/app/views/spree/order_mailer/_payment.html.haml +++ b/app/views/spree/order_mailer/_payment.html.haml @@ -8,7 +8,7 @@ = t :email_payment_summary %h4 = t :email_payment_method - %strong= OrderPaymentFinder.last_payment_method(@order)&.name + %strong= last_payment_method(@order)&.name %p - %em= OrderPaymentFinder.last_payment_method(@order)&.description + %em= last_payment_method(@order)&.description %p   diff --git a/app/views/spree/shared/_order_details.html.haml b/app/views/spree/shared/_order_details.html.haml index 22c8ccdf8f..f4861d8148 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= OrderPaymentFinder.last_payment_method(order)&.name + %strong= last_payment_method(order)&.name %p.text-small.text-skinny.pre-line - %em= OrderPaymentFinder.last_payment_method(order)&.description + %em= last_payment_method(order)&.description .order-summary.text-small %strong