Extract payment method logic from OrderPaymentFinder into the orders helper

This commit is contained in:
Luis Ramos
2020-04-18 11:44:46 +01:00
committed by Maikel Linke
parent 3dff11e405
commit 52becf6abc
4 changed files with 15 additions and 7 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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  

View File

@@ -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