mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Refresh cache when option value changed or destroyed
This commit is contained in:
20
app/models/spree/option_value_decorator.rb
Normal file
20
app/models/spree/option_value_decorator.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module Spree
|
||||
OptionValue.class_eval do
|
||||
after_save :refresh_products_cache
|
||||
around_destroy :refresh_products_cache_from_destroy
|
||||
|
||||
|
||||
private
|
||||
|
||||
def refresh_products_cache
|
||||
variants(:reload).each &:refresh_products_cache
|
||||
end
|
||||
|
||||
def refresh_products_cache_from_destroy
|
||||
vs = variants(:reload).to_a
|
||||
yield
|
||||
vs.each &:refresh_products_cache
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
26
spec/models/spree/option_value_spec.rb
Normal file
26
spec/models/spree/option_value_spec.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
describe OptionValue do
|
||||
describe "products cache" do
|
||||
let(:variant) { create(:variant) }
|
||||
let(:option_value) { create(:option_value) }
|
||||
|
||||
before do
|
||||
variant.option_values << option_value
|
||||
option_value.reload
|
||||
end
|
||||
|
||||
it "refreshes the products cache on change, via variant" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
|
||||
option_value.name = 'foo'
|
||||
option_value.save!
|
||||
end
|
||||
|
||||
it "refreshes the products cache on destruction, via variant" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
|
||||
option_value.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user