When removing a product's option type, remove its variants' associated option values

This commit is contained in:
Rohan Mitchell
2014-02-06 09:31:13 +11:00
parent 6243642cc9
commit eccea9d9ff
3 changed files with 45 additions and 0 deletions

View File

@@ -1,4 +1,10 @@
Spree::Product.class_eval do
# We have an after_destroy callback on Spree::ProductOptionType. However, if we
# don't specify dependent => destroy on this association, it is not called. See:
# https://github.com/rails/rails/issues/7618
has_many :option_types, :through => :product_option_types, :dependent => :destroy
belongs_to :supplier, :class_name => 'Enterprise'
has_many :product_distributions, :dependent => :destroy

View File

@@ -0,0 +1,10 @@
Spree::ProductOptionType.class_eval do
after_destroy :remove_option_values
def remove_option_values
self.product.variants_including_master.each do |variant|
option_values = variant.option_values.where(option_type_id: self.option_type)
variant.option_values.destroy *option_values
end
end
end