Module: Slugifyeobrien
- Defined in:
- lib/slugify-eobrien.rb
Overview
assumes slugging after validation
assumes
def slug_this
generate_slug(Category, self.foo)
end
in each model to be slugged
Instance Method Summary collapse
-
#generate_slug(model, var) ⇒ Object
Post, self.title.
- #to_param ⇒ Object
- #to_slug(name) ⇒ Object
Instance Method Details
#generate_slug(model, var) ⇒ Object
Post, self.title
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/slugify-eobrien.rb', line 19 def generate_slug(model, var) #Post, self.title str = to_slug(var) count = 2 obj = model.where(slug: str).first while obj && obj != self str = str + "-" + count.to_s obj = model.where(slug: str).first count += 1 end self.slug = str.downcase end |
#to_param ⇒ Object
15 16 17 |
# File 'lib/slugify-eobrien.rb', line 15 def to_param self.slug end |
#to_slug(name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/slugify-eobrien.rb', line 32 def to_slug(name) #strip the string ret = name.strip #blow away apostrophes ret.gsub! /['`]/,"" # @ --> at, and & --> and ret.gsub! /\s*@\s*/, " at " ret.gsub! /\s*&\s*/, " and " #replace all non alphanumeric with dash ret.gsub! /\s*[^A-Za-z0-9]\s*/, '-' #convert double dashes to single ret.gsub! /-+/,"-" #strip off leading/trailing dash ret.gsub! /\A[-\.]+|[-\.]+\z/,"" ret end |