Skip to content

Commit

Permalink
Fixed: don't destroy dependent associations if we cannot destroy the …
Browse files Browse the repository at this point in the history
…Product/Variant

Previously : ensure_no_line_items  callback could stop deleting the
Product/Variant but associations (with dependent: :destroy) were
deleted whatsoever, as this is a rails bug (see
rails/rails#3458)
  • Loading branch information
damianlegawiec committed Jun 15, 2016
1 parent 38ef7a7 commit 573272f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Product < Spree::Base

acts_as_paranoid

# we need to have this callback before any dependent: :destroy associations
# https://github.com/rails/rails/issues/3458
before_destroy :ensure_no_line_items

has_many :product_option_types, dependent: :destroy, inverse_of: :product
has_many :option_types, through: :product_option_types
has_many :product_properties, dependent: :destroy, inverse_of: :product
Expand Down Expand Up @@ -88,7 +92,6 @@ class Product < Spree::Base
after_save :run_touch_callbacks, if: :anything_changed?
after_save :reset_nested_changes
after_touch :touch_taxons
before_destroy :ensure_no_line_items

before_validation :normalize_slug, on: :update
before_validation :validate_master
Expand Down
5 changes: 4 additions & 1 deletion core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Variant < Spree::Base
:shipping_category_id, :meta_description, :meta_keywords,
:shipping_category

# we need to have this callback before any dependent: :destroy associations
# https://github.com/rails/rails/issues/3458
before_destroy :ensure_no_line_items

with_options inverse_of: :variant do
has_many :inventory_units
has_many :line_items
Expand Down Expand Up @@ -48,7 +52,6 @@ class Variant < Spree::Base

after_create :create_stock_items
after_create :set_master_out_of_stock, unless: :is_master?
before_destroy :ensure_no_line_items

after_touch :clear_in_stock_cache

Expand Down

0 comments on commit 573272f

Please sign in to comment.