Trigger products cache refresh when enterprise fee changed or destroyed

This commit is contained in:
Rohan Mitchell
2016-01-29 15:31:14 +11:00
parent 378a703cc3
commit 0c65d1ddd8
3 changed files with 46 additions and 4 deletions

View File

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

View File

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

View File

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