From b82a5dfd5642b4b8440752d1cb9f612785b94ca7 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 30 May 2013 11:54:02 -0400 Subject: [PATCH] Move logic to reject slugs that are just numbers into the slug module --- app/models/category.rb | 3 --- lib/slug.rb | 3 ++- spec/components/slug_spec.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index d52a6941d25..c7e3e9d9dff 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -85,9 +85,6 @@ 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 diff --git a/lib/slug.rb b/lib/slug.rb index 08555448f60..f44bce6fee1 100644 --- a/lib/slug.rb +++ b/lib/slug.rb @@ -6,7 +6,8 @@ module Slug 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 diff --git a/spec/components/slug_spec.rb b/spec/components/slug_spec.rb index 614dce90810..3c5d23cbcdf 100644 --- a/spec/components/slug_spec.rb +++ b/spec/components/slug_spec.rb @@ -43,5 +43,13 @@ describe Slug do Slug.for("o_o_o").should == "o-o-o" 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