Refactor #load_products and memoize

This commit is contained in:
Matt-Yorkley
2019-09-29 14:14:52 +01:00
parent d5e90c3c6c
commit e9acf6e0de
2 changed files with 6 additions and 8 deletions

View File

@@ -10,12 +10,10 @@ module OpenFoodNetwork
end
def products_json
products = load_products
if products
if shop_products
enterprise_fee_calculator = EnterpriseFeeCalculator.new @distributor, @order_cycle
ActiveModel::ArraySerializer.new(products,
ActiveModel::ArraySerializer.new(shop_products,
each_serializer: Api::ProductSerializer,
current_order_cycle: @order_cycle,
current_distributor: @distributor,
@@ -29,10 +27,10 @@ module OpenFoodNetwork
private
def load_products
def shop_products
return unless @order_cycle
ShopProductsService.new(@distributor, @order_cycle).relation.
@shop_products ||= ShopProductsService.new(@distributor, @order_cycle).relation.
order(taxon_order).
each { |product| scoper.scope(product) }
end

View File

@@ -25,13 +25,13 @@ module OpenFoodNetwork
it "sorts products by the distributor's preferred taxon list" do
allow(distributor).to receive(:preferred_shopfront_taxon_order) { "#{t1.id},#{t2.id}" }
products = pr.send(:load_products)
products = pr.send(:shop_products)
expect(products).to eq([p2, p4, p1, p3])
end
it "alphabetizes products by name when taxon list is not set" do
allow(distributor).to receive(:preferred_shopfront_taxon_order) { "" }
products = pr.send(:load_products)
products = pr.send(:shop_products)
expect(products).to eq([p1, p2, p3, p4])
end
end