When a product is deleted, touch the supplier and distributors

This commit is contained in:
Rohan Mitchell
2016-11-25 10:53:08 +11:00
parent da9a3ce9f3
commit 57363e2da5
2 changed files with 19 additions and 0 deletions

View File

@@ -188,6 +188,10 @@ Spree::Product.class_eval do
def delete_with_delete_from_order_cycles
transaction do
OpenFoodNetwork::ProductsCache.product_deleted(self) do
# Touch supplier and distributors as we would on #destroy
self.supplier.touch
touch_distributors
ExchangeVariant.where('exchange_variants.variant_id IN (?)', self.variants_including_master_and_deleted).destroy_all
delete_without_delete_from_order_cycles

View File

@@ -177,6 +177,21 @@ module Spree
# 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.
describe "touching affected enterprises when the product is deleted" do
let(:product) { create(:simple_product) }
let(:supplier) { product.supplier }
let(:distributor) { create(:distributor_enterprise) }
let!(:oc) { create(:simple_order_cycle, distributors: [distributor], variants: [product.variants.first]) }
it "touches the supplier" do
expect { product.delete }.to change { supplier.reload.updated_at }
end
it "touches all distributors" do
expect { product.delete }.to change { distributor.reload.updated_at }
end
end
end
describe "scopes" do