mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-02 21:57:17 +00:00
Update soft-delete of products
Spree changed their way of soft-deleting products, variants and some other models. `#destroy` is now soft-deleting and replaces `#delete`. This commit considers only products. Variants will follow in another commit. The other models can be ignored, because we don't call `delete` on them.
This commit is contained in:
@@ -33,7 +33,7 @@ Spree::Api::ProductsController.class_eval do
|
||||
authorize! :delete, Spree::Product
|
||||
@product = find_product(params[:product_id])
|
||||
authorize! :delete, @product
|
||||
@product.delete
|
||||
@product.destroy
|
||||
respond_with(@product, :status => 204)
|
||||
end
|
||||
|
||||
|
||||
@@ -202,20 +202,18 @@ Spree::Product.class_eval do
|
||||
end
|
||||
end
|
||||
|
||||
def delete_with_delete_from_order_cycles
|
||||
def destroy_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.with_deleted).destroy_all
|
||||
|
||||
delete_without_delete_from_order_cycles
|
||||
destroy_without_delete_from_order_cycles
|
||||
end
|
||||
end
|
||||
end
|
||||
alias_method_chain :delete, :delete_from_order_cycles
|
||||
alias_method_chain :destroy, :delete_from_order_cycles
|
||||
|
||||
|
||||
def refresh_products_cache
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module OpenFoodNetwork
|
||||
xdescribe ProductsAndInventoryReport do
|
||||
describe ProductsAndInventoryReport do
|
||||
context "As a site admin" do
|
||||
let(:user) do
|
||||
user = create(:user)
|
||||
@@ -105,7 +105,7 @@ module OpenFoodNetwork
|
||||
it "should filter deleted products" do
|
||||
product1 = create(:simple_product, supplier: supplier)
|
||||
product2 = create(:simple_product, supplier: supplier)
|
||||
product2.delete
|
||||
product2.destroy
|
||||
subject.filter(Spree::Variant.scoped).should match_array [product1.master, product1.variants.first]
|
||||
end
|
||||
describe "based on report type" do
|
||||
|
||||
@@ -98,7 +98,7 @@ module OpenFoodNetwork
|
||||
|
||||
it "refreshes the cache based on exchanges the variant was in before destruction" do
|
||||
expect(ProductsCache).to receive(:refresh_cache).with(distributor, oc)
|
||||
product.delete
|
||||
product.destroy
|
||||
end
|
||||
|
||||
it "performs the cache refresh after the product has been removed from the order cycle" do
|
||||
@@ -106,7 +106,7 @@ module OpenFoodNetwork
|
||||
expect(product.reload.deleted_at).not_to be_nil
|
||||
end
|
||||
|
||||
product.delete
|
||||
product.destroy
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ module Spree
|
||||
|
||||
it "refreshes the products cache on delete" do
|
||||
expect(OpenFoodNetwork::ProductsCache).to receive(:product_deleted).with(product)
|
||||
product.delete
|
||||
product.destroy
|
||||
end
|
||||
|
||||
# On destroy, all distributed variants are refreshed by a Variant around_destroy
|
||||
@@ -185,11 +185,15 @@ module Spree
|
||||
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 }
|
||||
expect { product.destroy }.to change { supplier.reload.updated_at }
|
||||
end
|
||||
|
||||
it "touches all distributors" do
|
||||
expect { product.delete }.to change { distributor.reload.updated_at }
|
||||
expect { product.destroy }.to change { distributor.reload.updated_at }
|
||||
end
|
||||
|
||||
it "removes variants from order cycles" do
|
||||
expect { product.destroy }.to change { ExchangeVariant.count }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -701,13 +705,13 @@ module Spree
|
||||
|
||||
it "removes the master variant from all order cycles" do
|
||||
e.variants << p.master
|
||||
p.delete
|
||||
p.destroy
|
||||
e.variants(true).should be_empty
|
||||
end
|
||||
|
||||
it "removes all other variants from order cycles" do
|
||||
e.variants << v
|
||||
p.delete
|
||||
p.destroy
|
||||
e.variants(true).should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
@@ -24,7 +24,7 @@ describe "Shop API", type: :request do
|
||||
set_order order
|
||||
|
||||
v61.update_attribute(:count_on_hand, 1)
|
||||
p6.delete
|
||||
p6.destroy
|
||||
v71.update_attribute(:count_on_hand, 1)
|
||||
v41.update_attribute(:count_on_hand, 1)
|
||||
v42.update_attribute(:count_on_hand, 0)
|
||||
|
||||
Reference in New Issue
Block a user