module Mongoid::Slug

Slugs your Mongoid model.

Constants

VERSION

Public Class Methods

mongoid3?() click to toggle source
# File lib/mongoid/slug.rb, line 268
def self.mongoid3?
  ::Mongoid.const_defined? :Observer
end

Public Instance Methods

apply_slug() click to toggle source
# File lib/mongoid/slug.rb, line 173
def apply_slug
  _new_slug = find_unique_slug

  #skip slug generation and use Mongoid id
  #to find document instead
  return true if _new_slug.size == 0

  # avoid duplicate slugs
  self._slugs.delete(_new_slug) if self._slugs

  if !!self.history && self._slugs.is_a?(Array)
    append_slug(_new_slug)
  else
    self._slugs = [_new_slug]
  end
end
build_slug() click to toggle source

Builds a new slug.

@return [true]

# File lib/mongoid/slug.rb, line 156
def build_slug
  if localized?
    begin
      orig_locale = I18n.locale
      all_locales.each do |target_locale|
        I18n.locale = target_locale
        apply_slug
      end
    ensure
      I18n.locale = orig_locale
    end
  else
    apply_slug
  end
  true
end
clear_slug!() click to toggle source

Sets the slug to its default value.

# File lib/mongoid/slug.rb, line 215
def clear_slug!
  self._slugs = []
end
find_unique_slug() click to toggle source

Finds a unique slug, were specified string used to generate a slug.

Returned slug will the same as the specified string when there are no duplicates.

@return [String] A unique slug

# File lib/mongoid/slug.rb, line 225
def find_unique_slug
  UniqueSlug.new(self).find_unique
end
paranoid_deleted?() click to toggle source

Indicates whether or not the document has been deleted in paranoid fashion Always returns false if the document is not paranoid

@return [Boolean] Whether or not the document has been deleted in paranoid fashion

# File lib/mongoid/slug.rb, line 238
def paranoid_deleted?
  !!(self.class.is_paranoid_doc? and self.deleted_at != nil)
end
reset_slug!() click to toggle source

Rolls back the slug value from the Mongoid changeset.

# File lib/mongoid/slug.rb, line 210
def reset_slug!
  self.reset__slugs!
end
set_slug!() click to toggle source

Builds slug then atomically sets it in the database. This is used when working with the Mongoid::Paranoia restore callback.

This method is adapted to use the :set method variants from both Mongoid 3 (two args) and Mongoid 4 (hash arg)

# File lib/mongoid/slug.rb, line 195
def set_slug!
  build_slug
  self.method(:set).arity == 1 ? set({_slugs: self._slugs}) : set(:_slugs, self._slugs)
end
slug() click to toggle source

@return [String] the slug, or nil if the document does not have a slug.

# File lib/mongoid/slug.rb, line 253
def slug
  return _slugs.last if _slugs
  return _id.to_s
end
slug_builder() click to toggle source
# File lib/mongoid/slug.rb, line 258
def slug_builder
  _cur_slug = nil
  if new_with_slugs? or persisted_with_slug_changes?
    #user defined slug
    _cur_slug = _slugs.last
  end
  #generate slug if the slug is not user defined or does not exist
  _cur_slug || pre_slug_string
end
slug_should_be_rebuilt?() click to toggle source

@return [Boolean] Whether the slug requires to be rebuilt

# File lib/mongoid/slug.rb, line 230
def slug_should_be_rebuilt?
  (new_record? or _slugs_changed? or slugged_attributes_changed?) and !paranoid_deleted?
end
slugged_attributes_changed?() click to toggle source
# File lib/mongoid/slug.rb, line 242
def slugged_attributes_changed?
  slugged_attributes.any? { |f| attribute_changed? f.to_s }
end
to_param() click to toggle source

@return [String] A string which Action Pack uses for constructing an URL to this record.

Calls superclass method
# File lib/mongoid/slug.rb, line 248
def to_param
  slug || super
end
unset_slug!() click to toggle source

Atomically unsets the slug field in the database. It is important to unset the field for the sparse index on slugs.

This also resets the in-memory value of the slug field to its default (empty array)

# File lib/mongoid/slug.rb, line 204
def unset_slug!
  unset(:_slugs)
  clear_slug!
end

Private Instance Methods

all_locales() click to toggle source

Return all possible locales for model Avoiding usage of I18n.available_locales in case the user hasn’t set it properly, or is doing something crazy, but at the same time we need a fallback in case the model doesn’t have any localized attributes at all (extreme edge case).

# File lib/mongoid/slug.rb, line 321
def all_locales
  locales = self.slugged_attributes
                .map{|attr| self.send("#{attr}_translations").keys if self.respond_to?("#{attr}_translations")}
                .flatten.compact.uniq
  locales = I18n.available_locales if locales.empty?
  locales
end
append_slug(_slug) click to toggle source
# File lib/mongoid/slug.rb, line 274
def append_slug(_slug)
  if localized?
    # This is necessary for the scenario in which the slugged locale is not yet present
    # but the default locale is. In this situation, self._slugs falls back to the default
    # which is undesired
    current_slugs = self._slugs_translations.fetch(I18n.locale.to_s, [])
    current_slugs << _slug
    self._slugs_translations = self._slugs_translations.merge(I18n.locale.to_s => current_slugs)
  else
    self._slugs << _slug
  end
end
localized?() click to toggle source
# File lib/mongoid/slug.rb, line 313
def localized?
  self.fields['_slugs'].options[:localize] rescue false
end
new_with_slugs?() click to toggle source

Returns true if object is a new record and slugs are present

# File lib/mongoid/slug.rb, line 288
def new_with_slugs?
  if localized?
    # We need to check if slugs are present for the locale without falling back
    # to a default
    new_record? and _slugs_translations.fetch(I18n.locale.to_s, []).any?
  else
    new_record? and _slugs.present?
  end
end
persisted_with_slug_changes?() click to toggle source

Returns true if object has been persisted and has changes in the slug

# File lib/mongoid/slug.rb, line 299
def persisted_with_slug_changes?
  if localized?
    changes = self._slugs_change
    return (persisted? and false) if changes.nil?

    # ensure we check for changes only between the same locale
    original = changes.first.try(:fetch, I18n.locale.to_s, nil)
    compare = changes.last.try(:fetch, I18n.locale.to_s, nil)
    persisted? and original != compare
  else
    persisted? and _slugs_changed?
  end
end
pre_slug_string() click to toggle source
# File lib/mongoid/slug.rb, line 329
def pre_slug_string
  self.slugged_attributes.map { |f| self.send f }.join ' '
end