Refresh products cache when price is changed or destroyed

This commit is contained in:
Rohan Mitchell
2016-02-03 16:08:41 +11:00
parent d8d803546b
commit 339f3fc2f0
3 changed files with 41 additions and 6 deletions

View 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

View File

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

View 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