Remove enterprise fee destruction cache callback - responsibility to be handled by dependent models

This commit is contained in:
Rohan Mitchell
2016-02-05 09:14:49 +11:00
parent a64a501dbb
commit 8bd5a36aaf
3 changed files with 4 additions and 27 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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])