Module: Sluggable

Extended by:
ActiveSupport::Concern
Defined in:
lib/sluggable_pparkernoverant.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_slug!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sluggable_pparkernoverant.rb', line 13

def generate_slug!
  slug_base = to_slug(self.send(self.class.slug_column.to_sym))
  slug_current = slug_base

  obj = self.class.find_by slug: slug_current
  count = 2

  while obj && obj != self
    slug_current = slug_base + "-#{count}"
    obj = self.class.find_by slug: slug_current
    count += 1
  end
  self.slug = slug_current
end

#to_paramObject



9
10
11
# File 'lib/sluggable_pparkernoverant.rb', line 9

def to_param
  self.slug
end

#to_slug(name) ⇒ Object



28
29
30
31
32
33
# File 'lib/sluggable_pparkernoverant.rb', line 28

def to_slug(name)
  str = name.strip
  str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
  str.gsub! /-+/, '-'
  str.downcase
end