Move logic to reject slugs that are just numbers into the slug module

This commit is contained in:
Neil Lalonde 2013-05-30 11:54:02 -04:00
parent 7d5c313456
commit b82a5dfd56
3 changed files with 10 additions and 4 deletions

View file

@ -85,9 +85,6 @@ 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? 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

View file

@ -6,7 +6,8 @@
module Slug module Slug
def self.for(string) def self.for(string)
string.parameterize.gsub("_", "-") slug = string.parameterize.gsub("_", "-")
slug =~ /[^\d]/ ? slug : '' # Reject slugs that only contain numbers, because they would be indistinguishable from id's.
end end
end end

View file

@ -43,5 +43,13 @@ describe Slug do
Slug.for("o_o_o").should == "o-o-o" Slug.for("o_o_o").should == "o-o-o"
end end
it "doesn't generate slugs that are just numbers" do
Slug.for('123').should be_blank
end
it "doesn't generate slugs that are just numbers" do
Slug.for('電車男 2').should be_blank
end
end end