From b625ea0c619a13a359bcbebe3c1e7b61723c8b29 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 17 Oct 2019 18:21:49 +0100 Subject: [PATCH] Extract to class ExchangeVariantDeleter --- .../spree/admin/products_controller_decorator.rb | 9 +-------- app/models/spree/product_set.rb | 9 +-------- app/services/exchange_variant_deleter.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 16 deletions(-) create mode 100644 app/services/exchange_variant_deleter.rb diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 10742f51cb..beb7865d5d 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -50,7 +50,7 @@ Spree::Admin::ProductsController.class_eval do delete_stock_params_and_set_after do super - remove_product_from_order_cycles if original_supplier != @product.supplier_id + ExchangeVariantDeleter.new.delete(@product) if original_supplier != @product.supplier_id end end @@ -193,13 +193,6 @@ Spree::Admin::ProductsController.class_eval do end end - def remove_product_from_order_cycles - variant_ids = @product.variants.map(&:id) - ExchangeVariant. - where(variant_id: variant_ids). - delete_all - end - def set_product_master_variant_price_to_zero @product.price = 0 if @product.price.nil? end diff --git a/app/models/spree/product_set.rb b/app/models/spree/product_set.rb index bd825759a4..e4c3eac1fb 100644 --- a/app/models/spree/product_set.rb +++ b/app/models/spree/product_set.rb @@ -54,7 +54,7 @@ class Spree::ProductSet < ModelSet end product.save if errors.empty? - remove_product_from_order_cycles(product) if original_supplier != product.supplier_id + ExchangeVariantDeleter.new.delete(product) if original_supplier != product.supplier_id end def validate_presence_of_unit_value(product, variant) @@ -64,13 +64,6 @@ class Spree::ProductSet < ModelSet product.errors.add(:unit_value, "can't be blank") end - def remove_product_from_order_cycles(product) - variant_ids = product.variants.map(&:id) - ExchangeVariant. - where(variant_id: variant_ids). - delete_all - end - def update_product_variants(product, attributes) return true unless attributes[:variants_attributes] update_variants_attributes(product, attributes[:variants_attributes]) diff --git a/app/services/exchange_variant_deleter.rb b/app/services/exchange_variant_deleter.rb new file mode 100644 index 0000000000..21e7f909d5 --- /dev/null +++ b/app/services/exchange_variant_deleter.rb @@ -0,0 +1,8 @@ +class ExchangeVariantDeleter + def delete(product) + variant_ids = product.variants.map(&:id) + ExchangeVariant. + where(variant_id: variant_ids). + delete_all + end +end