Files
openfoodnetwork/app/services/variant_deleter.rb
Matt-Yorkley a8559e621f Simplify exchange variant callback and prefer delete over destroy
Delete is faster than destroy but should only be used if the object has no callbacks or touches.
2023-05-19 11:26:33 +01:00

20 lines
390 B
Ruby

# frozen_string_literal: true
# Checks the validity of a soft-delete call.
class VariantDeleter
def delete(variant)
if only_variant_on_product?(variant)
variant.errors.add :product, I18n.t(:spree_variant_product_error)
return false
end
variant.destroy
end
private
def only_variant_on_product?(variant)
variant.product.variants == [variant]
end
end