From b7a88fd03b9c9fe7a3b39ff912abb445935a3de3 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 29 Jan 2016 10:14:15 +1100 Subject: [PATCH] Perform refresh of products cache for variant override change --- lib/open_food_network/products_cache.rb | 11 +++++++++-- .../open_food_network/products_cache_spec.rb | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/open_food_network/products_cache.rb b/lib/open_food_network/products_cache.rb index 2666f7ffc7..556b488316 100644 --- a/lib/open_food_network/products_cache.rb +++ b/lib/open_food_network/products_cache.rb @@ -29,6 +29,9 @@ module OpenFoodNetwork def self.variant_override_changed(variant_override) + exchanges_featuring_variants(variant_override.variant, distributor: variant_override.hub).each do |exchange| + refresh_cache exchange.receiver, exchange.order_cycle + end end @@ -38,13 +41,17 @@ module OpenFoodNetwork private - def self.exchanges_featuring_variants(variants) - Exchange. + def self.exchanges_featuring_variants(variants, distributor: nil) + exchanges = Exchange. outgoing. with_any_variant(variants). joins(:order_cycle). merge(OrderCycle.dated). merge(OrderCycle.not_closed) + + exchanges = exchanges.to_enterprise(distributor) if distributor + + exchanges end diff --git a/spec/lib/open_food_network/products_cache_spec.rb b/spec/lib/open_food_network/products_cache_spec.rb index d48f5e7d5c..ef3f3ed279 100644 --- a/spec/lib/open_food_network/products_cache_spec.rb +++ b/spec/lib/open_food_network/products_cache_spec.rb @@ -89,6 +89,25 @@ module OpenFoodNetwork end end + describe "when a variant override changes" do + let(:variant) { create(:variant) } + let(:d1) { create(:distributor_enterprise) } + let(:d2) { create(:distributor_enterprise) } + let!(:vo) { create(:variant_override, variant: variant, hub: d1) } + let!(:oc) { create(:open_order_cycle, distributors: [d1, d2], variants: [variant]) } + + it "refreshes the distributions that the variant override affects" do + expect(ProductsCache).to receive(:refresh_cache).with(d1, oc).once + ProductsCache.variant_override_changed vo + end + + it "does not refresh other distributors of the variant" do + expect(ProductsCache).to receive(:refresh_cache).with(d2, oc).never + ProductsCache.variant_override_changed vo + end + end + + describe "refreshing the cache" do let(:distributor) { double(:distributor, id: 123) } let(:order_cycle) { double(:order_cycle, id: 456) }