Module: Mongoid::SleepingKingStudios::Orderable::ClassMethods

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

Overview

Class methods added to the base class via #extend.

Since:

  • 0.7.0

Instance Method Summary collapse

Instance Method Details

#cache_ordering(sort_params, options = {}) ⇒ Object

Creates the order field and sets up the callbacks and helpers.

Parameters:

  • sort_params (Array)

    The sort query used to order the collection. Accepts a subset of the options for a default Origin sort operation:

    • :field_name.desc, :another_field

    • { :field_name => -1, :another_field => 1 }

    • [[:field_name, -1], [:another_field, :asc]]

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

    The options for the relation.

Options Hash (options):

  • :as (Symbol)

    Sets the name of the generated field and helpers. By default, uses the name(s) and direction(s) of the fields from the sort query, e.g. :field_name_asc_another_field_desc_order.

  • :filter (Hash)

    Sets a filter that excludes collection items from the ordering process. Accepts the same parameters as a Mongoid #where query.

Raises:

  • (Mongoid::Errors::InvalidOptions)

    If any of the provided options are invalid.

Since:

  • 0.7.0


188
189
190
191
# File 'lib/mongoid/sleeping_king_studios/orderable.rb', line 188

def cache_ordering *sort_params, **options
  concern = Mongoid::SleepingKingStudios::Orderable
  concern.apply self, sort_params, options
end

#first_ordering_nameMongoid::Document?

Finds the first document, based on the stored ordering values.

The generated name of this method will depend on the sort params or the :as option provided. For example, :as => :alphabetical_order will result in an instance method #first_alphabetical.

Returns:

  • (Mongoid::Document, nil)

    The first document in the order, or nil if there are no documents in the collection.


# File 'lib/mongoid/sleeping_king_studios/orderable.rb', line 193

#last_ordering_nameMongoid::Document?

Finds the last document, based on the stored ordering values.

The generated name of this method will depend on the sort params or the :as option provided. For example, :as => :alphabetical_order will result in an instance method #last_alphabetical.

Returns:

  • (Mongoid::Document, nil)

    The last document in the order, or nil if there are no documents in the collection.


# File 'lib/mongoid/sleeping_king_studios/orderable.rb', line 203

#reorder_ordering_name!Object

Iterates through the entire collection and sets the cached order of each item to its current order index. Filtered items have their order set to nil. Normally, this should be taken care of when the items are saved, but this method allows the process to be reset in case of data corruption or other issues.

The generated name of this method will depend on the sort params or the :as option provided. For example, :as => :alphabetical_order will result in a class method ::reorder_alphabetical!.


# File 'lib/mongoid/sleeping_king_studios/orderable.rb', line 213