module ClosureTree::Model

Public Instance Methods

_ct() click to toggle source

Delegate to the Support instance on the class:

# File lib/closure_tree/model.rb, line 49
def _ct
  self.class._ct
end
_ct_id() click to toggle source
# File lib/closure_tree/model.rb, line 178
def _ct_id
  read_attribute(_ct.model_class.primary_key)
end
_ct_parent_id() click to toggle source
# File lib/closure_tree/model.rb, line 170
def _ct_parent_id
  read_attribute(_ct.parent_column_sym)
end
_ct_quoted_id() click to toggle source
# File lib/closure_tree/model.rb, line 182
def _ct_quoted_id
  _ct.quoted_value(_ct_id)
end
_ct_quoted_parent_id() click to toggle source
# File lib/closure_tree/model.rb, line 174
def _ct_quoted_parent_id
  _ct.quoted_value(_ct_parent_id)
end
add_child(child_node) click to toggle source

Alias for appending to the children collection. You can also add directly to the children collection, if you'd prefer.

# File lib/closure_tree/model.rb, line 165
def add_child(child_node)
  children << child_node
  child_node
end
ancestor_ids() click to toggle source
# File lib/closure_tree/model.rb, line 90
def ancestor_ids
  _ct.ids_from(ancestors)
end
ancestor_of?(node) click to toggle source

node's ancestors include this record

# File lib/closure_tree/model.rb, line 144
def ancestor_of?(node)
  node.ancestors.include? self
end
ancestors() click to toggle source

enumerable of ancestors, immediate parent is first, root is last.

# File lib/closure_tree/model.rb, line 86
def ancestors
  without_self(self_and_ancestors)
end
ancestry_path(to_s_column = _ct.name_column) click to toggle source

Returns an array, root first, of self_and_ancestors' values of the to_s_column, which defaults to the name_column. (so child.ancestry_path == +%w{grandparent parent child}+

# File lib/closure_tree/model.rb, line 101
def ancestry_path(to_s_column = _ct.name_column)
  self_and_ancestors.map { |n| n.send to_s_column.to_sym }.reverse
end
child?() click to toggle source

Returns true if this node has a parent, and is not a root.

# File lib/closure_tree/model.rb, line 61
def child?
  !root?
end
child_ids() click to toggle source
# File lib/closure_tree/model.rb, line 105
def child_ids
  _ct.ids_from(children)
end
child_of?(node) click to toggle source

node is record's parent

# File lib/closure_tree/model.rb, line 154
def child_of?(node)
  self.parent == node
end
depth() click to toggle source
# File lib/closure_tree/model.rb, line 79
def depth
  ancestors.size
end
Also aliased as: level
descendant_ids() click to toggle source
# File lib/closure_tree/model.rb, line 117
def descendant_ids
  _ct.ids_from(descendants)
end
descendant_of?(node) click to toggle source

node is record's ancestor

# File lib/closure_tree/model.rb, line 149
def descendant_of?(node)
  self.ancestors.include? node
end
descendants() click to toggle source
# File lib/closure_tree/model.rb, line 109
def descendants
  without_self(self_and_descendants)
end
family_of?(node) click to toggle source

node and record have a same root

# File lib/closure_tree/model.rb, line 159
def family_of?(node)
  self.root == node.root
end
hash_tree(options = {}) click to toggle source

We have to redefine hash_tree because the activerecord relation is already scoped to parent_id.

# File lib/closure_tree/model.rb, line 24
def hash_tree(options = {})
  # we want limit_depth + 1 because we don't do self_and_descendants.
  limit_depth = options[:limit_depth]
  _ct.hash_tree(@association.owner.descendants, limit_depth ? limit_depth + 1 : nil)
end
leaf?() click to toggle source

Returns true if this node has no children.

# File lib/closure_tree/model.rb, line 66
def leaf?
  children.empty?
end
leaves() click to toggle source
# File lib/closure_tree/model.rb, line 75
def leaves
  self_and_descendants.leaves
end
level()
Alias for: depth
parent_of?(node) click to toggle source

node's parent is this record

# File lib/closure_tree/model.rb, line 134
def parent_of?(node)
  self == node.parent
end
root() click to toggle source

Returns the farthest ancestor, or self if root?

# File lib/closure_tree/model.rb, line 71
def root
  self_and_ancestors.where(_ct.parent_column_name.to_sym => nil).first
end
root?() click to toggle source

Returns true if this node has no parents.

# File lib/closure_tree/model.rb, line 54
def root?
  # Accessing the parent will fetch that row from the database,
  # so if we are persisted, just check that the parent_id column is nil.
  persisted? ? _ct_parent_id.nil? : parent.nil?
end
root_of?(node) click to toggle source

node's root is this record

# File lib/closure_tree/model.rb, line 139
def root_of?(node)
  self == node.root
end
self_and_ancestors_ids() click to toggle source
# File lib/closure_tree/model.rb, line 94
def self_and_ancestors_ids
  _ct.ids_from(self_and_ancestors)
end
self_and_descendant_ids() click to toggle source
# File lib/closure_tree/model.rb, line 113
def self_and_descendant_ids
  _ct.ids_from(self_and_descendants)
end
self_and_siblings() click to toggle source
# File lib/closure_tree/model.rb, line 121
def self_and_siblings
  _ct.scope_with_order(_ct.base_class.where(_ct.parent_column_sym => _ct_parent_id))
end
sibling_ids() click to toggle source
# File lib/closure_tree/model.rb, line 129
def sibling_ids
  _ct.ids_from(siblings)
end
siblings() click to toggle source
# File lib/closure_tree/model.rb, line 125
def siblings
  without_self(self_and_siblings)
end