mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-02 06:51:40 +00:00
Trigger products cache refresh when enterprise fee changed or destroyed
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user