Class: Mongoid::SleepingKingStudios::Orderable::Metadata
- Inherits:
-
Concern::Metadata
- Object
- Hash
- Concern::Metadata
- Mongoid::SleepingKingStudios::Orderable::Metadata
- Defined in:
- lib/mongoid/sleeping_king_studios/orderable/metadata.rb
Overview
Stores information about an Orderable concern.
Instance Attribute Summary
Attributes inherited from Concern::Metadata
Class Method Summary collapse
- .default_field_name(sort_params) ⇒ Object
-
.normalize_sort_params(sort_params) ⇒ Object
class method default_field_name.
Instance Method Summary collapse
-
#field_name ⇒ Symbol
The name of the field used to store the order.
-
#field_name? ⇒ Boolean
false.
-
#field_was ⇒ Symbol
The name of the dirty tracking method for the order field.
-
#field_writer ⇒ Symbol
The name of the writer for the order field.
-
#filter_criteria(criteria) ⇒ Mongoid::Criteria
The criteria to filter only the desired collection items to sort.
-
#filter_params ⇒ Hash
The options (if any) to filter the collection by prior to sorting.
-
#filter_params? ⇒ Boolean
True if filter params are defined; otherwise false.
-
#sort_criteria(criteria) ⇒ Mongoid::Criteria
The criteria to be used when sorting the collection.
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
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
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_name ⇒ Symbol
The name of the field used to store the order.
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.
58 59 60 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 58 def field_name? !!self[:as] end |
#field_was ⇒ Symbol
The name of the dirty tracking method for the order field.
65 66 67 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 65 def field_was :"#{field_name}_was" end |
#field_writer ⇒ Symbol
The name of the writer for the order field.
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.
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_params ⇒ Hash
The options (if any) to filter the collection by prior to sorting.
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.
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.
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 |