mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Refresh products cache also on Variant#delete
Note that, as explained in https://apidock.com/rails/v3.2.13/ActiveRecord/Relation/delete, `delete` does not trigger callbacks and so it skips the products cache logic. If we still want to avoid instantiating the AR object, we need to explicitly call that logic for the cache to be up-to-date.
This commit is contained in:
@@ -10,7 +10,8 @@ Spree::Admin::VariantsController.class_eval do
|
||||
|
||||
def destroy
|
||||
@variant = Spree::Variant.find(params[:id])
|
||||
@variant.delete # This line changed, as well as removal of following conditional
|
||||
@variant.delete_and_refresh_cache
|
||||
|
||||
flash[:success] = I18n.t('notice_messages.variant_deleted')
|
||||
|
||||
respond_with(@variant) do |format|
|
||||
|
||||
@@ -118,6 +118,11 @@ Spree::Variant.class_eval do
|
||||
end
|
||||
end
|
||||
|
||||
# Deletes the record, skipping callbacks, but it also refreshes the cache
|
||||
def delete_and_refresh_cache
|
||||
destruction { delete }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_weight_from_unit_value
|
||||
|
||||
@@ -196,7 +196,6 @@ module Spree
|
||||
end
|
||||
|
||||
it "refreshes the products cache for the entire product on destroy" do
|
||||
# Does this ever happen?
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_destroyed).never
|
||||
master.destroy
|
||||
@@ -551,4 +550,26 @@ module Spree
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#delete_and_refresh_cache' do
|
||||
context 'when it is not the master variant' do
|
||||
let(:variant) { create(:variant) }
|
||||
|
||||
it 'refreshes the products cache on delete' do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_destroyed).with(variant)
|
||||
variant.delete_and_refresh_cache
|
||||
end
|
||||
end
|
||||
|
||||
context "when it is the master variant" do
|
||||
let(:product) { create(:simple_product) }
|
||||
let(:master) { product.master }
|
||||
|
||||
it 'refreshes the products cache for the entire product on delete' do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:product_changed).with(product)
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:variant_destroyed).never
|
||||
master.delete_and_refresh_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user