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
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.rbindex 7919e1e343..0d97d9d924 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -301,11 +301,13 @@ module Spreeenddef 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+ endend# 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.rbindex 071bd77aaf..02688b204d 100644--- a/core/app/models/spree/stock_location.rb+++ b/core/app/models/spree/stock_location.rb@@ -2,6 +2,7 @@ module Spreeclass StockLocation < Spree::Basehas_many :shipmentshas_many :stock_items, dependent: :delete_all, inverse_of: :stock_location+ has_many :variants, through: :stock_itemshas_many :stock_movements, through: :stock_itemsbelongs_to :state, class_name: 'Spree::State', optional: true@@ -14,6 +15,7 @@ module Spreeafter_create :create_stock_items, if: :propagate_all_variants?after_save :ensure_one_default+ after_update :conditional_touch_recordsdef state_textstate.try(:abbr) || state.try(:name) || state_name@@ -134,5 +136,12 @@ module SpreeStockLocation.where(default: true).where.not(id: id).update_all(default: false)endend++ def conditional_touch_records+ return unless active_changed?++ stock_items.update_all(updated_at: Time.current)+ variants.update_all(updated_at: Time.current)+ endendenddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 1765dc9366..1c8225d90c 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -256,7 +256,13 @@ module Spreeendend- 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: :quantifieralias is_backorderable? backorderable?@@ -291,7 +297,7 @@ module Spreeenddef backordered?- @backordered ||= total_on_hand <= 0 && stock_items.exists?(backorderable: true)+ @backordered ||= !in_stock? && stock_items.exists?(backorderable: true)endprivatediff --git a/core/spec/models/spree/stock_location_spec.rb b/core/spec/models/spree/stock_location_spec.rbindex 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 Spreespecify { expect(subject.state_text).to eq('virginia') }endend++ 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+ endendend