module Turboslugs::InstanceMethods

Public Instance Methods

to_param() click to toggle source
# File lib/turboslugs.rb, line 14
def to_param
  self.slug
end

Private Instance Methods

generate_slug() click to toggle source
# File lib/turboslugs.rb, line 20
def generate_slug
  return unless !!slug_base

  base = slug_base
  slug = slugify(base)
  counter = 1

  while restricted?(slug) || taken?(slug)
    slug = slugify(base)
    counter += 1

    slug += "_#{counter}"
  end

  self.slug = slug

  slug
end
restricted?(slug) click to toggle source
# File lib/turboslugs.rb, line 39
def restricted? slug
  self.class.restricted_slugs.include?(slug.to_sym)
end
slug_base() click to toggle source
# File lib/turboslugs.rb, line 49
def slug_base
  base = self.class.slug_base
  send base if base
end
slugify(string) click to toggle source
# File lib/turboslugs.rb, line 54
def slugify string
  string.downcase.gsub(/\s/, '_').gsub(/\W/, '')
end
taken?(slug) click to toggle source
# File lib/turboslugs.rb, line 43
def taken? slug
  match = self.class.find_by(slug: slug)

  !!match && (match != self)
end