Module: Mongoid::Slug

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/slug.rb,
lib/mongoid/slug/index.rb,
lib/mongoid/slug/version.rb,
lib/mongoid/slug/criteria.rb,
lib/mongoid/slug/paranoia.rb,
lib/mongoid/slug/unique_slug.rb

Overview

Slugs your Mongoid model.

Defined Under Namespace

Modules: ClassMethods, Index, Paranoia Classes: Criteria, UniqueSlug

Constant Summary collapse

VERSION =
'5.1.0'

Instance Method Summary collapse

Instance Method Details

#apply_slugObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mongoid/slug.rb', line 175

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
  _slugs.delete(new_slug) if _slugs

  if !!history && _slugs.is_a?(Array)
    append_slug(new_slug)
  else
    self._slugs = [new_slug]
  end
end

#build_slugtrue

Builds a new slug.

Returns:

  • (true)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mongoid/slug.rb', line 158

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!Object

Sets the slug to its default value.



217
218
219
# File 'lib/mongoid/slug.rb', line 217

def clear_slug!
  self._slugs = []
end

#find_unique_slugString

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.

Returns:

  • (String)

    A unique slug



227
228
229
# File 'lib/mongoid/slug.rb', line 227

def find_unique_slug
  UniqueSlug.new(self).find_unique
end

#paranoid_deleted?Boolean

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

Returns:

  • (Boolean)

    Whether or not the document has been deleted in paranoid fashion



240
241
242
# File 'lib/mongoid/slug.rb', line 240

def paranoid_deleted?
  !!(self.class.is_paranoid_doc? && !deleted_at.nil?)
end

#reset_slug!Object

Rolls back the slug value from the Mongoid changeset.



212
213
214
# File 'lib/mongoid/slug.rb', line 212

def reset_slug!
  self.reset__slugs!
end

#set_slug!Object

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)



197
198
199
200
# File 'lib/mongoid/slug.rb', line 197

def set_slug!
  build_slug
  method(:set).arity == 1 ? set(_slugs: _slugs) : set(:_slugs, _slugs)
end

#slugString

Returns the slug, or nil if the document does not have a slug.

Returns:

  • (String)

    the slug, or nil if the document does not have a slug.



255
256
257
258
# File 'lib/mongoid/slug.rb', line 255

def slug
  return _slugs.last if _slugs
  _id.to_s
end

#slug_builderObject



260
261
262
263
264
265
266
267
268
# File 'lib/mongoid/slug.rb', line 260

def slug_builder
  cur_slug = nil
  if new_with_slugs? || 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?Boolean

Returns Whether the slug requires to be rebuilt.

Returns:

  • (Boolean)

    Whether the slug requires to be rebuilt



232
233
234
# File 'lib/mongoid/slug.rb', line 232

def slug_should_be_rebuilt?
  (new_record? || _slugs_changed? || slugged_attributes_changed?) && !paranoid_deleted?
end

#slugged_attributes_changed?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/mongoid/slug.rb', line 244

def slugged_attributes_changed?
  slugged_attributes.any? { |f| attribute_changed? f.to_s }
end

#to_paramString

to this record.

Returns:

  • (String)

    A string which Action Pack uses for constructing an URL



250
251
252
# File 'lib/mongoid/slug.rb', line 250

def to_param
  slug || super
end

#unset_slug!Object

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)



206
207
208
209
# File 'lib/mongoid/slug.rb', line 206

def unset_slug!
  unset(:_slugs)
  clear_slug!
end