Module: Mongoid::Slug

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/slug.rb,
lib/mongoid/slug/index.rb,
lib/mongoid/slug/railtie.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, Railtie, UniqueSlug

Constant Summary collapse

MONGO_INDEX_KEY_LIMIT_BYTES =
1024
VERSION =
'5.2.0'

Instance Method Summary collapse

Instance Method Details

#apply_slugObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mongoid/slug.rb', line 196

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.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/mongoid/slug.rb', line 179

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.



238
239
240
# File 'lib/mongoid/slug.rb', line 238

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.



248
249
250
# File 'lib/mongoid/slug.rb', line 248

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



261
262
263
# File 'lib/mongoid/slug.rb', line 261

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

#reset_slug!Object

Rolls back the slug value from the Mongoid changeset.



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

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)



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

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

#slugString



276
277
278
279
# File 'lib/mongoid/slug.rb', line 276

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

#slug_builderObject



281
282
283
284
285
286
287
288
289
# File 'lib/mongoid/slug.rb', line 281

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



253
254
255
# File 'lib/mongoid/slug.rb', line 253

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

#slugged_attributes_changed?Boolean



265
266
267
# File 'lib/mongoid/slug.rb', line 265

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

#to_paramString

to this record.



271
272
273
# File 'lib/mongoid/slug.rb', line 271

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)



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

def unset_slug!
  unset(:_slugs)
  clear_slug!
end