Module: Mongoid::SleepingKingStudios::Sluggable::ClassMethods

Defined in:
lib/mongoid/sleeping_king_studios/sluggable.rb

Overview

Class methods added to the base class via #extend.

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#slugify(attribute, options = {}) ⇒ Object

Creates the :slug field and sets up the callback and validations.

Parameters:

  • attribute (String, Symbol)

    The base field used to determine the value of the slug. When this field is changed via its writer method, the slug will be updated.

  • options (Hash) (defaults to: {})

    The options for the relation.

Options Hash (options):

  • :lockable (Boolean)

    The :lockable option allows the manual setting of the :slug field. To do so, it adds an additional :slug_lock field, which defaults to false but is set to true whenever #slug= is called. If the slug is locked, its value is not updated to track the base attribute. To resume tracking the base attribute, set :slug_lock to false.

Raises:

  • (Mongoid::Errors::InvalidOptions)

    If any of the provided options are invalid.

Since:

  • 0.1.0


224
225
226
227
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 224

def slugify attribute, **options
  concern = Mongoid::SleepingKingStudios::Sluggable
  concern.apply self, attribute, options
end

#slugify_all!Object

Loops through all documents in the collection. If the document's slug is blank, or if it does not match the base attribute value, calculates the value from the base attribute and assigns it atomically. Locked slugs (see the :lockable option) are unaffected.

Use this method to generate slugs when adding this concern to a model with existing documents.

Since:

  • 0.7.7


# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 229

#value_to_slug(value) ⇒ String

Converts the provided string to a slug value. Delegates to metadata.value_to_slug, so overriding this method will not change how slugs are generated.

Parameters:

  • value (String)

    The string to convert into a slug.

Returns:

  • (String)

    The converted string.

Since:

  • 0.7.8


# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 240