Module: Positionable::PositionableMethods::InstanceMethods

Defined in:
lib/positionable.rb

Instance Method Summary collapse

Instance Method Details

#all_nextObject

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.



202
203
204
# File 'lib/positionable.rb', line 202

def all_next
  self.class.where("#{scoped_position} > ?", position)
end

#all_next_wasObject

All the next records of the old scope, whose positions are greater than this record before it was moved from its old record.



208
209
210
# File 'lib/positionable.rb', line 208

def all_next_was
  self.class.where("#{scoped_position_was} > ?", position_was)
end

#all_previousObject

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).



220
221
222
# File 'lib/positionable.rb', line 220

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.



181
182
183
# File 'lib/positionable.rb', line 181

def down!
  swap_with(self.next) unless last?
end

#first?Boolean

Tells whether this record is the first one (of his scope, if any).

Returns:

  • (Boolean)


166
167
168
# File 'lib/positionable.rb', line 166

def first?
  position == start
end

#last?Boolean

Tells whether this record is the last one (of his scope, if any).

Returns:

  • (Boolean)


171
172
173
# File 'lib/positionable.rb', line 171

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.



187
188
189
190
191
192
# File 'lib/positionable.rb', line 187

def move_to(new_position)
  if range.include? new_position
    reorder(position, new_position)
    update_column(:position, new_position)
  end
end

#nextObject

The next sibling record, whose position is right after this record.



195
196
197
# File 'lib/positionable.rb', line 195

def next
  at(position + 1)
end

#previousObject

Gives the next sibling record, whose position is right before this record.



213
214
215
# File 'lib/positionable.rb', line 213

def previous
  at(position - 1)
end

#up!Object

Swaps this record position with his previous sibling, unless this record is the first one.



176
177
178
# File 'lib/positionable.rb', line 176

def up!
  swap_with(previous) unless first?
end