Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
return if amount.zero?
- adjustments.new(
- adjustable: adjustable,
+ adjustable.adjustments.new(
amount: amount,
included: included,
label: label,
- order: order
+ order: order,
+ source: self
).save
end
diff --git a/core/app/models/spree/order_merger.rb b/core/app/models/spree/order_merger.rb
index dfc4722249..03432b8c91 100644
--- a/core/app/models/spree/order_merger.rb
+++ b/core/app/models/spree/order_merger.rb
@@ -57,7 +57,7 @@ module Spree
current_line_item.quantity += other_order_line_item.quantity
handle_error(current_line_item) unless current_line_item.save
else
- other_order_line_item.order_id = order.id
+ order.line_items << other_order_line_item
other_order_line_item.adjustments.update_all(order_id: order.id)
handle_error(other_order_line_item) unless other_order_line_item.save
end
@@ -69,9 +69,7 @@ module Spree
end
def persist_merge
- updater.update_item_count
- updater.update_item_total
- updater.persist_totals
+ updater.update
end
def handle_gift_card(other_order)
diff --git a/core/app/models/spree/order_updater.rb b/core/app/models/spree/order_updater.rb
index 0de95e1d28..aaebdd9692 100644
--- a/core/app/models/spree/order_updater.rb
+++ b/core/app/models/spree/order_updater.rb
@@ -69,7 +69,7 @@ module Spree
end
def update_shipment_total
- order.shipment_total = shipments.sum(:cost)
+ order.shipment_total = shipments.to_a.sum(&:cost)
update_order_total
end
@@ -97,7 +97,7 @@ module Spree
end
def update_item_total
- order.item_total = line_items.sum('price * quantity')
+ order.item_total = line_items.to_a.sum(&:amount)
update_order_total
end
diff --git a/core/app/models/spree/promotion/actions/free_shipping.rb b/core/app/models/spree/promotion/actions/free_shipping.rb
index 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 Spree
def 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
).save
end
end
diff --git a/core/app/services/spree/cart/recalculate.rb b/core/app/services/spree/cart/recalculate.rb
index 2c6df9c311..2096a2d7ea 100644
--- a/core/app/services/spree/cart/recalculate.rb
+++ b/core/app/services/spree/cart/recalculate.rb
@@ -4,11 +4,12 @@ module Spree
prepend Spree::ServiceModule::Base
def 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_totals
shipment = 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.rb
index 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 Spree
def 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.reload
success(line_item)
end
end
diff --git a/core/app/services/spree/line_items/destroy.rb b/core/app/services/spree/line_items/destroy.rb
index 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 Spree
order = line_item.order
ActiveRecord::Base.transaction do
- line_item.destroy!
+ order.line_items.destroy(line_item)
recalculate_service.call(order: order, line_item: line_item, options: options)
end
success(line_item)
diff --git a/core/spec/models/spree/order/state_machine_spec.rb b/core/spec/models/spree/order/state_machine_spec.rb
index 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 do
allow(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)
end
diff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rb
index 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 Spree
specify do
subject.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])
end
end
@@ -138,6 +138,7 @@ module Spree
it 'creates errors with invalid line items' do
variant_2.destroy!
+ order_2.line_items.to_a.first.reload
subject.merge!(order_2)
expect(order_1.errors.full_messages).not_to be_empty
end
diff --git a/core/spec/models/spree/order_updater_spec.rb b/core/spec/models/spree/order_updater_spec.rb
index 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 Spree
context 'order totals' do
before do
create_list(:line_item, 2, order: order, price: 10)
+ order.reload
order.update_with_updater!
end
@@ -25,6 +26,7 @@ module Spree
it 'update shipment total' do
create(:shipment, order: order, cost: 10)
+ order.reload
updater.update_shipment_total
expect(order.shipment_total).to eq(10)
end
@@ -42,6 +44,7 @@ module Spree
updater.update
create(:adjustment, source: promotion_action, adjustable: order, order: order)
create(:line_item, order: order, price: 10) # in addition to the two already created
+ order.reload
updater.update
end
diff --git a/emails/spec/models/spree/order_spec.rb b/emails/spec/models/spree/order_spec.rb
index 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 do
allow(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