module ClosureTree::Model
Public Instance Methods
Delegate to the Support
instance on the class:
# File lib/closure_tree/model.rb, line 49 def _ct self.class._ct end
# File lib/closure_tree/model.rb, line 178 def _ct_id read_attribute(_ct.model_class.primary_key) end
# File lib/closure_tree/model.rb, line 170 def _ct_parent_id read_attribute(_ct.parent_column_sym) end
# File lib/closure_tree/model.rb, line 182 def _ct_quoted_id _ct.quoted_value(_ct_id) end
# File lib/closure_tree/model.rb, line 174 def _ct_quoted_parent_id _ct.quoted_value(_ct_parent_id) end
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
# File lib/closure_tree/model.rb, line 90 def ancestor_ids _ct.ids_from(ancestors) end
node's ancestors include this record
# File lib/closure_tree/model.rb, line 144 def ancestor_of?(node) node.ancestors.include? self end
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
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
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
# File lib/closure_tree/model.rb, line 105 def child_ids _ct.ids_from(children) end
node is record's parent
# File lib/closure_tree/model.rb, line 154 def child_of?(node) self.parent == node end
# File lib/closure_tree/model.rb, line 79 def depth ancestors.size end
# File lib/closure_tree/model.rb, line 117 def descendant_ids _ct.ids_from(descendants) end
node is record's ancestor
# File lib/closure_tree/model.rb, line 149 def descendant_of?(node) self.ancestors.include? node end
# File lib/closure_tree/model.rb, line 109 def descendants without_self(self_and_descendants) end
node and record have a same root
# File lib/closure_tree/model.rb, line 159 def family_of?(node) self.root == node.root end
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
Returns true if this node has no children.
# File lib/closure_tree/model.rb, line 66 def leaf? children.empty? end
# File lib/closure_tree/model.rb, line 75 def leaves self_and_descendants.leaves end
node's parent is this record
# File lib/closure_tree/model.rb, line 134 def parent_of?(node) self == node.parent end
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
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
node's root is this record
# File lib/closure_tree/model.rb, line 139 def root_of?(node) self == node.root end
# File lib/closure_tree/model.rb, line 94 def self_and_ancestors_ids _ct.ids_from(self_and_ancestors) end
# File lib/closure_tree/model.rb, line 113 def self_and_descendant_ids _ct.ids_from(self_and_descendants) end
# 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
# File lib/closure_tree/model.rb, line 129 def sibling_ids _ct.ids_from(siblings) end
# File lib/closure_tree/model.rb, line 125 def siblings without_self(self_and_siblings) end