diff options
author | Alessandro Desantis <desa.alessandro@gmail.com> | 2021-03-13 12:10:11 +0100 |
---|---|---|
committer | Noah Silvera <noah@super.gd> | 2021-06-16 14:54:40 -0700 |
commit | 95091a9e81c86a3e4f2f6238e79dccf374eec5fa (patch) | |
tree | 9ffb6e38d268c9d41aaf8a605761f8a829cda961 | |
parent | 5ad960c7588e8422dc1b3cc4577ec578bc30d54e (diff) |
Support order_recalculated event in Solidus < 2.11
Solidus < 2.11 doesn't support the `order_recalculated` event, so we
implement a decorator to support this feature in Solidus versions we
test against (2.10 and 2.9). We need this event to support sending order
updates to TaxJar, so we add it first.
Note that we include `SolidusSupport::EngineExtensions` so decorators
are loaded.
Co-authored-by: Nick Van Doorn <nick@super.gd>
Co-authored-by: Noah Silvera <noah@super.gd>
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb | 18 | ||||
-rw-r--r-- | lib/super_good/engine.rb | 2 | ||||
-rw-r--r-- | spec/models/spree/order_updater_spec.rb | 12 |
4 files changed, 34 insertions, 0 deletions
@@ -55,6 +55,8 @@ The extension provides currently two high level `calculator` classes that wrap t * tax calculator * tax rate calculator +The extension requires the `order_recalculated` event which is not supported on Solidus < 2.11, so this extension provides a [compatibility layer](app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb). + ### TaxCalculator `SuperGood::SolidusTaxjar::TaxCalculator` allows calculating the full tax breakdown for a given `Spree::Order`. The breakdown includes separate line items taxes and shipment taxes. diff --git a/app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb b/app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb new file mode 100644 index 0000000..1e169bc --- /dev/null +++ b/app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module SuperGood + module SolidusTaxjar + module Spree + module OrderUpdater + module FireRecalculatedEvent + def persist_totals + ::Spree::Event.fire 'order_recalculated', order: order + super + end + + ::Spree::OrderUpdater.prepend(self) if ::Spree.solidus_gem_version < Gem::Version.new('2.11.0') + end + end + end + end +end diff --git a/lib/super_good/engine.rb b/lib/super_good/engine.rb index 90f6a6e..b61d590 100644 --- a/lib/super_good/engine.rb +++ b/lib/super_good/engine.rb @@ -4,5 +4,7 @@ module SuperGoodSolidusTaxjar class Engine < Rails::Engine isolate_namespace Spree engine_name 'super_good_solidus_taxjar' + + include SolidusSupport::EngineExtensions end end diff --git a/spec/models/spree/order_updater_spec.rb b/spec/models/spree/order_updater_spec.rb new file mode 100644 index 0000000..69ecdc9 --- /dev/null +++ b/spec/models/spree/order_updater_spec.rb @@ -0,0 +1,12 @@ +RSpec.describe Spree::OrderUpdater do + describe '#update' do + it 'fires the order_recalculated event exactly once' do + stub_const('Spree::Event', class_spy(Spree::Event)) + order = create(:order) + + described_class.new(order).update + + expect(Spree::Event).to have_received(:fire).with('order_recalculated', order: order).once + end + end +end |