Module: ActsAsSlugged

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/acts_as_slugged.rb

Overview

ActsAsSlugged

This module automatically generates slugs based on the :to_s field using a before_validation filter

Mark your model with ‘acts_as_sluggable’ make sure you have a string field :slug

Defined Under Namespace

Modules: Base, ClassMethods, FinderMethods

Instance Method Summary collapse

Instance Method Details

#build_slugObject

Instance Methods



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/concerns/acts_as_slugged.rb', line 46

def build_slug
  slug = self.to_s.parameterize.downcase[0, 250]

  if self.class.excluded_slugs.include?(slug)
    slug = "#{slug}-#{self.class.name.demodulize.parameterize}"
  end

  if (count = self.class.where(slug: slug).count) > 0
    slug = "#{slug}-#{count+1}"
  end

  slug
end

#to_paramObject



60
61
62
# File 'app/models/concerns/acts_as_slugged.rb', line 60

def to_param
  slug
end