module OrderedTree::InstanceMethods::Tree

Public Instance Methods

ancestors(reload = false) click to toggle source

returns an array of ancestors, starting from parent until root.

return is cached
use ancestors(true) to force a reload
# File lib/ordered_tree/instance_methods/tree.rb, line 18
def ancestors(reload = false)
  reload = true if !@ancestors
  reload ? find_ancestors : @ancestors
end
children(reload=false) click to toggle source

returns an array of the object’s immediate children

auto-loads itself on first access
instead of returning "<child_nodes not loaded yet>"

return is cached
use children(true) to force a reload
# File lib/ordered_tree/instance_methods/tree.rb, line 40
def children(reload=false)
  reload = true if !@children
  reload ? child_nodes(true) : @children
end
descendants(reload = false) click to toggle source

returns an array of the object’s descendants

return is cached
use descendants(true) to force a reload
# File lib/ordered_tree/instance_methods/tree.rb, line 49
def descendants(reload = false)
  @descendants = nil if reload
  reload = true if !@descendants
  reload ? find_descendants(self) : @descendants
end
orphan() click to toggle source

orphans the node (sends it to the roots list)

(descendants follow)
# File lib/ordered_tree/instance_methods/tree.rb, line 89
def orphan
  self[foreign_key_column] = 0
  self.save
end
orphan_children() click to toggle source

orphans the node’s children

sends all immediate children to the 'roots' list
# File lib/ordered_tree/instance_methods/tree.rb, line 96
def orphan_children
  self.class.transaction do
    children(true).each{|child| child.orphan}
  end
end
orphan_self_and_children() click to toggle source

sends self and immediate children to the roots list

# File lib/ordered_tree/instance_methods/tree.rb, line 115
def orphan_self_and_children
  self.class.transaction do
    orphan_children
    orphan
  end
end
orphan_self_and_parent_adopts_children() click to toggle source

hands children off to parent (if possible), then orphans itself

# File lib/ordered_tree/instance_methods/tree.rb, line 123
def orphan_self_and_parent_adopts_children
  self.class.transaction do
    parent_adopts_children
    orphan
  end
end
parent(reload=false) click to toggle source

returns object’s parent in the tree

auto-loads itself on first access
instead of returning "<parent_node not loaded yet>"

return is cached, unless nil
use parent(true) to force a reload
# File lib/ordered_tree/instance_methods/tree.rb, line 29
def parent(reload=false)
  reload = true if !@parent
  reload ? parent_node(true) : @parent
end
parent_adopts_children() click to toggle source

hands children off to parent

if no parent, children will be orphaned
# File lib/ordered_tree/instance_methods/tree.rb, line 104
def parent_adopts_children
  if parent(true)
    self.class.transaction do
      children(true).each{|child| parent.children << child}
    end
  else
    orphan_children
  end
end
root(reload = false) click to toggle source

returns the top node in the object’s tree

return is cached, unless nil
use root(true) to force a reload
# File lib/ordered_tree/instance_methods/tree.rb, line 10
def root(reload = false)
  reload = true if !@root
  reload ? find_root : @root
end
shift_to(new_parent = nil, new_sibling = nil) click to toggle source

shifts a node to another parent, optionally specifying it’s position

(descendants will follow along)

shift_to()
  defaults to the bottom of the "roots" list

shift_to(nil, new_sibling)
  will move the item to "roots",
  and position the item above new_sibling

shift_to(new_parent)
  will move the item to the new parent,
  and position at the bottom of the parent's list

shift_to(new_parent, new_sibling)
  will move the item to the new parent,
  and position the item above new_sibling
# File lib/ordered_tree/instance_methods/tree.rb, line 75
def shift_to(new_parent = nil, new_sibling = nil)
  if new_parent
    ok = new_parent.children(true) << self
  else
    ok = orphan
  end
  if ok && new_sibling
    ok = move_above(new_sibling) if self_and_siblings(true).include?(new_sibling)
  end
  return ok
end