Skip to content

Commit

Permalink
Merge pull request #4189 from Azeem838/fix-product-dup-bug
Browse files Browse the repository at this point in the history
Fix discarded duplicated products bug
  • Loading branch information
waiting-for-dev authored Jun 8, 2022
2 parents 41c3c73 + 5f248b2 commit 92a4f0f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Variant < Spree::Base

validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
validates :price, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
validates_uniqueness_of :sku, allow_blank: true, case_sensitive: true, if: :enforce_unique_sku?
validates_uniqueness_of :sku, allow_blank: true, case_sensitive: true, conditions: -> { where(deleted_at: nil) }, if: :enforce_unique_sku?

after_create :create_stock_items
after_create :set_position
Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/product_duplicator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ module Spree
it "will not duplicate the option values" do
expect{ duplicator.duplicate }.to change{ Spree::OptionValue.count }.by(0)
end

it "will duplicate the variants after initial duplicate is discarded" do
duplicator.duplicate.discard
expect { duplicator.duplicate }.to change { Spree::Variant.count }.by(3)
end
end
end
end
5 changes: 5 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
variant.price = nil
expect(variant).to be_invalid
end

it "should have a unique sku" do
variant_with_same_sku = build(:variant, sku: variant.sku)
expect(variant_with_same_sku).to be_invalid
end
end

context "after create" do
Expand Down

0 comments on commit 92a4f0f

Please sign in to comment.