Module: SluggableGem

Extended by:
ActiveSupport::Concern
Defined in:
lib/sluggable_tom.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
# File 'lib/sluggable_tom.rb', line 13

def generate_slug!
  the_slug = self.send(self.class.slug_column.to_sym).parameterize

  while self.class.where(slug: the_slug).exists?
    if the_slug.split('-').last.to_i != 0
      the_slug = "#{the_slug.split('-').slice(0...-1).join('-')}-#{the_slug.split('-').last.to_i + 1}"
    else
      the_slug += "-2"
    end
  end
  self.slug = the_slug
end

#to_paramObject



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

def to_param
  self.slug
end