Use OrderPaymentFinder methods instead of payments.last and payments.pending.last

This commit is contained in:
Luis Ramos
2020-04-18 11:56:52 +01:00
committed by Maikel Linke
parent 52becf6abc
commit d482cccefe
5 changed files with 27 additions and 15 deletions

View File

@@ -6,13 +6,23 @@ class OrderPaymentFinder
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`
# without order is not deterministic.
#
# 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)
last(@order.payments)
end
def last_pending_payment
last(@order.pending_payments)
end
private
# `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`
# without order is not deterministic.
#
# 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.
def last(payments)
payments.max_by(&:created_at)
end
end