Memoize presenters in OrderInvoiceComparator

This is now twice as fast and triggers half the number of database queries if both comparison methods get called
This commit is contained in:
Matt-Yorkley
2023-05-09 15:24:10 +01:00
committed by Konrad
parent c6ec13443e
commit 3946e7a6f5

View File

@@ -13,14 +13,13 @@ class OrderInvoiceComparator
# We'll use a recursive BFS algorithm to find if the invoice is outdated
# the root will be the order
# On each node, we'll a list of relevant attributes that will be used on the comparison
different?(current_state_invoice.presenter, latest_invoice.presenter,
invoice_generation_selector)
different?(current_state_invoice, latest_invoice, invoice_generation_selector)
end
def can_update_latest_invoice?
return false if invoices.empty?
different?(current_state_invoice.presenter, latest_invoice.presenter, invoice_update_selector)
different?(current_state_invoice, latest_invoice, invoice_update_selector)
end
def different?(node1, node2, attributes_selector)
@@ -65,12 +64,12 @@ class OrderInvoiceComparator
private
def current_state_invoice
Invoice.new(
@current_state_invoice ||= Invoice.new(
order: order,
data: serialize_for_invoice,
date: Time.zone.today,
number: invoices.count + 1
)
).presenter
end
def invoices
@@ -78,7 +77,7 @@ class OrderInvoiceComparator
end
def latest_invoice
invoices.last
@latest_invoice ||= invoices.last.presenter
end
def serialize_for_invoice