From 8bd5a36aaff4e020f7cdb414f0305b08e3f2947d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 5 Feb 2016 09:14:49 +1100 Subject: [PATCH] Remove enterprise fee destruction cache callback - responsibility to be handled by dependent models --- app/models/enterprise_fee.rb | 21 ++++----------------- lib/open_food_network/products_cache.rb | 5 ----- spec/models/enterprise_fee_spec.rb | 5 ----- 3 files changed, 4 insertions(+), 27 deletions(-) diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index 8e45707d07..6e26b9f5fa 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -2,15 +2,16 @@ class EnterpriseFee < ActiveRecord::Base belongs_to :enterprise belongs_to :tax_category, class_name: 'Spree::TaxCategory', foreign_key: 'tax_category_id' - has_many :coordinator_fees + has_many :coordinator_fees, dependent: :destroy has_many :order_cycles, through: :coordinator_fees - has_many :exchange_fees + has_many :exchange_fees, dependent: :destroy has_many :exchanges, through: :exchange_fees after_save :refresh_products_cache - around_destroy :destruction + # After destroy, the products cache is refreshed via the after_destroy hook for + # coordinator_fees and exchange_fees calculated_adjustments @@ -70,18 +71,4 @@ class EnterpriseFee < ActiveRecord::Base 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(:reload).clear - exchange_fees(:reload).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 50a6099f4b..d01bcf86e3 100644 --- a/lib/open_food_network/products_cache.rb +++ b/lib/open_food_network/products_cache.rb @@ -57,11 +57,6 @@ module OpenFoodNetwork 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 7140feda4a..109e5baac7 100644 --- a/spec/models/enterprise_fee_spec.rb +++ b/spec/models/enterprise_fee_spec.rb @@ -18,11 +18,6 @@ describe EnterpriseFee do 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 oc = create(:simple_order_cycle, coordinator_fees: [ef])