mirror of
https://github.com/discourse/discourse.git
synced 2025-09-07 12:02:53 +08:00
Don't allow category slugs that are numbers
This commit is contained in:
parent
5d444be72b
commit
7d5c313456
2 changed files with 13 additions and 0 deletions
|
@ -85,6 +85,11 @@ class Category < ActiveRecord::Base
|
||||||
if name.present?
|
if name.present?
|
||||||
self.slug = Slug.for(name)
|
self.slug = Slug.for(name)
|
||||||
|
|
||||||
|
# Reject slugs that only contain numbers, because that's indistinguishable from an id.
|
||||||
|
self.slug = '' unless self.slug =~ /[^\d]/
|
||||||
|
|
||||||
|
return if self.slug.blank?
|
||||||
|
|
||||||
# If a category with that slug already exists, set the slug to nil so the category can be found
|
# If a category with that slug already exists, set the slug to nil so the category can be found
|
||||||
# another way.
|
# another way.
|
||||||
category = Category.where(slug: self.slug)
|
category = Category.where(slug: self.slug)
|
||||||
|
|
|
@ -104,6 +104,14 @@ describe Category do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'slug would be a number' do
|
||||||
|
let(:category) { Fabricate(:category, name: "電車男 2") }
|
||||||
|
|
||||||
|
it 'creates a blank slug' do
|
||||||
|
category.slug.should be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'after create' do
|
describe 'after create' do
|
||||||
before do
|
before do
|
||||||
@category = Fabricate(:category, name: 'Amazing Category')
|
@category = Fabricate(:category, name: 'Amazing Category')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue