Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix generating slugs for deleting products (#12959)

Łukasz Adamczyk1d01e5ffa6b4
diff --git a/core/app/models/spree/product/slugs.rb b/core/app/models/spree/product/slugs.rb
index 5b1b6e9a8d..b37b487547 100644
--- a/core/app/models/spree/product/slugs.rb
+++ b/core/app/models/spree/product/slugs.rb
@@ -57,8 +57,6 @@ module Spree
def slug_candidates
if defined?(:deleted_at) && deleted_at.present?
[
- ['deleted', :name],
- ['deleted', :name, :sku],
['deleted', :name, :uuid_for_friendly_id]
]
else
@@ -91,7 +89,7 @@ module Spree
set_slug
update_column(:slug, slug)
- new_slug = ->(rec) { "deleted-#{rec.id}_#{rec.slug}"[..254] }
+ new_slug = ->(rec) { "deleted-#{rec.slug}-#{uuid_for_friendly_id}"[..254] }
translations.with_deleted.each { |rec| rec.update_columns(slug: new_slug.call(rec)) }
slugs.with_deleted.each { |rec| rec.update_column(:slug, new_slug.call(rec)) }
diff --git a/core/spec/models/spree/product/slugs_spec.rb b/core/spec/models/spree/product/slugs_spec.rb
index 2f91940caf..dd0ef3d393 100644
--- a/core/spec/models/spree/product/slugs_spec.rb
+++ b/core/spec/models/spree/product/slugs_spec.rb
@@ -19,8 +19,8 @@ describe Spree::Product::Slugs, type: :model do
product.update!(name: 'ala', slug: nil)
end
- it 'updates slugs withs deleted-{id} prefix to ensure uniqueness' do
- expect { product.destroy! }.to change { product.slugs.with_deleted.first.slug }.to a_string_matching(/deleted-\d+_.*/)
+ it 'updates slugs with deleted-{id} prefix to ensure uniqueness' do
+ expect { product.destroy! }.to change { product.slugs.with_deleted.first.slug }.to a_string_matching(/deleted-ala-.*/)
end
it 'soft deletes slug record' do
@@ -34,15 +34,38 @@ describe Spree::Product::Slugs, type: :model do
let(:name) { 'product-name' }
+ before do
+ another_product.destroy!
+ end
+
it 'can use original slug' do
- original_slugs_slug = another_product.slugs.first.slug
- original_product_slug = another_product.slug
+ product.save!
- another_product.destroy!
+ expect(product.slugs.first.slug).to eq(name)
+ expect(product.slug).to eq(name)
+
+ deleted_slug = another_product.slugs.with_deleted.first
+ expect(deleted_slug.slug).to match(/deleted-#{name}-.+/)
+ expect(another_product.slug).to match(/deleted-#{name}-.+/)
+ end
+
+ it 'can delete the product again and then create a new product with the original slug' do
product.save!
+ product.destroy!
+
+ new_product = build(:product, name: name)
+ expect(new_product.save!).to eq(true)
- expect(product.slugs.first.slug).to eq(original_slugs_slug)
- expect(product.slug).to eq(original_product_slug)
+ expect(new_product.slug).to eq(name)
+ expect(new_product.slugs.first.slug).to eq(name)
+
+ deleted_slug_1 = another_product.slugs.with_deleted.first
+ expect(deleted_slug_1.slug).to match(/deleted-#{name}-.+/)
+ expect(another_product.slug).to match(/deleted-#{name}-.+/)
+
+ deleted_slug_2 = product.slugs.with_deleted.first
+ expect(deleted_slug_2.slug).to match(/deleted-#{name}-.+/)
+ expect(product.slug).to match(/deleted-#{name}-.+/)
end
end
@@ -56,7 +79,7 @@ describe Spree::Product::Slugs, type: :model do
context 'when product destroyed' do
it 'renames slug' do
product.destroy!
- expect(product.slug).to match(/deleted-product-[0-9]+/)
+ expect(product.slug).to match(/deleted-product-[0-9]+-.+/)
end
context 'when more than one translation exists' do
@@ -68,8 +91,8 @@ describe Spree::Product::Slugs, type: :model do
it 'renames slug for all translations' do
product.destroy!
- expect(product.slug).to match(/deleted-product-[0-9]+/)
- expect(product.translations.with_deleted.where(locale: :fr).first.slug).to match(/deleted-\d+_french-slug/)
+ expect(product.slug).to match(/deleted-product-[0-9]+-.+/)
+ expect(product.translations.with_deleted.where(locale: :fr).first.slug).to match(/deleted-french-slug-.+/)
end
end
@@ -113,23 +136,32 @@ describe Spree::Product::Slugs, type: :model do
end
context 'history' do
- before do
- @product = create(:product, stores: [store])
- end
+ let(:product) { create(:product, name: 'Product 67345', stores: [store]) }
- it 'keeps the history when the product is destroyed' do
- @product.destroy
+ context 'when product is destroyed' do
+ before do
+ product.destroy
+ end
- expect(@product.slugs.with_deleted).not_to be_empty
+ it 'keeps the history' do
+ expect(product.slugs.with_deleted).not_to be_empty
+ end
end
- it 'updates the history when the product is restored' do
- @product.destroy
+ context 'when product is restored' do
+ before do
+ product.destroy
+ product.restore(recursive: true)
+ end
- @product.restore(recursive: true)
+ it 'updates the history' do
+ expect(product.slugs).not_to be_empty
+ expect(product.slugs.find_by(slug: 'product-67345')).to be_present
+ end
- latest_slug = @product.slugs.find_by slug: @product.slug
- expect(latest_slug).not_to be_nil
+ it 'regenerates the product slug' do
+ expect(product.slug).to eq('product-67345')
+ end
end
end