Module: Mongoid::Orderable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid/orderable.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #add_to_list ⇒ Object
- #first? ⇒ Boolean
- #in_list? ⇒ Boolean
- #last? ⇒ Boolean
- #move_to(target_position) ⇒ Object (also: #insert_at)
- #move_to!(target_position) ⇒ Object (also: #insert_at!)
- #move_to=(target_position) ⇒ Object (also: #insert_at=)
-
#next_item ⇒ Object
returns the next item in the list.
-
#next_items ⇒ Object
Returns items below the current document.
-
#previous_item ⇒ Object
(also: #prev_item)
returns the previous item in the list.
-
#previous_items ⇒ Object
(also: #prev_items)
Returns items above the current document.
- #remove_from_list ⇒ Object
Instance Method Details
#add_to_list ⇒ Object
155 156 157 |
# File 'lib/mongoid/orderable.rb', line 155 def add_to_list apply_position @move_to end |
#first? ⇒ Boolean
143 144 145 |
# File 'lib/mongoid/orderable.rb', line 143 def first? in_list? && orderable_position == orderable_base end |
#in_list? ⇒ Boolean
151 152 153 |
# File 'lib/mongoid/orderable.rb', line 151 def in_list? !orderable_position.nil? end |
#last? ⇒ Boolean
147 148 149 |
# File 'lib/mongoid/orderable.rb', line 147 def last? in_list? && orderable_position == bottom_orderable_position end |
#move_to(target_position) ⇒ Object Also known as: insert_at
113 114 115 |
# File 'lib/mongoid/orderable.rb', line 113 def move_to target_position @move_to = target_position end |
#move_to!(target_position) ⇒ Object Also known as: insert_at!
107 108 109 110 |
# File 'lib/mongoid/orderable.rb', line 107 def move_to! target_position @move_to = target_position save end |
#move_to=(target_position) ⇒ Object Also known as: insert_at=
118 119 120 |
# File 'lib/mongoid/orderable.rb', line 118 def move_to= target_position @move_to = target_position end |
#next_item ⇒ Object
returns the next item in the list
98 99 100 101 102 103 104 105 |
# File 'lib/mongoid/orderable.rb', line 98 def next_item if next_items.present? next_position = self.position + 1 orderable_scoped.where(:position => next_position).first else nil end end |
#next_items ⇒ Object
Returns items below the current document. Items with a position greater than this document’s position.
82 83 84 |
# File 'lib/mongoid/orderable.rb', line 82 def next_items orderable_scoped.where(orderable_column.gt => self.position) end |
#previous_item ⇒ Object Also known as: prev_item
returns the previous item in the list
87 88 89 90 91 92 93 94 |
# File 'lib/mongoid/orderable.rb', line 87 def previous_item if previous_items.present? previous_position = self.position - 1 orderable_scoped.where(:position => previous_position).first else nil end end |
#previous_items ⇒ Object Also known as: prev_items
Returns items above the current document. Items with a position lower than this document’s position.
74 75 76 |
# File 'lib/mongoid/orderable.rb', line 74 def previous_items orderable_scoped.where(orderable_column.lt => self.position) end |
#remove_from_list ⇒ Object
159 160 161 |
# File 'lib/mongoid/orderable.rb', line 159 def remove_from_list MongoidOrderable.inc orderable_scoped.where(orderable_column.gt => orderable_position), orderable_column, -1 end |