diff options
author | andrea longhi <andrea@spaghetticode.it> | 2020-01-07 16:48:54 +0100 |
---|---|---|
committer | andrea longhi <andrea@spaghetticode.it> | 2020-01-14 09:44:59 +0100 |
commit | 37120896954c4f765c047c0ab0528dde1c41ae09 (patch) | |
tree | 3c010263d5c58f4021d5e2aa6cdcb3804e663cfc /lib | |
parent | 98677696e6f43432c05eb581dcaec292beeafaaa (diff) |
Add CalculatorHelper for sharing logic
This module includes helper methods that can be shared among different
calculators.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/super_good/solidus_taxjar.rb | 1 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/calculator_helper.rb | 44 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_calculator.rb | 35 |
3 files changed, 46 insertions, 34 deletions
diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb index 9318181..8841acd 100644 --- a/lib/super_good/solidus_taxjar.rb +++ b/lib/super_good/solidus_taxjar.rb @@ -5,6 +5,7 @@ require 'taxjar' require "super_good/solidus_taxjar/version" require "super_good/solidus_taxjar/api_params" require "super_good/solidus_taxjar/api" +require "super_good/solidus_taxjar/calculator_helper" require "super_good/solidus_taxjar/tax_calculator" require "super_good/solidus_taxjar/discount_calculator" diff --git a/lib/super_good/solidus_taxjar/calculator_helper.rb b/lib/super_good/solidus_taxjar/calculator_helper.rb new file mode 100644 index 0000000..89dedc0 --- /dev/null +++ b/lib/super_good/solidus_taxjar/calculator_helper.rb @@ -0,0 +1,44 @@ +module SuperGood + module SolidusTaxJar + module CalculatorHelper + extend ActiveSupport::Concern + + class_methods do + def default_api + ::SuperGood::SolidusTaxJar::API.new + end + end + + def incomplete_address?(address) + return true if address.is_a?(Spree::Tax::TaxLocation) + + [ + address.address1, + address.city, + address&.state&.abbr || address.state_name, + address.zipcode, + address.country.iso + ].any?(&:blank?) + end + + def taxable_address?(address) + SuperGood::SolidusTaxJar.taxable_address_check.(address) + end + + def cache + if !Rails.env.test? + Rails.cache.fetch( + cache_key, + expires_in: SuperGood::SolidusTaxJar.cache_duration + ) { yield } + else + yield + end + end + + def exception_handler + SuperGood::SolidusTaxJar.exception_handler + end + end + end +end diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index c7d44a9..7fac1bd 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -1,9 +1,7 @@ module SuperGood module SolidusTaxJar class TaxCalculator - def self.default_api - ::SuperGood::SolidusTaxJar::API.new - end + include CalculatorHelper def initialize(order, api: self.class.default_api) @order = order @@ -115,33 +113,14 @@ module SuperGood Spree::TaxRate.find_by(name: "Sales Tax") end - def cache - if !Rails.env.test? - Rails.cache.fetch( - cache_key, - expires_in: SuperGood::SolidusTaxJar.cache_duration - ) { yield } - else - yield - end - end - def cache_key SuperGood::SolidusTaxJar.cache_key.(order) end - def exception_handler - SuperGood::SolidusTaxJar.exception_handler - end - def taxable_order?(order) SuperGood::SolidusTaxJar.taxable_order_check.(order) end - def taxable_address?(address) - SuperGood::SolidusTaxJar.taxable_address_check.(address) - end - def shipping_tax_label(shipment, shipping_tax) SuperGood::SolidusTaxJar.shipping_tax_label_maker.( shipment, @@ -152,18 +131,6 @@ module SuperGood def line_item_tax_label(taxjar_line_item, spree_line_item) SuperGood::SolidusTaxJar.line_item_tax_label_maker.(taxjar_line_item, spree_line_item) end - - def incomplete_address?(tax_address) - return true if tax_address.is_a?(Spree::Tax::TaxLocation) - - [ - tax_address.address1, - tax_address.city, - tax_address&.state&.abbr || tax_address.state_name, - tax_address.zipcode, - tax_address.country.iso - ].any?(&:blank?) - end end end end |