Module: Mongoid::Orderable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_order.rb

Overview

Mongoid::Orderable

Utility methods

There are several methods to move nodes around in the list:

node.move_up
node.move_down
node.move_to_top
node.move_to_bottom
node.move_above(other)
node.move_below(other)

Additionally there are some methods to check aspects of the document in the list of children:

node.at_top?
node.at_bottom?

Instance Method Summary collapse

Instance Method Details

#at_bottom?Boolean

Is this the lowest sibling?

Returns:

  • (Boolean)


67
68
69
# File 'lib/mongoid_order.rb', line 67

def at_bottom?
  lower_siblings.empty?
end

#at_top?Boolean

Is this the highest sibling?

Returns:

  • (Boolean)


61
62
63
# File 'lib/mongoid_order.rb', line 61

def at_top?
  higher_siblings.empty?
end

#first_sibling_in_listObject

Returns the highest sibling (could be self)



55
56
57
# File 'lib/mongoid_order.rb', line 55

def first_sibling_in_list
  self.class.asc(:position).first
end

#higher_siblingsObject

Returns siblings above the current document. Siblings with a position lower than this documents’s position.



43
44
45
# File 'lib/mongoid_order.rb', line 43

def higher_siblings
  self.class.where(:position.lt => self.position)
end

#last_sibling_in_listObject

Returns the lowest sibling (could be self)



49
50
51
# File 'lib/mongoid_order.rb', line 49

def last_sibling_in_list
  self.class.asc(:position).last
end

#lower_siblingsObject

Returns siblings below the current document. Siblings with a position greater than this documents’s position.



36
37
38
# File 'lib/mongoid_order.rb', line 36

def lower_siblings
  self.class.where(:position.gt => self.position)
end

#move_above(other) ⇒ Object

Move this node above the specified node

This method changes the node’s parent if nescessary.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mongoid_order.rb', line 105

def move_above(other)
  if position > other.position
    new_position = other.position
    other.lower_siblings.where(:position.lt => self.position).each { |s| s.inc(:position, 1) }
    other.inc(:position, 1)
    self.position = new_position
    save!
  else
    new_position = other.position - 1
    other.higher_siblings.where(:position.gt => self.position).each { |s| s.inc(:position, -1) }
    self.position = new_position
    save!
  end
end

#move_below(other) ⇒ Object

Move this node below the specified node

This method changes the node’s parent if nescessary.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mongoid_order.rb', line 124

def move_below(other)
  if position > other.position
    new_position = other.position + 1
    other.lower_siblings.where(:position.lt => self.position).each { |s| s.inc(:position, 1) }
    self.position = new_position
    save!
  else
    new_position = other.position
    other.higher_siblings.where(:position.gt => self.position).each { |s| s.inc(:position, -1) }
    other.inc(:position, -1)
    self.position = new_position
    save!
  end
end

#move_downObject

Move this node one position down



95
96
97
98
99
# File 'lib/mongoid_order.rb', line 95

def move_down
  return if at_bottom?
  self.class.where(:position => self.position + 1).first.inc(:position, -1)
  inc(:position, 1)
end

#move_to_bottomObject

Move this node below all its siblings



80
81
82
83
# File 'lib/mongoid_order.rb', line 80

def move_to_bottom
  return true if at_bottom?
  move_below(last_sibling_in_list)
end

#move_to_topObject

Move this node above all its siblings



73
74
75
76
# File 'lib/mongoid_order.rb', line 73

def move_to_top
  return true if at_top?
  move_above(first_sibling_in_list)
end

#move_upObject

Move this node one position up



87
88
89
90
91
# File 'lib/mongoid_order.rb', line 87

def move_up
  return if at_top?
  self.class.where(:position => self.position - 1).first.inc(:position, 1)
  inc(:position, -1)
end