From a978e992bfa7dbd2f2c70f04b78a41c2ab96a158 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 19 Dec 2018 14:42:38 +1100 Subject: [PATCH] Remove `tax_rates` shortcut from adjustment Becoming less dependent on Spree, using our own namespace, keeping decorators small. --- app/helpers/checkout_helper.rb | 2 +- app/models/spree/adjustment_decorator.rb | 5 ----- app/models/spree/order_decorator.rb | 2 +- app/services/tax_rate_finder.rb | 10 ++++++++++ lib/open_food_network/enterprise_fee_applicator.rb | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index bc885bd5dc..4d64735cfc 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -54,7 +54,7 @@ module CheckoutHelper end def display_adjustment_tax_rates(adjustment) - tax_rates = adjustment.tax_rates + tax_rates = TaxRateFinder.tax_rates_of(adjustment) tax_rates.map { |tr| number_to_percentage(tr.amount * 100, :precision => 1) }.join(", ") end diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb index 61e968d844..e7ffcf41f0 100644 --- a/app/models/spree/adjustment_decorator.rb +++ b/app/models/spree/adjustment_decorator.rb @@ -40,11 +40,6 @@ module Spree included_tax > 0 end - # @return [Array] - def tax_rates - TaxRateFinder.new.tax_rates(originator, source, amount, included_tax) - end - def self.without_callbacks skip_callback :save, :after, :update_adjustable skip_callback :destroy, :after, :update_adjustable diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 5baf95fa1c..be6fcd8fa1 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -293,7 +293,7 @@ Spree::Order.class_eval do def tax_adjustment_totals tax_adjustments.each_with_object(Hash.new) do |adjustment, hash| - tax_rates = adjustment.tax_rates + tax_rates = TaxRateFinder.tax_rates_of(adjustment) tax_rates_hash = Hash[tax_rates.collect do |tax_rate| tax_amount = tax_rates.one? ? adjustment.included_tax : tax_rate.compute_tax(adjustment.amount) [tax_rate, tax_amount] diff --git a/app/services/tax_rate_finder.rb b/app/services/tax_rate_finder.rb index 2ce475fc5a..dc6bdd83a2 100644 --- a/app/services/tax_rate_finder.rb +++ b/app/services/tax_rate_finder.rb @@ -2,6 +2,16 @@ # For example a packaging fee may contain VAT. This service finds the VAT rate # for the tax included in the packaging fee. class TaxRateFinder + # @return [Array] + def self.tax_rates_of(adjustment) + new.tax_rates( + adjustment.originator, + adjustment.source, + adjustment.amount, + adjustment.included_tax + ) + end + # @return [Array] def tax_rates(originator, source, amount, included_tax) find_associated_tax_rate(originator, source) || diff --git a/lib/open_food_network/enterprise_fee_applicator.rb b/lib/open_food_network/enterprise_fee_applicator.rb index e213204047..cafe67ea7b 100644 --- a/lib/open_food_network/enterprise_fee_applicator.rb +++ b/lib/open_food_network/enterprise_fee_applicator.rb @@ -32,7 +32,7 @@ module OpenFoodNetwork end def adjustment_tax(adjustable, adjustment) - tax_rates = adjustment.tax_rates + tax_rates = TaxRateFinder.tax_rates_of(adjustment) tax_rates.select(&:included_in_price).sum do |rate| rate.compute_tax adjustment.amount