Class: Mongoid::SleepingKingStudios::Orderable::Metadata

Inherits:
Concern::Metadata show all
Defined in:
lib/mongoid/sleeping_king_studios/orderable/metadata.rb

Overview

Stores information about an Orderable concern.

Since:

  • 0.7.0

Instance Attribute Summary

Attributes inherited from Concern::Metadata

#name, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Concern::Metadata

#characterize, #initialize, #relation_key, #relation_key?

Constructor Details

This class inherits a constructor from Mongoid::SleepingKingStudios::Concern::Metadata

Class Method Details

.default_field_name(sort_params) ⇒ Object

Since:

  • 0.7.0


10
11
12
13
14
# 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) ⇒ Object

class method default_field_name

Since:

  • 0.7.0


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#field_nameSymbol

The name of the field used to store the order.

Returns:

  • (Symbol)

    The field name.

Since:

  • 0.7.0


52
53
54
# 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?Boolean

false.

Returns:

  • (Boolean)

    True if a custom field name is defined; otherwise

Since:

  • 0.7.0


58
59
60
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 58

def field_name?
  !!self[:as]
end

#field_wasSymbol

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

Returns:

  • (Symbol)

    The method name.

Since:

  • 0.7.0


65
66
67
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 65

def field_was
  :"#{field_name}_was"
end

#field_writerSymbol

The name of the writer for the order field.

Returns:

  • (Symbol)

    The method name.

Since:

  • 0.7.0


72
73
74
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 72

def field_writer
  :"#{field_name}="
end

#filter_criteria(criteria) ⇒ Mongoid::Criteria

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

Parameters:

  • criteria (Mongoid::Criteria)

    The base criteria to modify using the filter params.

Returns:

  • (Mongoid::Criteria)

Since:

  • 0.7.0


82
83
84
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 82

def filter_criteria criteria
  filter_params? ? criteria.where(filter_params) : criteria
end

#filter_paramsHash

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

Returns:

  • (Hash)

Since:

  • 0.7.0


89
90
91
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 89

def filter_params
  self[:filter]
end

#filter_params?Boolean

Returns True if filter params are defined; otherwise false.

Returns:

  • (Boolean)

    True if filter params are defined; otherwise false.

Since:

  • 0.7.0


94
95
96
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 94

def filter_params?
  !!self[:filter]
end

#sort_criteria(criteria) ⇒ Mongoid::Criteria

The criteria to be used when sorting the collection.

Parameters:

  • criteria (Mongoid::Criteria)

    The base criteria to modify using the sort params.

Returns:

  • (Mongoid::Criteria)

Since:

  • 0.7.0


104
105
106
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 104

def sort_criteria criteria
  filter_criteria(criteria).order_by(self[:sort_params])
end