From af7e3380d3be4866e64649280a3392525abc5f61 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 29 Jan 2016 15:35:26 +1100 Subject: [PATCH] Perform refresh of products cache when coordinator fee is changed --- lib/open_food_network/products_cache.rb | 8 ++++++++ .../open_food_network/products_cache_spec.rb | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/open_food_network/products_cache.rb b/lib/open_food_network/products_cache.rb index 858316f7f8..639076fb82 100644 --- a/lib/open_food_network/products_cache.rb +++ b/lib/open_food_network/products_cache.rb @@ -50,6 +50,7 @@ module OpenFoodNetwork def self.enterprise_fee_changed(enterprise_fee) + refresh_coordinator_fee enterprise_fee end @@ -74,6 +75,13 @@ module OpenFoodNetwork end + def self.refresh_coordinator_fee(enterprise_fee) + enterprise_fee.order_cycles.each do |order_cycle| + order_cycle_changed order_cycle + end + end + + def self.refresh_cache(distributor, order_cycle) Delayed::Job.enqueue RefreshProductsCacheJob.new distributor.id, order_cycle.id end diff --git a/spec/lib/open_food_network/products_cache_spec.rb b/spec/lib/open_food_network/products_cache_spec.rb index c44d47fca2..26f3a9b844 100644 --- a/spec/lib/open_food_network/products_cache_spec.rb +++ b/spec/lib/open_food_network/products_cache_spec.rb @@ -164,6 +164,23 @@ module OpenFoodNetwork end + describe "when an enterprise fee is changed" do + let(:s) { create(:supplier_enterprise) } + let(:c) { create(:distributor_enterprise) } + let(:d1) { create(:distributor_enterprise) } + let(:d2) { create(:distributor_enterprise) } + let(:ef) { create(:enterprise_fee) } + let(:ef_coord) { create(:enterprise_fee, order_cycles: [oc]) } + let(:oc) { create(:open_order_cycle, coordinator: c) } + + + it "updates order cycles when it's a coordinator fee" do + ef_coord + expect(ProductsCache).to receive(:order_cycle_changed).with(oc).once + ProductsCache.enterprise_fee_changed ef_coord + end + end + describe "refreshing the cache" do let(:distributor) { double(:distributor, id: 123) } let(:order_cycle) { double(:order_cycle, id: 456) }