Skip to content

Commit

Permalink
Optimization works
Browse files Browse the repository at this point in the history
  • Loading branch information
nnande committed Nov 5, 2020
1 parent 40753d9 commit 60c15f9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app/models/concerns/spree_taxjar/taxable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/calculator/taxjar_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/models/spree/taxjar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 60c15f9

Please sign in to comment.