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.

V-3068 ADD random number to slug when product name already taken (#12657)

brozek077f19dc7bec
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index 94463efecb..e965e4c686 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -15,6 +15,7 @@ module Spree
before_action :prepare_product_params, only: [:create, :update]
before_action :strip_stock_items_param, only: [:create, :update]
before_action :ensure_session_uploaded_assets_uuid, only: :new
+ before_action :check_slug_availability, only: [:create, :update]
new_action.before :build_master_prices
new_action.before :build_master_stock_items
@@ -354,6 +355,11 @@ module Spree
variant_images: [],
}
end
+
+ def check_slug_availability
+ new_slug = permitted_resource_params[:slug]
+ permitted_resource_params[:slug] = @product.ensure_slug_is_unique(new_slug)
+ end
end
end
end
diff --git a/admin/app/javascript/spree/admin/controllers/slug_form_controller.js b/admin/app/javascript/spree/admin/controllers/slug_form_controller.js
index ff237fd322..0f943e5209 100644
--- a/admin/app/javascript/spree/admin/controllers/slug_form_controller.js
+++ b/admin/app/javascript/spree/admin/controllers/slug_form_controller.js
@@ -10,8 +10,5 @@ export default class extends Controller {
const name = this.nameTarget.value
const url = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)+/g, '')
this.urlTarget.value = url
-
- // TODO: do ajax call and check if url is available
- // if not, append a number to the end to make it unique
}
}
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 37be77b032..3687e15d45 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -1070,30 +1070,36 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
describe 'failing to update product' do
- let!(:existing_product) { create(:product, name: 'Existing Product', slug: 'existing-product', stores: [store]) }
+ let(:product_params) { { name: '' } }
- let(:product_params) do
- {
- name: 'Existing Product',
- slug: 'existing-product'
- }
- end
-
- context 'using the same slug' do
- before do
- send_request
- end
+ context 'using empty name' do
+ before { send_request }
it 'renders the edit page' do
expect(response).to render_template(:edit)
expect(response).to have_http_status(:unprocessable_entity)
end
- it 'should render the error' do
- expect(response.body).to include('Slug has already been taken')
+ it 'renders the error' do
+ expect(response.body).to include('Name can't be blank')
end
end
end
+
+ describe 'using same slug' do
+ let!(:product) { create(:product, name: 'Existing Product', slug: 'existing-product', stores: [store]) }
+ let!(:product_2) { create(:product, name: 'Existing Product', slug: 'existing-product-2', stores: [store]) }
+ let(:product_params) { { slug: 'existing-product-2' } }
+
+ before do
+ allow(SecureRandom).to receive(:uuid).and_return('2dc1cfdf-81fe-4983-8709-ef6ee843c41d')
+ send_request
+ end
+
+ it 'updates the slug with uuid' do
+ expect(product.reload.slug).to eq('existing-product-2-2dc1cfdf-81fe-4983-8709-ef6ee843c41d')
+ end
+ end
end
describe 'PUT #clone' do
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index 884858278a..01997389dc 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -12,6 +12,7 @@ module Spree
@taxons = taxon_ids(params.dig(:filter, :taxons))
@concat_taxons = taxon_ids(params.dig(:filter, :concat_taxons))
@name = params.dig(:filter, :name)
+ @slug = params.dig(:filter, :slug)
@options = params.dig(:filter, :options).try(:to_unsafe_hash)
@option_value_ids = params.dig(:filter, :option_value_ids)
@sort_by = params.dig(:sort_by)
@@ -41,6 +42,7 @@ module Spree
products = by_taxons(products)
products = by_concat_taxons(products)
products = by_name(products)
+ products = by_slug(products)
products = by_options(products)
products = by_option_value_ids(products)
products = by_properties(products)
@@ -60,7 +62,7 @@ module Spree
attr_reader :ids, :skus, :price, :currency, :taxons, :concat_taxons, :name, :options, :option_value_ids, :scope,
:sort_by, :deleted, :discontinued, :properties, :store, :in_stock, :backorderable, :purchasable, :tags,
- :query, :vendor_ids, :out_of_stock
+ :query, :vendor_ids, :out_of_stock, :slug
def query?
query.present?
@@ -94,6 +96,10 @@ module Spree
name.present?
end
+ def slug?
+ slug.present?
+ end
+
def options?
options.present?
end
@@ -172,6 +178,12 @@ module Spree
products.i18n { name.matches("%#{product_name}%") }
end
+ def by_slug(products)
+ return products unless slug.present?
+
+ products.i18n.where(slug: slug)
+ end
+
def by_options(products)
return products unless options?
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 213790d21d..1c21074a18 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -512,6 +512,17 @@ module Spree
where conditions.inject(:or)
end
+ def self.slug_available?(slug, id)
+ !where(slug: slug).where.not(id: id).exists?
+ end
+
+ def ensure_slug_is_unique(candidate_slug)
+ return slug if candidate_slug.blank? || slug.blank?
+ return candidate_slug if self.class.slug_available?(candidate_slug, id)
+
+ normalize_friendly_id([candidate_slug, uuid_for_friendly_id])
+ end
+
# Suitable for displaying only variants that has at least one option value.
# There may be scenarios where an option type is removed and along with it
# all option values. At that point all variants associated with only those
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 9f5551d260..c1952f06b0 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -94,6 +94,7 @@ module Spree
:option_values_hash, :weight, :height, :width, :depth,
:shipping_category_id, :tax_category_id,
:cost_currency, :cost_price, :compare_at_price,
+ :slug,
{
tag_list: [],
option_type_ids: [],
diff --git a/core/spec/finders/spree/products/find_spec.rb b/core/spec/finders/spree/products/find_spec.rb
index b6c9b0eb36..66c4029bec 100644
--- a/core/spec/finders/spree/products/find_spec.rb
+++ b/core/spec/finders/spree/products/find_spec.rb
@@ -547,5 +547,35 @@ module Spree
expect(products).to match_array([product_3, product_2, product, in_stock_product, not_backorderable_product])
end
end
+
+ describe 'filter by slug' do
+ subject(:products) { described_class.new(scope: Spree::Product.all, params: params).execute }
+
+ let(:params) { { filter: { slug: 'slug-1' } } }
+
+ before { product.update(slug: 'slug-1') }
+
+ context 'when product with given slug is present' do
+ it 'returns products with the given slug' do
+ expect(products).to contain_exactly(product)
+ end
+ end
+
+ context 'when product with given slug is not present' do
+ let(:params) { { filter: { slug: 'slug-2' } } }
+
+ it 'returns all products' do
+ expect(products).to be_empty
+ end
+ end
+
+ context 'when slug is not present' do
+ let(:params) { { filter: { slug: '' } } }
+
+ it 'returns all products' do
+ expect(products).to contain_exactly(product, product_2, product_3, in_stock_product, not_backorderable_product)
+ end
+ end
+ end
end
end