Module: Qbrick::Orderable::InstanceMethods

Defined in:
lib/qbrick/orderable.rb

Instance Method Summary collapse

Instance Method Details

#decrement_positionObject



17
18
19
# File 'lib/qbrick/orderable.rb', line 17

def decrement_position
  update_attribute :position, position - 1
end

#increment_positionObject



13
14
15
# File 'lib/qbrick/orderable.rb', line 13

def increment_position
  update_attribute :position, position + 1
end

#position_to_topObject



37
38
39
40
# File 'lib/qbrick/orderable.rb', line 37

def position_to_top
  update_attribute :position, 1
  recount_siblings_position_from 1
end

#preceding_siblingObject



21
22
23
# File 'lib/qbrick/orderable.rb', line 21

def preceding_sibling
  siblings.where('position = ?', position - 1).first
end

#preceding_siblingsObject



29
30
31
# File 'lib/qbrick/orderable.rb', line 29

def preceding_siblings
  siblings.where('position <= ?', position).where('id != ?', id)
end

#recount_siblings_position_from(position) ⇒ Object



42
43
44
45
46
47
48
# 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) ⇒ Object



50
51
52
53
54
55
56
57
# 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_positionObject



59
60
61
62
# 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_siblingObject



25
26
27
# File 'lib/qbrick/orderable.rb', line 25

def succeeding_sibling
  siblings.where('position = ?', position + 1).first
end

#succeeding_siblingsObject



33
34
35
# File 'lib/qbrick/orderable.rb', line 33

def succeeding_siblings
  siblings.where('position >= ?', position).where('id != ?', id)
end