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
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.
-
#initialize(name, properties = {}) ⇒ Metadata
constructor
A new instance of Metadata.
-
#sort_criteria(criteria) ⇒ Mongoid::Criteria
The criteria to be used when sorting the collection.
Methods inherited from Concern::Metadata
#characterize, #relation_key, #relation_key?
Constructor Details
#initialize(name, properties = {}) ⇒ Metadata
Returns a new instance of Metadata.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 11 def initialize name, properties = {} super self[:sort_params] = case properties[:sort_params] when Array properties[:sort_params].reduce({}) do |hsh, param| hsh.merge parse_sort_param(param) end # each when Hash properties[:sort_params].each.with_object({}) do |(key, value), hsh| hsh[key] = parse_sort_direction(value) end # each when Symbol, Origin::Key parse_sort_param(properties[:sort_params]) end # case end |
Instance Method Details
#field_name ⇒ Symbol
The name of the field used to store the order.
31 32 33 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 31 def field_name fetch(:as, default_field_name).intern end |
#field_name? ⇒ Boolean
false.
37 38 39 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 37 def field_name? !!self[:as] end |
#field_was ⇒ Symbol
The name of the dirty tracking method for the order field.
44 45 46 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 44 def field_was :"#{field_name}_was" end |
#field_writer ⇒ Symbol
The name of the writer for the order field.
51 52 53 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 51 def field_writer :"#{field_name}=" end |
#filter_criteria(criteria) ⇒ Mongoid::Criteria
The criteria to filter only the desired collection items to sort.
61 62 63 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 61 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.
68 69 70 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 68 def filter_params self[:filter] end |
#filter_params? ⇒ Boolean
Returns True if filter params are defined; otherwise false.
73 74 75 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 73 def filter_params? !!self[:filter] end |
#sort_criteria(criteria) ⇒ Mongoid::Criteria
The criteria to be used when sorting the collection.
83 84 85 |
# File 'lib/mongoid/sleeping_king_studios/orderable/metadata.rb', line 83 def sort_criteria criteria filter_criteria(criteria).order_by(self[:sort_params]) end |