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_slugged’ 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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/models/concerns/acts_as_slugged.rb', line 187

def build_slug
  slug = 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
    uid = Time.zone.now.nsec.to_s(16) # This is a unique 7-8 digit url safe hex string
    slug = "#{slug}-#{uid}"
  end

  slug
end

#reload(options = nil) ⇒ Object



211
212
213
214
215
216
# File 'app/models/concerns/acts_as_slugged.rb', line 211

def reload(options = nil)
  self.class.instance_variable_set(:@_effective_reloading, true)
  retval = super
  self.class.instance_variable_set(:@_effective_reloading, nil)
  retval
end

#to_global_id(**params) ⇒ Object



206
207
208
209
# File 'app/models/concerns/acts_as_slugged.rb', line 206

def to_global_id(**params)
  params[:tenant] = Tenant.current if defined?(Tenant)
  GlobalID.new(URI::GID.build(app: Rails.application.config.global_id.app, model_name: model_name, model_id: to_param, params: params))
end

#to_paramObject



202
203
204
# File 'app/models/concerns/acts_as_slugged.rb', line 202

def to_param
  slug_was || slug
end