mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-27 06:05:19 +00:00
When removing a product's option type, remove its variants' associated option values
This commit is contained in:
@@ -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
|
||||
|
||||
10
app/models/spree/product_option_type_decorator.rb
Normal file
10
app/models/spree/product_option_type_decorator.rb
Normal 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
|
||||
Reference in New Issue
Block a user