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
Performance: use loaded associations in `Quantifier` to speeds things up (#12092)
Damian Legawiec15b4a15f6327
diff --git a/core/app/models/spree/stock/quantifier.rb b/core/app/models/spree/stock/quantifier.rbindex 06a01770c7..29e0ff28e2 100644--- a/core/app/models/spree/stock/quantifier.rb+++ b/core/app/models/spree/stock/quantifier.rb@@ -10,7 +10,11 @@ module Spreedef total_on_hand@total_on_hand ||= if variant.should_track_inventory?- stock_items.sum(:count_on_hand)+ if association_loaded?+ stock_items.sum(&:count_on_hand)+ else+ stock_items.sum(:count_on_hand)+ endelseBigDecimal::INFINITYend@@ -30,10 +34,24 @@ module Spreeprivate- def scope_to_location(collection)- return collection.with_active_stock_location if stock_location.blank?+ def association_loaded?+ variant.association(:stock_items).loaded? && variant.association(:stock_locations).loaded?+ end- collection.where(stock_location: stock_location)+ def scope_to_location(collection)+ if stock_location.blank?+ if association_loaded?+ return collection.select { |si| si.stock_location&.active? }+ else+ return collection.with_active_stock_location+ end+ end++ if association_loaded?+ collection.select { |si| si.stock_location_id == stock_location.id }+ else+ collection.where(stock_location: stock_location)+ endendendend