module CollectiveIdea::Acts::NestedSet::Model::Movable

Public Instance Methods

find_left_neighbor(parent, order_attribute, ascending) click to toggle source

Find the node immediately to the left of this node.

    # File lib/awesome_nested_set/model/movable.rb
 92 def find_left_neighbor(parent, order_attribute, ascending)
 93   left = nil
 94   parent.children.each do |n|
 95     if ascending
 96       left = n if n.send(order_attribute) < self.send(order_attribute)
 97     else
 98       left = n if n.send(order_attribute) > self.send(order_attribute)
 99     end
100   end
101   left
102 end
move_left() click to toggle source

Shorthand method for finding the left sibling and moving to the left of it.

   # File lib/awesome_nested_set/model/movable.rb
18 def move_left
19   move_to_left_of left_sibling
20 end
move_possible?(target) click to toggle source
   # File lib/awesome_nested_set/model/movable.rb
 9 def move_possible?(target)
10   self != target && # Can't target self
11     same_scope?(target) && # can't be in different scopes
12     # detect impossible move
13     within_bounds?(target.left, target.left) &&
14     within_bounds?(target.right, target.right)
15 end
move_right() click to toggle source

Shorthand method for finding the right sibling and moving to the right of it.

   # File lib/awesome_nested_set/model/movable.rb
23 def move_right
24   move_to_right_of right_sibling
25 end
move_to(target, position) click to toggle source
    # File lib/awesome_nested_set/model/movable.rb
104 def move_to(target, position)
105   prevent_unpersisted_move
106 
107   run_callbacks :move do
108     in_tenacious_transaction do
109       target = reload_target(target, position)
110       self.reload_nested_set
111 
112       Move.new(target, position, self).move
113       update_counter_cache
114     end
115     after_move_to(target, position)
116   end
117 end
move_to_child_of(node) click to toggle source

Move the node to the child of another node

   # File lib/awesome_nested_set/model/movable.rb
38 def move_to_child_of(node)
39   if node == :root
40     move_to_root
41   else
42     move_to node, :child
43   end
44 end
move_to_child_with_index(node, index) click to toggle source

Move the node to the child of another node with specify index

   # File lib/awesome_nested_set/model/movable.rb
47 def move_to_child_with_index(node, index)
48   siblings = node == :root ? roots : node.children
49   if siblings.empty?
50     move_to_child_of(node)
51   elsif siblings.count == index
52     move_to_right_of(siblings.last)
53   else
54     my_position = siblings.index(self)
55     if my_position && my_position < index
56       # e.g. if self is at position 0 and we want to move self to position 1 then self
57       # needs to move to the *right* of the node at position 1. That's because the node
58       # that is currently at position 1 will be at position 0 after the move completes.
59       move_to_right_of(siblings[index])
60     elsif my_position && my_position == index
61       # do nothing. already there.
62     else
63       move_to_left_of(siblings[index])
64     end
65   end
66 end
move_to_left_of(node) click to toggle source

Move the node to the left of another node

   # File lib/awesome_nested_set/model/movable.rb
28 def move_to_left_of(node)
29   move_to node, :left
30 end
move_to_ordered_child_of(parent, order_attribute, ascending = true) click to toggle source

Order children in a nested set by an attribute Can order by any attribute class that uses the Comparable mixin, for example a string or integer Usage example when sorting categories alphabetically: @new_category.move_to_ordered_child_of(@root, “name”)

   # File lib/awesome_nested_set/model/movable.rb
76 def move_to_ordered_child_of(parent, order_attribute, ascending = true)
77   self.move_to_root and return unless parent
78 
79   left_neighbor = find_left_neighbor(parent, order_attribute, ascending)
80   self.move_to_child_of(parent)
81 
82   return unless parent.children.many?
83 
84   if left_neighbor
85     self.move_to_right_of(left_neighbor)
86   else # Self is the left most node.
87     self.move_to_left_of(parent.children[0])
88   end
89 end
move_to_right_of(node) click to toggle source

Move the node to the right of another node

   # File lib/awesome_nested_set/model/movable.rb
33 def move_to_right_of(node)
34   move_to node, :right
35 end
move_to_root() click to toggle source

Move the node to root nodes

   # File lib/awesome_nested_set/model/movable.rb
69 def move_to_root
70   move_to self, :root
71 end

Protected Instance Methods

after_move_to(target, position) click to toggle source
    # File lib/awesome_nested_set/model/movable.rb
121 def after_move_to(target, position)
122   target.reload_nested_set if target
123   self.set_depth_for_self_and_descendants!
124   self.reload_nested_set
125 end
move_to_new_parent() click to toggle source
    # File lib/awesome_nested_set/model/movable.rb
127 def move_to_new_parent
128   if @move_to_new_parent_id.nil?
129     move_to_root
130   elsif @move_to_new_parent_id
131     move_to_child_of(@move_to_new_parent_id)
132   end
133 end
out_of_bounds?(left_bound, right_bound) click to toggle source
    # File lib/awesome_nested_set/model/movable.rb
135 def out_of_bounds?(left_bound, right_bound)
136   left <= left_bound && right >= right_bound
137 end
prevent_unpersisted_move() click to toggle source
    # File lib/awesome_nested_set/model/movable.rb
139 def prevent_unpersisted_move
140   if self.new_record?
141     raise ActiveRecord::ActiveRecordError, "You cannot move a new node"
142   end
143 end
within_bounds?(left_bound, right_bound) click to toggle source
    # File lib/awesome_nested_set/model/movable.rb
145 def within_bounds?(left_bound, right_bound)
146   !out_of_bounds?(left_bound, right_bound)
147 end