From 37120896954c4f765c047c0ab0528dde1c41ae09 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Tue, 7 Jan 2020 16:48:54 +0100 Subject: Add CalculatorHelper for sharing logic This module includes helper methods that can be shared among different calculators. --- lib/super_good/solidus_taxjar/calculator_helper.rb | 44 ++++++++++++++++++++++ lib/super_good/solidus_taxjar/tax_calculator.rb | 35 +---------------- 2 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 lib/super_good/solidus_taxjar/calculator_helper.rb (limited to 'lib/super_good/solidus_taxjar') 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 -- cgit v1.2.3