I am using friendly_id for my rails app and noticed it put in a slug, word-for-word. I was wondering if there was a way to parse the slug from containing non-SEO relevant words such as "and" and "for". I have tried some REGEX
but I'm not even sure if it applies to friendly_id
.
In my Application Record (Model):
def to_slug(param=self.slug)
# strip the string
ret = param.strip
#blow away apostrophes
ret.gsub! /['`]/, ""
# @ --> at, and & --> and
ret.gsub! /\s*@\s*/, " at "
ret.gsub! /\s*&\s*/, " and "
# replace all non alphanumeric, periods with dash
ret.gsub! /\s*[^A-Za-z0-9\.]\s*/, '-'
# replace underscore with dash
ret.gsub! /[-_]{2,}/, '-'
# convert double dashes to single
ret.gsub! /-+/, "-"
# strip off leading/trailing dash
ret.gsub! /\A[-\.]+|[-\.]+\z/, ""
ret
end
I'm definitely no wiz at regular expression, I would love some help on this one, thanks.
P.S. Does my to_slug method even apply to friendly_id
? I cant tell if the gem already performs all of these actions or not, thanks!