mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-24 05:38:52 +00:00
Refresh products cache on product change
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -21,6 +21,10 @@ module OpenFoodNetwork
|
||||
end
|
||||
|
||||
|
||||
def self.product_changed(product)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def self.exchanges_featuring_variant(variant)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user