11068: update product/varaint delete actions

This commit is contained in:
Ahmed Ejaz
2023-12-14 01:20:33 +05:00
parent a71bb21789
commit 26d102f66b
3 changed files with 23 additions and 14 deletions

View File

@@ -46,21 +46,31 @@ class ProductsReflex < ApplicationReflex
render_products_form_with_flash
end
def delete_product(product_id)
if ProductDeleter.delete(product_id)
def delete_product(id)
authorize! :delete, Spree::Product
product = product_finder(id).find_product
authorize! :delete, product
if product.destroy
puts "Deleted Successfully"
else
puts "Failure"
end
fetch_and_render_products
end
def delete_variant(variant_id)
if VariantDeleter.new.delete(variant_id)
def delete_variant(id)
authorize! :delete, Spree::Variant
variant = variant_scope.find(id)
authorize! :delete, variant
if VariantDeleter.new.delete(variant)
puts "Deleted Successfully"
else
puts "Failure"
end
fetch_and_render_products
end
@@ -238,4 +248,12 @@ class ProductsReflex < ApplicationReflex
params.permit(products: ::PermittedAttributes::Product.attributes)
.to_h.with_indifferent_access
end
def product_finder(id)
ProductScopeQuery.new(current_user, {id:})
end
def variant_scope
Spree::Variant.active
end
end

View File

@@ -1,10 +0,0 @@
# frozen_string_literal: true
# This soft deletes the product
class ProductDeleter
# @param id [int] ID of the product to be deleted
def self.delete(id)
product = Spree::Product.find_by(id:)
product&.destroy
end
end

View File

@@ -16,6 +16,7 @@ export default class extends ApplicationController {
}
deleteVariant() {
window.dispatchEvent(new Event('modal:close'));
this.stimulate('Products#delete_variant', this.currentIdValue)
}