module Qbrick::Orderable::InstanceMethods

Public Instance Methods

decrement_position() click to toggle source
# File lib/qbrick/orderable.rb, line 17
def decrement_position
  update_attribute :position, position - 1
end
increment_position() click to toggle source
# File lib/qbrick/orderable.rb, line 13
def increment_position
  update_attribute :position, position + 1
end
position_to_top() click to toggle source
# File lib/qbrick/orderable.rb, line 37
def position_to_top
  update_attribute :position, 1
  recount_siblings_position_from 1
end
preceding_sibling() click to toggle source
# File lib/qbrick/orderable.rb, line 21
def preceding_sibling
  siblings.where('position = ?', position - 1).first
end
preceding_siblings() click to toggle source
# File lib/qbrick/orderable.rb, line 29
def preceding_siblings
  siblings.where('position <= ?', position).where('id != ?', id)
end
recount_siblings_position_from(position) click to toggle source
# File lib/qbrick/orderable.rb, line 42
def recount_siblings_position_from(position)
  counter = position
  succeeding_siblings.each do |s|
    counter += 1
    s.update_attribute(:position, counter)
  end
end
reposition(before_id) click to toggle source
# File lib/qbrick/orderable.rb, line 50
def reposition(before_id)
  if before_id.blank?
    position_to_top
  else
    update_attribute :position, self.class.position_of(before_id) + 1
    recount_siblings_position_from position
  end
end
set_position() click to toggle source
# File lib/qbrick/orderable.rb, line 59
def set_position
  initial_position = siblings.blank? ? 1 : siblings.count + 1
  update_attribute(:position, initial_position)
end
succeeding_sibling() click to toggle source
# File lib/qbrick/orderable.rb, line 25
def succeeding_sibling
  siblings.where('position = ?', position + 1).first
end
succeeding_siblings() click to toggle source
# File lib/qbrick/orderable.rb, line 33
def succeeding_siblings
  siblings.where('position >= ?', position).where('id != ?', id)
end