Module: ClosureTree::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/closure_tree/model.rb

Instance Method Summary collapse

Instance Method Details

#_ctObject

Delegate to the Support instance on the class:



10
11
12
# File 'lib/closure_tree/model.rb', line 10

def _ct
  self.class._ct
end

#_ct_idObject



141
142
143
# File 'lib/closure_tree/model.rb', line 141

def _ct_id
  read_attribute(_ct.model_class.primary_key)
end

#_ct_parent_idObject



133
134
135
# File 'lib/closure_tree/model.rb', line 133

def _ct_parent_id
  read_attribute(_ct.parent_column_sym)
end

#_ct_quoted_idObject



145
146
147
# File 'lib/closure_tree/model.rb', line 145

def _ct_quoted_id
  _ct.quoted_value(_ct_id)
end

#_ct_quoted_parent_idObject



137
138
139
# File 'lib/closure_tree/model.rb', line 137

def _ct_quoted_parent_id
  _ct.quoted_value(_ct_parent_id)
end

#add_child(child_node) ⇒ Object

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



128
129
130
131
# File 'lib/closure_tree/model.rb', line 128

def add_child(child_node)
  children << child_node
  child_node
end

#ancestor_idsObject



51
52
53
# File 'lib/closure_tree/model.rb', line 51

def ancestor_ids
  _ct.ids_from(ancestors)
end

#ancestor_of?(node) ⇒ Boolean

node’s ancestors include this record

Returns:

  • (Boolean)


107
108
109
# File 'lib/closure_tree/model.rb', line 107

def ancestor_of?(node)
  node.ancestors.include? self
end

#ancestorsObject

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



47
48
49
# File 'lib/closure_tree/model.rb', line 47

def ancestors
  without_self(self_and_ancestors)
end

#ancestry_path(to_s_column = _ct.name_column) ⇒ Object

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}



62
63
64
# File 'lib/closure_tree/model.rb', line 62

def ancestry_path(to_s_column = _ct.name_column)
  self_and_ancestors.map { |n| n.send to_s_column.to_sym }.reverse
end

#child?Boolean

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/closure_tree/model.rb', line 22

def child?
  !root?
end

#child_idsObject



66
67
68
# File 'lib/closure_tree/model.rb', line 66

def child_ids
  _ct.ids_from(children)
end

#child_of?(node) ⇒ Boolean

node is record’s parent

Returns:

  • (Boolean)


117
118
119
# File 'lib/closure_tree/model.rb', line 117

def child_of?(node)
  parent == node
end

#depthObject Also known as: level



40
41
42
# File 'lib/closure_tree/model.rb', line 40

def depth
  ancestor_hierarchies.size - 1
end

#descendant_idsObject



78
79
80
# File 'lib/closure_tree/model.rb', line 78

def descendant_ids
  _ct.ids_from(descendants)
end

#descendant_of?(node) ⇒ Boolean

node is record’s ancestor

Returns:

  • (Boolean)


112
113
114
# File 'lib/closure_tree/model.rb', line 112

def descendant_of?(node)
  ancestors.include? node
end

#descendantsObject



70
71
72
# File 'lib/closure_tree/model.rb', line 70

def descendants
  without_self(self_and_descendants)
end

#family_of?(node) ⇒ Boolean

node and record have a same root

Returns:

  • (Boolean)


122
123
124
# File 'lib/closure_tree/model.rb', line 122

def family_of?(node)
  root == node.root
end

#leaf?Boolean

Returns true if this node has no children.

Returns:

  • (Boolean)


27
28
29
# File 'lib/closure_tree/model.rb', line 27

def leaf?
  children.empty?
end

#leavesObject



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

def leaves
  self_and_descendants.leaves
end

#parent_of?(node) ⇒ Boolean

node’s parent is this record

Returns:

  • (Boolean)


97
98
99
# File 'lib/closure_tree/model.rb', line 97

def parent_of?(node)
  self == node.parent
end

#rootObject

Returns the farthest ancestor, or self if root?



32
33
34
# File 'lib/closure_tree/model.rb', line 32

def root
  self_and_ancestors.where(_ct.parent_column_name.to_sym => nil).first
end

#root?Boolean

Returns true if this node has no parents.

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/closure_tree/model.rb', line 15

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) ⇒ Boolean

node’s root is this record

Returns:

  • (Boolean)


102
103
104
# File 'lib/closure_tree/model.rb', line 102

def root_of?(node)
  self == node.root
end

#self_and_ancestors_idsObject



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

def self_and_ancestors_ids
  _ct.ids_from(self_and_ancestors)
end

#self_and_descendant_idsObject



74
75
76
# File 'lib/closure_tree/model.rb', line 74

def self_and_descendant_ids
  _ct.ids_from(self_and_descendants)
end

#self_and_siblingsObject



82
83
84
85
86
# File 'lib/closure_tree/model.rb', line 82

def self_and_siblings
  scope = _ct.base_class.where(_ct.parent_column_sym => _ct_parent_id)
  scope = _ct.apply_scope_conditions(scope, self)
  _ct.scope_with_order(scope)
end

#sibling_idsObject



92
93
94
# File 'lib/closure_tree/model.rb', line 92

def sibling_ids
  _ct.ids_from(siblings)
end

#siblingsObject



88
89
90
# File 'lib/closure_tree/model.rb', line 88

def siblings
  without_self(self_and_siblings)
end