Refresh products cache when product properties are changed

This commit is contained in:
Rohan Mitchell
2016-02-03 14:22:05 +11:00
parent f756749e02
commit 7c4e9e5838
5 changed files with 68 additions and 4 deletions

View File

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

View 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

View 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

View 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

View 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