Module: Sluggable

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#append_suffix(str, count) ⇒ Object



17
18
19
20
21
22
# File 'lib/sluggable_pocket.rb', line 17

def append_suffix(str, count)
  if str.split('-').last.to_i != 0
    str = str.split('-').slice(0..-2).join('-')
  end
  str << "-#{count.to_s}"
end

#create_slug(fulltext) ⇒ Object



13
14
15
# File 'lib/sluggable_pocket.rb', line 13

def create_slug(fulltext)
  slug = fulltext.downcase.strip.gsub(/[^\w-]/, "-").gsub(/-+/, "-")
end

#generate_slug!Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sluggable_pocket.rb', line 24

def generate_slug!
    slug = create_slug(self.send(self.class.slug_column.to_sym)) 
    i = 2
    record = self.class.find_by(slug: slug)
    while record && record != self
      slug = self.append_suffix(slug, i)
      record = self.class.find_by(slug: slug)
      i += 1
    end 
    self.slug = slug
end

#to_paramObject



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

def to_param
    slug
end