diff options
author | Nicholas Van Doorn <nick@super.gd> | 2021-06-21 15:43:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-21 15:43:02 -0700 |
commit | f64364a5b00a2464b5b3ba3662a90045b3821ea7 (patch) | |
tree | 5f16f9d74b8d96a72f72ae4e21cd09a9fcf08cca | |
parent | 155bf053d08bda60782cf3ee37c52850465fcb6d (diff) | |
parent | 8c20dd5ba7a7fb472247a8d2308678ccba5aa003 (diff) |
Merge pull request #80 from nvandoorn/77-support-order-recalculated
Support order_recalculated event in Solidus < 2.11
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-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-- | lib/super_good/solidus_taxjar/addresses.rb | 6 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/calculator_helper.rb | 2 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_calculator.rb | 8 | ||||
-rw-r--r-- | spec/models/spree/order_updater_spec.rb | 12 |
8 files changed, 43 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index dfd7239..f3fb0d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - [#69](https://github.com/SuperGoodSoft/solidus_taxjar/pull/69) Lock ExecJS version - [#37](https://github.com/SuperGoodSoft/solidus_taxjar/pull/37) Added a basic Taxjar settings admin interface which displays placeholder text. - [#64](https://github.com/SuperGoodSoft/solidus_taxjar/pull/64) Provide Spree::Address.address2 to TaxJar address validation if it is present. +- [#80](https://github.com/SuperGoodSoft/solidus_taxjar/pull/80) Support order_recalculated event in < 2.11 ## v0.18.1 @@ -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/lib/super_good/solidus_taxjar/addresses.rb b/lib/super_good/solidus_taxjar/addresses.rb index 7fdf852..409d0d3 100644 --- a/lib/super_good/solidus_taxjar/addresses.rb +++ b/lib/super_good/solidus_taxjar/addresses.rb @@ -20,7 +20,7 @@ module SuperGood return if taxjar_address.nil? - Spree::Address.immutable_merge(spree_address, { + ::Spree::Address.immutable_merge(spree_address, { country: us, # TaxJar only supports the US currently. state: state(taxjar_address.state), zipcode: taxjar_address.zip, @@ -31,7 +31,7 @@ module SuperGood def possibilities(spree_address) taxjar_addresses(spree_address).map { |taxjar_address| - Spree::Address.immutable_merge(spree_address, { + ::Spree::Address.immutable_merge(spree_address, { country: us, # TaxJar only supports the US currently. state: state(taxjar_address.state), zipcode: taxjar_address.zip, @@ -52,7 +52,7 @@ module SuperGood end def us - Spree::Country.find_by iso: "US" + ::Spree::Country.find_by iso: "US" end def state(abbr) diff --git a/lib/super_good/solidus_taxjar/calculator_helper.rb b/lib/super_good/solidus_taxjar/calculator_helper.rb index edf0754..9145658 100644 --- a/lib/super_good/solidus_taxjar/calculator_helper.rb +++ b/lib/super_good/solidus_taxjar/calculator_helper.rb @@ -4,7 +4,7 @@ module SuperGood extend ActiveSupport::Concern def incomplete_address?(address) - return true if address.is_a?(Spree::Tax::TaxLocation) + return true if address.is_a?(::Spree::Tax::TaxLocation) [ address.address1, diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index 74e6f9b..1c9a4da 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -17,7 +17,7 @@ module SuperGood cache do next no_tax unless taxjar_breakdown - Spree::Tax::OrderTax.new( + ::Spree::Tax::OrderTax.new( order_id: order.id, line_item_taxes: line_item_taxes, shipment_taxes: shipment_taxes @@ -41,7 +41,7 @@ module SuperGood # orders aren't going to have a huge number of line items. spree_line_item = order.line_items.find { |li| li.id == spree_line_item_id } - Spree::Tax::ItemTax.new( + ::Spree::Tax::ItemTax.new( item_id: spree_line_item_id, label: line_item_tax_label(taxjar_line_item, spree_line_item), tax_rate: tax_rate, @@ -102,7 +102,7 @@ module SuperGood end def no_tax - Spree::Tax::OrderTax.new( + ::Spree::Tax::OrderTax.new( order_id: order.id, line_item_taxes: [], shipment_taxes: [] @@ -110,7 +110,7 @@ module SuperGood end def tax_rate - Spree::TaxRate.find_by(name: "Sales Tax") + ::Spree::TaxRate.find_by(name: "Sales Tax") end def cache_key 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 |