Module: Sluggable

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_slugObject



13
14
15
16
17
18
19
20
21
# File 'lib/sluggable_kel.rb', line 13

def generate_slug
  the_slug = to_slug(self.send(self.class.slug_column.to_sym))
  obj = self.class.find_by slug: the_slug
  if obj && obj != self
    obj = self.class.order(:created_at).last
    the_slug << "-#{obj.slug[/\d+$/].to_i + 1}"
  end
  self.slug = the_slug
end

#to_paramObject



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

def to_param
  self.slug
end

#to_slug(column) ⇒ Object



23
24
25
26
27
# File 'lib/sluggable_kel.rb', line 23

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