mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Refactor #load_products and memoize
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user