From f52d03fbf4548d99a0b63b81bba41b6bf6a4f764 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 26 Apr 2021 11:14:27 +0100 Subject: [PATCH] Refactor Order::Updater#update so we don't call update_totals twice Calling this method twice on every update is really expensive and not necessary. --- .../app/services/order_management/order/updater.rb | 4 +--- .../spec/services/order_management/order/updater_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/engines/order_management/app/services/order_management/order/updater.rb b/engines/order_management/app/services/order_management/order/updater.rb index e1bd346619..6fcbc6e2d6 100644 --- a/engines/order_management/app/services/order_management/order/updater.rb +++ b/engines/order_management/app/services/order_management/order/updater.rb @@ -19,6 +19,7 @@ module OrderManagement # object with callbacks (otherwise you will end up in an infinite recursion as the # associations try to save and then in turn try to call +update!+ again.) def update + update_all_adjustments update_totals if order.completed? @@ -27,9 +28,6 @@ module OrderManagement update_shipment_state end - update_all_adjustments - # update totals a second time in case updated adjustments have an effect on the total - update_totals persist_totals end diff --git a/engines/order_management/spec/services/order_management/order/updater_spec.rb b/engines/order_management/spec/services/order_management/order/updater_spec.rb index f85ccfa176..c2ddf1f9d7 100644 --- a/engines/order_management/spec/services/order_management/order/updater_spec.rb +++ b/engines/order_management/spec/services/order_management/order/updater_spec.rb @@ -132,8 +132,8 @@ module OrderManagement end end - it "updates totals twice" do - expect(updater).to receive(:update_totals).twice + it "updates totals once" do + expect(updater).to receive(:update_totals).once updater.update end