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



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

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



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

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



208
209
210
211
# File 'app/models/concerns/acts_as_slugged.rb', line 208

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



204
205
206
# File 'app/models/concerns/acts_as_slugged.rb', line 204

def to_param
  slug_was || slug
end