Module: JulienSluggable::InstanceMethods

Defined in:
lib/julien-sluggable.rb

Instance Method Summary collapse

Instance Method Details

#generate_slugObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/julien-sluggable.rb', line 11

def generate_slug
  slug = self.send(self.class::SLUG_BASE)
  slug = slug.gsub(' ', '_').gsub(/\W/, '').downcase

  unless self.class.find_by(slug: slug).nil?
    num = 1
    until self.class.find_by(slug: "#{slug}-#{num}").nil?
      num += 1
    end
    slug = "#{slug}-#{num}"
  end

  self.slug = slug
end

#to_paramObject



26
27
28
# File 'lib/julien-sluggable.rb', line 26

def to_param
  self.slug
end