class Mongoid::SleepingKingStudios::Orderable::Metadata

Stores information about an Orderable concern.

Public Class Methods

default_field_name(sort_params) click to toggle source
# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 10
def default_field_name sort_params
  (sort_params.map { |key, value|
    "#{key}_#{value == 1 ? 'asc' : 'desc'}"
  }.join('_') + '_order').intern
end
normalize_sort_params(sort_params) click to toggle source
# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 16
def normalize_sort_params sort_params
  case sort_params
  when Array
    sort_params.reduce({}) do |hsh, param|
      hsh.merge parse_sort_param(param)
    end # each
  when Hash
    sort_params.each.with_object({}) do |(key, value), hsh|
      hsh[key] = parse_sort_direction(value)
    end # each
  when Symbol, Origin::Key
    parse_sort_param(sort_params)
  end # case
end

Private Class Methods

parse_sort_direction(direction) click to toggle source
# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 33
def parse_sort_direction direction
  (direction == -1 || direction.to_s.downcase == 'desc') ? -1 : 1
end
parse_sort_param(param) click to toggle source
# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 37
def parse_sort_param param
  case param
  when Array
    { param[0] => parse_sort_direction(param[1]) }
  when Origin::Key
    { param.name => param.operator }
  when Symbol
    { param => 1 }
  end # case
end

Public Instance Methods

field_name() click to toggle source

The name of the field used to store the order.

@return [Symbol] The field name.

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 52
def field_name
  fetch(:as, Metadata.default_field_name(self[:sort_params])).intern
end
field_name?() click to toggle source

@return [Boolean] True if a custom field name is defined; otherwise false.

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 58
def field_name?
  !!self[:as]
end
field_was() click to toggle source

The name of the dirty tracking method for the order field.

@return [Symbol] The method name.

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 65
def field_was
  :"#{field_name}_was"
end
field_writer() click to toggle source

The name of the writer for the order field.

@return [Symbol] The method name.

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 72
def field_writer
  :"#{field_name}="
end
filter_criteria(criteria) click to toggle source

The criteria to filter only the desired collection items to sort.

@param [Mongoid::Criteria] criteria The base criteria to modify using

the filter params.

@return [Mongoid::Criteria]

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 82
def filter_criteria criteria
  filter_params? ? criteria.where(filter_params) : criteria
end
filter_params() click to toggle source

The options (if any) to filter the collection by prior to sorting.

@return [Hash]

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 89
def filter_params
  self[:filter]
end
filter_params?() click to toggle source

@return [Boolean] True if filter params are defined; otherwise false.

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 94
def filter_params?
  !!self[:filter]
end
sort_criteria(criteria) click to toggle source

The criteria to be used when sorting the collection.

@param [Mongoid::Criteria] criteria The base criteria to modify using

the sort params.

@return [Mongoid::Criteria]

# File lib/mongoid/sleeping_king_studios/orderable/metadata.rb, line 104
def sort_criteria criteria
  filter_criteria(criteria).order_by(self[:sort_params])
end