module Sluggable

Public Instance Methods

append(str, count) click to toggle source
# File lib/sluggable_ellery_mar.rb, line 25
def append(str, count)
  if str.split('-').last.to_i != 0
    return str.split('-').slice(0...-1).join('-') + '-' + count.to_s
  else
    return str + '-' + count.to_s
  end
end
generate_slug!() click to toggle source
# File lib/sluggable_ellery_mar.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)
  count = 2
  while obj && obj != self
    the_slug = append(the_slug, count)
    obj = self.class.find_by(slug: the_slug)
    count += 1
  end
  self.slug = the_slug
end
to_param() click to toggle source
# File lib/sluggable_ellery_mar.rb, line 9
def to_param
  self.slug
end
to_slug(title) click to toggle source
# File lib/sluggable_ellery_mar.rb, line 33
def to_slug(title)
  str = title.downcase
  str.gsub!(/\s*[^a-zA-Z0-9]\s*/, '-')
  arr = str.split('-')

  arr.each do |char|
    if char == ''
      arr.delete(char)
    end
  end

  return str = arr.join('-')
end