Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Reduce number of SQL queries in Tax Adjuster (#13091)
Damian Legawiecefb200652129
diff --git a/core/app/models/spree/adjustable/adjuster/tax.rb b/core/app/models/spree/adjustable/adjuster/tax.rbindex b0c51e36df..31711b8d8c 100644--- a/core/app/models/spree/adjustable/adjuster/tax.rb+++ b/core/app/models/spree/adjustable/adjuster/tax.rb@@ -4,8 +4,8 @@ module Spreeclass Tax < Spree::Adjustable::Adjuster::Basedef updatetax = adjustments.tax- included_tax_total = tax.is_included.reload.map(&:update!).compact.sum- additional_tax_total = tax.additional.reload.map(&:update!).compact.sum+ included_tax_total = tax.find_all(&:included?).map(&:update!).compact.sum || 0+ additional_tax_total = tax.find_all(&:additional?).map(&:update!).compact.sum || 0update_totals(included_tax_total, additional_tax_total)enddiff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rbindex 0adc28298a..70c3540f86 100644--- a/core/app/models/spree/adjustment.rb+++ b/core/app/models/spree/adjustment.rb@@ -92,6 +92,10 @@ module Spreesource_type == 'Spree::TaxRate'end+ def additional?+ !included?+ end+# Passing a target here would always be recommended as it would avoid# hitting the database again and would ensure you're compute values over# the specific object amount passed here.diff --git a/core/app/models/spree/tax_rate.rb b/core/app/models/spree/tax_rate.rbindex 757daaf65f..6d9cec5dd1 100644--- a/core/app/models/spree/tax_rate.rb+++ b/core/app/models/spree/tax_rate.rb@@ -115,6 +115,14 @@ module Spreecompute(item)end+ def included?+ included_in_price+ end++ def additional?+ !included_in_price+ end+privatedef labeldiff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rbindex 6981e1519b..e5ca8c16ea 100644--- a/core/spec/models/spree/promotion_spec.rb+++ b/core/spec/models/spree/promotion_spec.rb@@ -809,6 +809,7 @@ describe Spree::Promotion, type: :model dopromo.activate(order: order)order.update_with_updater!+ line_item.reloadexpect(line_item.adjustments.size).to eq(1)expect(order.adjustment_total).to eq(-5)