mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Refresh products cache when price is changed or destroyed
This commit is contained in:
13
app/models/spree/price_decorator.rb
Normal file
13
app/models/spree/price_decorator.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module Spree
|
||||
Price.class_eval do
|
||||
after_save :refresh_products_cache
|
||||
after_destroy :refresh_products_cache
|
||||
|
||||
|
||||
private
|
||||
|
||||
def refresh_products_cache
|
||||
variant.refresh_products_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -85,12 +85,6 @@ Spree::Variant.class_eval do
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_weight_from_unit_value
|
||||
self.weight = weight_from_unit_value if self.product.variant_unit == 'weight' && unit_value.present?
|
||||
end
|
||||
|
||||
def refresh_products_cache
|
||||
if is_master?
|
||||
product.refresh_products_cache
|
||||
@@ -99,6 +93,13 @@ Spree::Variant.class_eval do
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def update_weight_from_unit_value
|
||||
self.weight = weight_from_unit_value if self.product.variant_unit == 'weight' && unit_value.present?
|
||||
end
|
||||
|
||||
def destruction
|
||||
if is_master?
|
||||
exchange_variants(:reload).destroy_all
|
||||
|
||||
21
spec/models/spree/price_spec.rb
Normal file
21
spec/models/spree/price_spec.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
describe Price do
|
||||
describe "callbacks" do
|
||||
let(:variant) { create(:variant) }
|
||||
let(:price) { variant.default_price }
|
||||
|
||||
it "refreshes the products cache on change" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
|
||||
price.amount = 123
|
||||
price.save
|
||||
end
|
||||
|
||||
it "refreshes the products cache on destroy" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_changed).with(variant)
|
||||
price.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user