diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 11f5e378..c939b02d 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -5,6 +5,8 @@ has_many :assemblies, through: :assemblies_variants, class_name: "Spree::Variant", dependent: :destroy has_many :parts, through: :parts_variants, class_name: "Spree::Variant", dependent: :destroy + before_destroy :ensure_no_inventory_units + def assemblies_for(products) assemblies.where(id: products) end @@ -12,4 +14,12 @@ def assemblies_for(products) def part? assemblies.exists? end + + private + def ensure_no_inventory_units + if inventory_units.any? + errors.add(:base, Spree.t(:cannot_destroy_if_attached_to_inventory_units)) + return false + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index f5baaac5..a5af1ebb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,6 +3,7 @@ en: spree: available_parts: Available parts can_be_part: Can be part + cannot_destroy_if_attached_to_inventory_units: Cannot delete variants once they are attached to inventory unit. individual_sale: Individual sale no_variants: No variants parts: Parts