From 5ad960c7588e8422dc1b3cc4577ec578bc30d54e Mon Sep 17 00:00:00 2001 From: Noah Silvera Date: Wed, 16 Jun 2021 14:53:16 -0700 Subject: Specify all spree namespaces in lib as root namespaces An upcoming commit will add a decorator that creates the SuperGood::SolidusTaxjar::Spree namespace, which will change which module all existing Spree instances in the lib folder reference. We should force them to look for the root Spree namespace so tests do not break. --- lib/super_good/solidus_taxjar/addresses.rb | 6 +++--- lib/super_good/solidus_taxjar/calculator_helper.rb | 2 +- lib/super_good/solidus_taxjar/tax_calculator.rb | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) 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 -- cgit v1.2.3 From 95091a9e81c86a3e4f2f6238e79dccf374eec5fa Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Sat, 13 Mar 2021 12:10:11 +0100 Subject: 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 Co-authored-by: Noah Silvera --- README.md | 2 ++ .../spree/order_updater/fire_recalculated_event.rb | 18 ++++++++++++++++++ lib/super_good/engine.rb | 2 ++ spec/models/spree/order_updater_spec.rb | 12 ++++++++++++ 4 files changed, 34 insertions(+) create mode 100644 app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb create mode 100644 spec/models/spree/order_updater_spec.rb diff --git a/README.md b/README.md index fc6c7bb..d16074b 100644 --- a/README.md +++ b/README.md @@ -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 -- cgit v1.2.3 From 8c20dd5ba7a7fb472247a8d2308678ccba5aa003 Mon Sep 17 00:00:00 2001 From: Nicholas Van Doorn Date: Wed, 9 Jun 2021 12:21:17 -0700 Subject: Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index abca6b1..cbbd9d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,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 -- cgit v1.2.3