mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Avoid extra query on stock_items every time #on_demand is called on a variant.
In the case where the variant has not been saved yet, we can use #new_record? here instead of #stock_items.empty?, to avoid an additional query. This can be called a vast number of times per request, in various N+1s. The other case where we need to return here is when a variant has been deleted, so #stock_items will be empty and #stock_item will be nil. Likewise, we can just check that with #deleted? and avoid #stock_items.empty?
This commit is contained in:
@@ -40,9 +40,9 @@ module VariantStock
|
||||
|
||||
# Checks whether this variant is produced on demand.
|
||||
def on_demand
|
||||
# A variant that has not been saved yet, doesn't have a stock item
|
||||
# A variant that has not been saved yet or has been soft-deleted doesn't have a stock item
|
||||
# This provides a default value for variant.on_demand using Spree::StockLocation.backorderable_default
|
||||
return Spree::StockLocation.first.backorderable_default if stock_items.empty?
|
||||
return Spree::StockLocation.first.backorderable_default if new_record? || deleted?
|
||||
|
||||
stock_item.backorderable?
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user