Skip to content

Commit

Permalink
Add Spree::Config.tax_adjuster extension point
Browse files Browse the repository at this point in the history
To allow easier customization of tax calculation in extensions or
applications.
  • Loading branch information
jordan-brough committed Sep 28, 2016
1 parent 1f20ce8 commit 097a6b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Solidus 2.1.0 (master, unreleased)

* Added Spree::Config.tax_adjuster_class

To allow easier customization of tax calculation in extensions or
applications.

* Remove `is_default` boolean from `Spree::Price` model

This boolean used to mean "the price to be used". With the new
Expand All @@ -19,7 +24,7 @@
* Removals

* Removed deprecated method `Spree::TaxRate.adjust` (not to be confused with
Spree::TaxRate#adjust) in favor of `Spree::Tax::OrderAdjuster`.
Spree::TaxRate#adjust) in favor of `Spree::Config.tax_adjuster_class`.

https://github.com/solidusio/solidus/pull/1462

Expand Down
10 changes: 10 additions & 0 deletions core/app/models/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ def add_payment_sources_to_wallet_class
@add_payment_sources_to_wallet_class ||= Spree::Wallet::AddPaymentSourcesToWallet
end

# Allows providing your own class for calculating taxes on an order.
#
# @!attribute [rw] tax_adjuster_class
# @return [Class] a class with the same public interfaces as
# Spree::Tax::OrderAdjuster
attr_writer :tax_adjuster_class
def tax_adjuster_class
@tax_adjuster_class ||= Spree::Tax::OrderAdjuster
end

def static_model_preferences
@static_model_preferences ||= Spree::Preferences::StaticModelPreferences.new
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def line_item_options_match(line_item, options)
# include taxes then price adjustments are created instead.
# @deprecated This now happens during #update!
def create_tax_charge!
Spree::Tax::OrderAdjuster.new(self).adjust!
Spree::Config.tax_adjuster_class.new(self).adjust!
end
deprecate create_tax_charge!: :update!, deprecator: Spree::Deprecation

Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def update_order_promotions
end

def update_taxes
Spree::Tax::OrderAdjuster.new(order).adjust!
Spree::Config.tax_adjuster_class.new(order).adjust!

[*line_items, *shipments].each do |item|
tax_adjustments = item.adjustments.select(&:tax?)
Expand Down
17 changes: 17 additions & 0 deletions core/spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,23 @@ def create_adjustment(label, amount)
}.from(1).to(2)
end
end

context 'with a custom tax_adjuster_class' do
let(:custom_adjuster_class) { double }
let(:custom_adjuster_instance) { double }

before do
order # generate this first so we can expect it
Spree::Config.tax_adjuster_class = custom_adjuster_class
end

it 'uses the configured class' do
expect(custom_adjuster_class).to receive(:new).with(order).at_least(:once).and_return(custom_adjuster_instance)
expect(custom_adjuster_instance).to receive(:adjust!).at_least(:once)

order.update!
end
end
end
end

Expand Down

0 comments on commit 097a6b3

Please sign in to comment.