Don't allow category slugs that are numbers

This commit is contained in:
Neil Lalonde 2013-05-30 11:09:09 -04:00
parent 5d444be72b
commit 7d5c313456
2 changed files with 13 additions and 0 deletions

View file

@ -85,6 +85,11 @@ class Category < ActiveRecord::Base
if name.present?
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
# another way.
category = Category.where(slug: self.slug)

View file

@ -104,6 +104,14 @@ describe Category do
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
before do
@category = Fabricate(:category, name: 'Amazing Category')