From 0c65d1ddd88a93f1bcf168f419bb5dd8a1b3692f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 29 Jan 2016 15:31:14 +1100 Subject: [PATCH] Trigger products cache refresh when enterprise fee changed or destroyed --- app/models/enterprise_fee.rb | 26 +++++++++++++++++++++++-- lib/open_food_network/products_cache.rb | 9 +++++++++ spec/models/enterprise_fee_spec.rb | 15 ++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index e19f91d0f4..e2831a4178 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -2,10 +2,11 @@ class EnterpriseFee < ActiveRecord::Base belongs_to :enterprise belongs_to :tax_category, class_name: 'Spree::TaxCategory', foreign_key: 'tax_category_id' has_and_belongs_to_many :order_cycles, join_table: 'coordinator_fees' - has_many :exchange_fees, dependent: :destroy + has_many :exchange_fees has_many :exchanges, through: :exchange_fees - before_destroy { order_cycles.clear } + after_save :refresh_products_cache + around_destroy :destruction calculated_adjustments @@ -57,4 +58,25 @@ class EnterpriseFee < ActiveRecord::Base :mandatory => mandatory, :locked => true}, :without_protection => true) end + + + private + + def refresh_products_cache + OpenFoodNetwork::ProductsCache.enterprise_fee_changed self + end + + def destruction + OpenFoodNetwork::ProductsCache.enterprise_fee_destroyed(self) do + # Remove this association here instead of using dependent: :destroy because + # dependent-destroy acts before this around_filter is called, so ProductsCache + # has no way of knowing where this fee was used. + order_cycles.clear + exchange_fees.destroy_all + + # Destroy the enterprise fee + yield + end + + end end diff --git a/lib/open_food_network/products_cache.rb b/lib/open_food_network/products_cache.rb index 8c22c40db7..858316f7f8 100644 --- a/lib/open_food_network/products_cache.rb +++ b/lib/open_food_network/products_cache.rb @@ -49,6 +49,15 @@ module OpenFoodNetwork end + def self.enterprise_fee_changed(enterprise_fee) + end + + + def self.enterprise_fee_destroyed(enterprise_fee, &block) + block.call + end + + private def self.exchanges_featuring_variants(variants, distributor: nil) diff --git a/spec/models/enterprise_fee_spec.rb b/spec/models/enterprise_fee_spec.rb index f0f9df1f0b..7140feda4a 100644 --- a/spec/models/enterprise_fee_spec.rb +++ b/spec/models/enterprise_fee_spec.rb @@ -10,8 +10,20 @@ describe EnterpriseFee do end describe "callbacks" do + let(:ef) { create(:enterprise_fee) } + + it "refreshes the products cache when saved" do + expect(OpenFoodNetwork::ProductsCache).to receive(:enterprise_fee_changed).with(ef) + ef.name = 'foo' + ef.save + end + + it "refreshes the products cache when destroyed" do + expect(OpenFoodNetwork::ProductsCache).to receive(:enterprise_fee_destroyed).with(ef) + ef.destroy + end + it "removes itself from order cycle coordinator fees when destroyed" do - ef = create(:enterprise_fee) oc = create(:simple_order_cycle, coordinator_fees: [ef]) ef.destroy @@ -19,7 +31,6 @@ describe EnterpriseFee do end it "removes itself from order cycle exchange fees when destroyed" do - ef = create(:enterprise_fee) oc = create(:simple_order_cycle) ex = create(:exchange, order_cycle: oc, enterprise_fees: [ef])