From 7e760702f2a3d726607950c1e938d6d3dabcf2b8 Mon Sep 17 00:00:00 2001 From: Sofia Besenski Date: Fri, 6 Dec 2024 14:24:19 -0800 Subject: [PATCH] Introduce new InMemoryOrderAdjuster class for promotions This is just a stub for now, but we want to eventually introduce a class to handle running the promotion adjustments in memory. Co-authored-by: Adam Mueller Co-authored-by: Andrew Stewart Co-authored-by: Harmony Evangelina Co-authored-by: Kendra Riga Co-authored-by: Jared Norman Co-authored-by: Tom Van Manen Co-authored-by: Senem Soy Co-authored-by: Benjamin Willems --- .../models/spree/in_memory_order_updater.rb | 18 +++++++++++++++--- .../spree/in_memory_order_updater_spec.rb | 11 +++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/core/app/models/spree/in_memory_order_updater.rb b/core/app/models/spree/in_memory_order_updater.rb index fc59a8a29f..9624bff84d 100644 --- a/core/app/models/spree/in_memory_order_updater.rb +++ b/core/app/models/spree/in_memory_order_updater.rb @@ -117,7 +117,7 @@ def update_adjustments(persist:) # http://www.hmrc.gov.uk/vat/managing/charging/discounts-etc.htm#1 # It also fits the criteria for sales tax as outlined here: # http://www.boe.ca.gov/formspubs/pub113/ - update_promotions + update_promotions(persist:) update_taxes update_item_totals(persist:) end @@ -153,8 +153,12 @@ def update_adjustment_total(persist:) recalculate_order_total end - def update_promotions - Spree::Config.promotions.order_adjuster_class.new(order).call + def update_promotions(persist:) + if persist + Spree::Config.promotions.order_adjuster_class + else + InMemoryOrderAdjuster + end.new(order).call end def update_taxes @@ -239,5 +243,13 @@ def log_state_change(name) ) end end + + class InMemoryOrderAdjuster + def initialize(order) + end + + def call + end + end end end diff --git a/core/spec/models/spree/in_memory_order_updater_spec.rb b/core/spec/models/spree/in_memory_order_updater_spec.rb index 91ce3451bb..9794ca8995 100644 --- a/core/spec/models/spree/in_memory_order_updater_spec.rb +++ b/core/spec/models/spree/in_memory_order_updater_spec.rb @@ -166,6 +166,17 @@ module Spree line_item.additional_tax_total }.from(1).to(2) end + + context "when recalculating the order in memory" do + it "raises an error" do + order_adjuster = double + allow(order_adjuster).to receive(:call) { raise NotImplementedError } + allow(Spree::InMemoryOrderUpdater::InMemoryOrderAdjuster).to receive(:new).and_return(order_adjuster) + + expect{described_class.new(order).recalculate(persist: false)} + .to raise_error(NotImplementedError) + end + end end context 'with a custom tax_calculator_class' do