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
Use in-memory objects in order updater to reduce number of SQL queries (#13031)
Damian Legawiec313326a0d5f2
diff --git a/core/app/models/concerns/spree/adjustment_source.rb b/core/app/models/concerns/spree/adjustment_source.rbindex f78c1208bd..1bde610b10 100644--- a/core/app/models/concerns/spree/adjustment_source.rb+++ b/core/app/models/concerns/spree/adjustment_source.rb@@ -14,12 +14,12 @@ module Spreereturn if amount.zero?- adjustments.new(- adjustable: adjustable,+ adjustable.adjustments.new(amount: amount,included: included,label: label,- order: order+ order: order,+ source: self).saveenddiff --git a/core/app/models/spree/order_merger.rb b/core/app/models/spree/order_merger.rbindex dfc4722249..03432b8c91 100644--- a/core/app/models/spree/order_merger.rb+++ b/core/app/models/spree/order_merger.rb@@ -57,7 +57,7 @@ module Spreecurrent_line_item.quantity += other_order_line_item.quantityhandle_error(current_line_item) unless current_line_item.saveelse- other_order_line_item.order_id = order.id+ order.line_items << other_order_line_itemother_order_line_item.adjustments.update_all(order_id: order.id)handle_error(other_order_line_item) unless other_order_line_item.saveend@@ -69,9 +69,7 @@ module Spreeenddef persist_merge- updater.update_item_count- updater.update_item_total- updater.persist_totals+ updater.updateenddef handle_gift_card(other_order)diff --git a/core/app/models/spree/order_updater.rb b/core/app/models/spree/order_updater.rbindex 0de95e1d28..aaebdd9692 100644--- a/core/app/models/spree/order_updater.rb+++ b/core/app/models/spree/order_updater.rb@@ -69,7 +69,7 @@ module Spreeenddef update_shipment_total- order.shipment_total = shipments.sum(:cost)+ order.shipment_total = shipments.to_a.sum(&:cost)update_order_totalend@@ -97,7 +97,7 @@ module Spreeenddef update_item_total- order.item_total = line_items.sum('price * quantity')+ order.item_total = line_items.to_a.sum(&:amount)update_order_totalenddiff --git a/core/app/models/spree/promotion/actions/free_shipping.rb b/core/app/models/spree/promotion/actions/free_shipping.rbindex 7e59d6d9d4..94e1a1c00c 100644--- a/core/app/models/spree/promotion/actions/free_shipping.rb+++ b/core/app/models/spree/promotion/actions/free_shipping.rb@@ -18,12 +18,12 @@ module Spreedef create_adjustment(order, adjustable, included = false)amount = compute_amount(adjustable)- adjustments.new(- adjustable: adjustable,+ adjustable.adjustments.new(amount: amount,included: included,label: label,- order: order+ order: order,+ source: self).saveendenddiff --git a/core/app/services/spree/cart/recalculate.rb b/core/app/services/spree/cart/recalculate.rbindex 2c6df9c311..2096a2d7ea 100644--- a/core/app/services/spree/cart/recalculate.rb+++ b/core/app/services/spree/cart/recalculate.rb@@ -4,11 +4,12 @@ module Spreeprepend Spree::ServiceModule::Basedef call(order:, line_item:, line_item_created: false, options: {})- order.remove_gift_card if order.gift_card.present?order_updater = ::Spree::OrderUpdater.new(order)- order.payments.store_credits.checkout.destroy_all- order_updater.update+ order.remove_gift_card if order.gift_card.present?+ order.payments.store_credits.checkout.destroy_all if order.payments.store_credits.checkout.any?+ order_updater.update_totals+ order_updater.persist_totalsshipment = options[:shipment]if shipment.present?diff --git a/core/app/services/spree/cart/remove_line_item.rb b/core/app/services/spree/cart/remove_line_item.rbindex 046465f6c0..c7e6ffc321 100644--- a/core/app/services/spree/cart/remove_line_item.rb+++ b/core/app/services/spree/cart/remove_line_item.rb@@ -6,12 +6,11 @@ module Spreedef call(order:, line_item:, options: nil)options ||= {}ActiveRecord::Base.transaction do- line_item.destroy!+ order.line_items.destroy(line_item)Spree::Dependencies.cart_recalculate_service.constantize.new.call(order: order,line_item: line_item,options: options)end- order.reloadsuccess(line_item)endenddiff --git a/core/app/services/spree/line_items/destroy.rb b/core/app/services/spree/line_items/destroy.rbindex cdda16c077..722e3684b1 100644--- a/core/app/services/spree/line_items/destroy.rb+++ b/core/app/services/spree/line_items/destroy.rb@@ -8,7 +8,7 @@ module Spreeorder = line_item.orderActiveRecord::Base.transaction do- line_item.destroy!+ order.line_items.destroy(line_item)recalculate_service.call(order: order, line_item: line_item, options: options)endsuccess(line_item)diff --git a/core/spec/models/spree/order/state_machine_spec.rb b/core/spec/models/spree/order/state_machine_spec.rbindex 57ff5ea0e8..a09909ea89 100644--- a/core/spec/models/spree/order/state_machine_spec.rb+++ b/core/spec/models/spree/order/state_machine_spec.rb@@ -114,7 +114,7 @@ describe Spree::Order, type: :model doallow(shipments).to receive_messages ready: []allow(shipments).to receive_messages pending: []allow(shipments).to receive_messages shipped: []- allow(shipments).to receive(:sum).with(:cost).and_return(shipment.cost)+ allow(shipments).to receive_message_chain(:to_a, :sum).and_return(shipment.cost)allow_any_instance_of(Spree::OrderUpdater).to receive(:update_adjustment_total).and_return(10)enddiff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rbindex 8eacc5f578..562f4afbcc 100644--- a/core/spec/models/spree/order_merger_spec.rb+++ b/core/spec/models/spree/order_merger_spec.rb@@ -116,15 +116,15 @@ module Spreespecify dosubject.merge!(order_2)- line_items = order_1.line_items.reload- expect(line_items.count).to eq(2)+ expect(order_1.line_items.length).to eq(2)+ expect(order_1.line_items.count).to eq(2)expect(order_1.item_count).to eq 2- expect(order_1.item_total).to eq line_items.map(&:amount).sum+ expect(order_1.item_total).to eq order_1.line_items.map(&:amount).sum# No guarantee on ordering of line items, so we do this:- expect(line_items.pluck(:quantity)).to match_array([1, 1])- expect(line_items.pluck(:variant_id)).to match_array([variant.id, variant_2.id])+ expect(order_1.line_items.pluck(:quantity)).to match_array([1, 1])+ expect(order_1.line_items.pluck(:variant_id)).to match_array([variant.id, variant_2.id])endend@@ -138,6 +138,7 @@ module Spreeit 'creates errors with invalid line items' dovariant_2.destroy!+ order_2.line_items.to_a.first.reloadsubject.merge!(order_2)expect(order_1.errors.full_messages).not_to be_emptyenddiff --git a/core/spec/models/spree/order_updater_spec.rb b/core/spec/models/spree/order_updater_spec.rbindex 66ba5380ec..52531ec160 100644--- a/core/spec/models/spree/order_updater_spec.rb+++ b/core/spec/models/spree/order_updater_spec.rb@@ -9,6 +9,7 @@ module Spreecontext 'order totals' dobefore docreate_list(:line_item, 2, order: order, price: 10)+ order.reloadorder.update_with_updater!end@@ -25,6 +26,7 @@ module Spreeit 'update shipment total' docreate(:shipment, order: order, cost: 10)+ order.reloadupdater.update_shipment_totalexpect(order.shipment_total).to eq(10)end@@ -42,6 +44,7 @@ module Spreeupdater.updatecreate(:adjustment, source: promotion_action, adjustable: order, order: order)create(:line_item, order: order, price: 10) # in addition to the two already created+ order.reloadupdater.updateenddiff --git a/emails/spec/models/spree/order_spec.rb b/emails/spec/models/spree/order_spec.rbindex fe5b262bdb..3ecaa5448b 100644--- a/emails/spec/models/spree/order_spec.rb+++ b/emails/spec/models/spree/order_spec.rb@@ -117,7 +117,7 @@ describe Spree::Order, type: :model doallow(shipments).to receive_messages ready: []allow(shipments).to receive_messages pending: []allow(shipments).to receive_messages shipped: []- allow(shipments).to receive(:sum).with(:cost).and_return(shipment.cost)+ allow(shipments).to receive_message_chain(:sum, :cost).and_return(shipment.cost)allow_any_instance_of(Spree::OrderUpdater).to receive(:update_adjustment_total).and_return(10)end