module Sluggable

Public Instance Methods

generate_slug!(attribute) click to toggle source
# File lib/ckb-sluggable.rb, line 6
def generate_slug!(attribute)
  temp_slug = to_slug(self[attribute])

  counter = 0
  while self.class.find_by(slug: temp_slug)
    counter += 1

    # if conditional makes sure we only remove a number we added
    temp_slug.gsub!(/-\d\z/, "") if counter > 1
    temp_slug = "#{temp_slug}-#{counter}"
  end

  self.slug = temp_slug
end
to_param() click to toggle source
# File lib/ckb-sluggable.rb, line 21
def to_param
  slug
end
to_slug(text) click to toggle source
# File lib/ckb-sluggable.rb, line 2
def to_slug(text)
  text.strip.downcase.gsub(/\s+/, "-").gsub(/[^\w-]/, "")
end