From 590ce67f380722954691bd60097e89447f31e9a8 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Thu, 5 Sep 2019 18:37:33 +0800 Subject: [PATCH] Fetch and scope variants for shop in ProductsRenderer only once --- lib/open_food_network/products_renderer.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/open_food_network/products_renderer.rb b/lib/open_food_network/products_renderer.rb index 6cc4307eea..350395ca40 100644 --- a/lib/open_food_network/products_renderer.rb +++ b/lib/open_food_network/products_renderer.rb @@ -54,14 +54,16 @@ module OpenFoodNetwork end def all_variants_for_shop - # We use the in_stock? method here instead of the in_stock scope because we need to - # look up the stock as overridden by VariantOverrides, and the scope method is not affected - # by them. - scoper = OpenFoodNetwork::ScopeVariantToHub.new(@distributor) - Spree::Variant. - for_distribution(@order_cycle, @distributor). - each { |v| scoper.scope(v) }. - select(&:in_stock?) + @all_variants_for_shop ||= begin + # We use the in_stock? method here instead of the in_stock scope + # because we need to look up the stock as overridden by + # VariantOverrides, and the scope method is not affected by them. + scoper = OpenFoodNetwork::ScopeVariantToHub.new(@distributor) + Spree::Variant. + for_distribution(@order_cycle, @distributor). + each { |v| scoper.scope(v) }. + select(&:in_stock?) + end end def variants_for_shop_by_id