mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Refresh products cache when product properties are changed
This commit is contained in:
@@ -191,6 +191,11 @@ Spree::Product.class_eval do
|
||||
alias_method_chain :delete, :delete_from_order_cycles
|
||||
|
||||
|
||||
def refresh_products_cache
|
||||
OpenFoodNetwork::ProductsCache.product_changed self
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def set_available_on_to_now
|
||||
@@ -251,8 +256,4 @@ Spree::Product.class_eval do
|
||||
self.permalink = create_unique_permalink(requested.parameterize)
|
||||
end
|
||||
end
|
||||
|
||||
def refresh_products_cache
|
||||
OpenFoodNetwork::ProductsCache.product_changed self
|
||||
end
|
||||
end
|
||||
|
||||
10
app/models/spree/product_property_decorator.rb
Normal file
10
app/models/spree/product_property_decorator.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
module Spree
|
||||
ProductProperty.class_eval do
|
||||
after_save :refresh_products_cache
|
||||
after_destroy :refresh_products_cache
|
||||
|
||||
def refresh_products_cache
|
||||
product.refresh_products_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
15
app/models/spree/property_decorator.rb
Normal file
15
app/models/spree/property_decorator.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module Spree
|
||||
Property.class_eval do
|
||||
after_save :refresh_products_cache
|
||||
|
||||
# When a Property is destroyed, dependent-destroy will destroy all ProductProperties,
|
||||
# which will take care of refreshing the products cache
|
||||
|
||||
|
||||
private
|
||||
|
||||
def refresh_products_cache
|
||||
product_properties(:reload).each &:refresh_products_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
21
spec/models/spree/product_property_spec.rb
Normal file
21
spec/models/spree/product_property_spec.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
describe ProductProperty do
|
||||
describe "callbacks" do
|
||||
let(:product) { product_property.product }
|
||||
let(:product_property) { create(:product_property) }
|
||||
|
||||
it "refreshes the products cache on save, via Product" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
|
||||
product_property.value = 123
|
||||
product_property.save
|
||||
end
|
||||
|
||||
it "refreshes the products cache on destroy" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
|
||||
product_property.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
spec/models/spree/property_spec.rb
Normal file
17
spec/models/spree/property_spec.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
describe Property do
|
||||
describe "callbacks" do
|
||||
let(:property) { product_property.property }
|
||||
let(:product) { product_property.product }
|
||||
let(:product_property) { create(:product_property) }
|
||||
|
||||
it "refreshes the products cache on save" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
|
||||
property.name = 'asdf'
|
||||
property.save
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user