Module: Positionable::PositionableMethods::InstanceMethods
- Defined in:
- lib/positionable.rb
Instance Method Summary collapse
-
#all_next ⇒ Object
All the next records, whose positions are greater than this record.
-
#all_next_was ⇒ Object
All the next records of the old scope, whose positions are greater than this record before it was moved from its old record.
-
#all_previous ⇒ Object
All the next records, whose positions are smaller than this record.
-
#down! ⇒ Object
Swaps this record position with his next sibling, unless this record is the last one.
-
#first? ⇒ Boolean
Tells whether this record is the first one (of his scope, if any).
-
#last? ⇒ Boolean
Tells whether this record is the last one (of his scope, if any).
-
#move_to(new_position) ⇒ Object
Moves this record at the given position, and updates positions of the impacted sibling records accordingly.
-
#next ⇒ Object
The next sibling record, whose position is right after this record.
-
#previous ⇒ Object
Gives the next sibling record, whose position is right before this record.
-
#up! ⇒ Object
Swaps this record position with his previous sibling, unless this record is the first one.
Instance Method Details
#all_next ⇒ Object
All the next records, whose positions are greater than this record. Records are ordered by their respective positions, depending on the order
option provided to is_positionable
.
206 207 208 |
# File 'lib/positionable.rb', line 206 def all_next self.class.where("#{scoped_position} > ?", position) end |
#all_next_was ⇒ Object
All the next records of the old scope, whose positions are greater than this record before it was moved from its old record.
212 213 214 |
# File 'lib/positionable.rb', line 212 def all_next_was self.class.where("#{scoped_position_was} > ?", position_was) end |
#all_previous ⇒ Object
All the next records, whose positions are smaller than this record. Records are ordered by their respective positions, depending on the order
option provided to is_positionable
(ascending by default).
224 225 226 |
# File 'lib/positionable.rb', line 224 def all_previous self.class.where("#{scoped_position} < ?", position) end |
#down! ⇒ Object
Swaps this record position with his next sibling, unless this record is the last one.
185 186 187 |
# File 'lib/positionable.rb', line 185 def down! swap_with(self.next) unless last? end |
#first? ⇒ Boolean
Tells whether this record is the first one (of his scope, if any).
170 171 172 |
# File 'lib/positionable.rb', line 170 def first? position == start end |
#last? ⇒ Boolean
Tells whether this record is the last one (of his scope, if any).
175 176 177 |
# File 'lib/positionable.rb', line 175 def last? position == bottom end |
#move_to(new_position) ⇒ Object
Moves this record at the given position, and updates positions of the impacted sibling records accordingly. If the new position is out of range, then the record is not moved.
191 192 193 194 195 196 |
# File 'lib/positionable.rb', line 191 def move_to(new_position) if range.include? new_position reorder(position, new_position) update_column(:position, new_position) end end |
#next ⇒ Object
The next sibling record, whose position is right after this record.
199 200 201 |
# File 'lib/positionable.rb', line 199 def next at(position + 1) end |
#previous ⇒ Object
Gives the next sibling record, whose position is right before this record.
217 218 219 |
# File 'lib/positionable.rb', line 217 def previous at(position - 1) end |
#up! ⇒ Object
Swaps this record position with his previous sibling, unless this record is the first one.
180 181 182 |
# File 'lib/positionable.rb', line 180 def up! swap_with(previous) unless first? end |