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.

Improve `Product#total_on_hand` & `Variant#backorderable?` performance (#11335)

Damian Legawiec5e1ecaed36a4
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 7919e1e343..0d97d9d924 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -301,11 +301,13 @@ module Spree
end
def total_on_hand
- @total_on_hand ||= if any_variants_not_track_inventory?
- Float::INFINITY
- else
- stock_items.sum(:count_on_hand)
- end
+ @total_on_hand ||= Rails.cache.fetch(['product-total-on-hand', cache_key_with_version]) do
+ if any_variants_not_track_inventory?
+ Float::INFINITY
+ else
+ stock_items.sum(:count_on_hand)
+ end
+ end
end
# Master variant may be deleted (i.e. when the product is deleted)
diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rb
index 071bd77aaf..02688b204d 100644
--- a/core/app/models/spree/stock_location.rb
+++ b/core/app/models/spree/stock_location.rb
@@ -2,6 +2,7 @@ module Spree
class StockLocation < Spree::Base
has_many :shipments
has_many :stock_items, dependent: :delete_all, inverse_of: :stock_location
+ has_many :variants, through: :stock_items
has_many :stock_movements, through: :stock_items
belongs_to :state, class_name: 'Spree::State', optional: true
@@ -14,6 +15,7 @@ module Spree
after_create :create_stock_items, if: :propagate_all_variants?
after_save :ensure_one_default
+ after_update :conditional_touch_records
def state_text
state.try(:abbr) || state.try(:name) || state_name
@@ -134,5 +136,12 @@ module Spree
StockLocation.where(default: true).where.not(id: id).update_all(default: false)
end
end
+
+ def conditional_touch_records
+ return unless active_changed?
+
+ stock_items.update_all(updated_at: Time.current)
+ variants.update_all(updated_at: Time.current)
+ end
end
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 1765dc9366..1c8225d90c 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -256,7 +256,13 @@ module Spree
end
end
- delegate :total_on_hand, :can_supply?, :backorderable?, to: :quantifier
+ def backorderable?
+ @backorderable ||= Rails.cache.fetch(['variant-backorderable', cache_key_with_version]) do
+ quantifier.backorderable?
+ end
+ end
+
+ delegate :total_on_hand, :can_supply?, to: :quantifier
alias is_backorderable? backorderable?
@@ -291,7 +297,7 @@ module Spree
end
def backordered?
- @backordered ||= total_on_hand <= 0 && stock_items.exists?(backorderable: true)
+ @backordered ||= !in_stock? && stock_items.exists?(backorderable: true)
end
private
diff --git a/core/spec/models/spree/stock_location_spec.rb b/core/spec/models/spree/stock_location_spec.rb
index 07c512616c..d258c8eebf 100644
--- a/core/spec/models/spree/stock_location_spec.rb
+++ b/core/spec/models/spree/stock_location_spec.rb
@@ -253,5 +253,19 @@ module Spree
specify { expect(subject.state_text).to eq('virginia') }
end
end
+
+ describe '#conditionally_touch_records' do
+ let(:item) { subject.items.last }
+ let(:variant) { subject.variants.last }
+
+ context 'active has changed' do
+ it { expect { subject.update(active: false).to change(variant, :updated_at) } }
+ it { expect { subject.update(active: false).to change(item, :updated_at) } }
+ end
+
+ context 'active has not changed' do
+ it { expect { subject.update(name: 'my other warehouse').to change(variant, :updated_at) } }
+ end
+ end
end
end