Refresh products cache on product change

This commit is contained in:
Rohan Mitchell
2016-01-28 15:41:17 +11:00
parent 6d39cc39c6
commit 0c0c98a0b0
3 changed files with 26 additions and 5 deletions

View File

@@ -22,8 +22,6 @@ Spree::Product.class_eval do
attr_accessible :variant_unit, :variant_unit_scale, :variant_unit_name, :unit_value
attr_accessible :inherits_properties, :sku
before_validation :sanitize_permalink
# validates_presence_of :variants, unless: :new_record?, message: "Product must have at least one variant"
validates_presence_of :supplier
validates :primary_taxon, presence: { message: "^Product Category can't be blank" }
@@ -35,11 +33,13 @@ Spree::Product.class_eval do
validates_presence_of :variant_unit_name,
if: -> p { p.variant_unit == 'items' }
after_save :ensure_standard_variant
after_initialize :set_available_on_to_now, :if => :new_record?
after_save :update_units
after_touch :touch_distributors
before_validation :sanitize_permalink
before_save :add_primary_taxon_to_taxons
after_touch :touch_distributors
after_save :ensure_standard_variant
after_save :update_units
after_save :refresh_products_cache
# -- Joins
@@ -251,4 +251,8 @@ 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

@@ -21,6 +21,10 @@ module OpenFoodNetwork
end
def self.product_changed(product)
end
private
def self.exchanges_featuring_variant(variant)

View File

@@ -161,6 +161,19 @@ module Spree
end
end
describe "callbacks" do
let(:product) { create(:simple_product) }
it "refreshes the products cache on save" do
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
product.name = 'asdf'
product.save
end
# On destroy, all distributed variants are refreshed by a Variant around_destroy
# callback, so we don't need to do anything on the product model.
end
describe "scopes" do
describe "in_supplier" do
it "shows products in supplier" do