From 60c15f9a2d2f5060d9b1cbbfafa2bbf4db9304f7 Mon Sep 17 00:00:00 2001 From: Nathaniel Nande Date: Thu, 5 Nov 2020 10:06:06 +0100 Subject: [PATCH] Optimization works --- app/models/concerns/spree_taxjar/taxable.rb | 5 ++++- app/models/spree/calculator/taxjar_calculator.rb | 2 +- app/models/spree/taxjar.rb | 6 +++--- .../spree/admin/taxjar_settings_controller_spec.rb | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/spree_taxjar/taxable.rb b/app/models/concerns/spree_taxjar/taxable.rb index 4f3f2a3..014804c 100644 --- a/app/models/concerns/spree_taxjar/taxable.rb +++ b/app/models/concerns/spree_taxjar/taxable.rb @@ -5,7 +5,10 @@ module Taxable private def taxjar_applicable?(order) - ::Spree::TaxRate.match(order.tax_zone).any? { |rate| rate.calculator_type == "Spree::Calculator::TaxjarCalculator" } + ::Spree::TaxRate.joins(:calculator) + .match(order.tax_zone) + .where(spree_calculators: { type: ::Spree::Calculator::TaxjarCalculator.to_s } ) + .any? end end end diff --git a/app/models/spree/calculator/taxjar_calculator.rb b/app/models/spree/calculator/taxjar_calculator.rb index 06a390d..d48fa42 100644 --- a/app/models/spree/calculator/taxjar_calculator.rb +++ b/app/models/spree/calculator/taxjar_calculator.rb @@ -96,7 +96,7 @@ def cache_key(order, item, address) if item.is_a?(Spree::LineItem) [Spree::LineItem.to_s, order.id, item.id, address.state_id, address.zipcode, item.taxable_amount, :amount_to_collect] else - [Spree::Shipment.to_s, order.id, item.id, address.state_id, address.zipcode, item.cost, item.adjustments.select { |adjustment| adjustment.source_type != Spree::TaxRate.to_s }.map(&:amount).sum.to_f, :amount_to_collect] + [Spree::Shipment.to_s, order.id, item.id, address.state_id, address.zipcode, item.cost, item.adjustments.where.not(source_type: Spree::TaxRate.to_s).sum(:amount), :amount_to_collect] end end end diff --git a/app/models/spree/taxjar.rb b/app/models/spree/taxjar.rb index 229504e..e054cf9 100644 --- a/app/models/spree/taxjar.rb +++ b/app/models/spree/taxjar.rb @@ -118,7 +118,7 @@ def tax_params end def taxable_line_items_params - @order.line_items.map do |item| + @order.line_items.includes(:variant, :product, :tax_category).map do |item| { id: item.id, quantity: item.quantity, @@ -189,7 +189,7 @@ def shipment_tax_params end def line_item_params - @order.line_items.map do |item| + @order.line_items.includes(:variant, :product, :tax_category).map do |item| unit_price = item.taxable_amount / item.quantity { quantity: item.quantity, @@ -210,7 +210,7 @@ def discount_weightage(item, unit_price) end def adjustments_total(adjustments) - adjustments.select { |adjustment| adjustment.source_type != Spree::TaxRate.to_s }.map(&:amount).sum.to_f + adjustments.where.not(source_type: Spree::TaxRate.to_s).sum(:amount) end def client_params diff --git a/spec/controllers/spree/admin/taxjar_settings_controller_spec.rb b/spec/controllers/spree/admin/taxjar_settings_controller_spec.rb index 8d08275..8b0825a 100644 --- a/spec/controllers/spree/admin/taxjar_settings_controller_spec.rb +++ b/spec/controllers/spree/admin/taxjar_settings_controller_spec.rb @@ -21,7 +21,7 @@ def send_request end it "assigns @preferences_api" do - expect(assigns[:preferences_api]).to eq([:taxjar_api_key, :taxjar_enabled, :taxjar_debug_enabled]) + expect(assigns[:preferences_api]).to eq([:taxjar_api_key, :taxjar_enabled, :taxjar_debug_enabled, :taxjar_sandbox_environment_enabled]) end it "renders edit template" do