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:



49
50
51
# File 'lib/closure_tree/model.rb', line 49

def _ct
  self.class._ct
end

#_ct_idObject



178
179
180
# File 'lib/closure_tree/model.rb', line 178

def _ct_id
  read_attribute(_ct.model_class.primary_key)
end

#_ct_parent_idObject



170
171
172
# File 'lib/closure_tree/model.rb', line 170

def _ct_parent_id
  read_attribute(_ct.parent_column_sym)
end

#_ct_quoted_idObject



182
183
184
# File 'lib/closure_tree/model.rb', line 182

def _ct_quoted_id
  _ct.quoted_value(_ct_id)
end

#_ct_quoted_parent_idObject



174
175
176
# File 'lib/closure_tree/model.rb', line 174

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.



165
166
167
168
# File 'lib/closure_tree/model.rb', line 165

def add_child(child_node)
  children << child_node
  child_node
end

#ancestor_idsObject



90
91
92
# File 'lib/closure_tree/model.rb', line 90

def ancestor_ids
  _ct.ids_from(ancestors)
end

#ancestor_of?(node) ⇒ Boolean

node’s ancestors include this record

Returns:

  • (Boolean)


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

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

#ancestorsObject

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



86
87
88
# File 'lib/closure_tree/model.rb', line 86

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}



101
102
103
# 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?Boolean

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

Returns:

  • (Boolean)


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

def child?
  !root?
end

#child_idsObject



105
106
107
# File 'lib/closure_tree/model.rb', line 105

def child_ids
  _ct.ids_from(children)
end

#child_of?(node) ⇒ Boolean

node is record’s parent

Returns:

  • (Boolean)


154
155
156
# File 'lib/closure_tree/model.rb', line 154

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

#depthObject Also known as: level



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

def depth
  ancestors.size
end

#descendant_idsObject



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

def descendant_ids
  _ct.ids_from(descendants)
end

#descendant_of?(node) ⇒ Boolean

node is record’s ancestor

Returns:

  • (Boolean)


149
150
151
# File 'lib/closure_tree/model.rb', line 149

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

#descendantsObject



109
110
111
# File 'lib/closure_tree/model.rb', line 109

def descendants
  without_self(self_and_descendants)
end

#family_of?(node) ⇒ Boolean

node and record have a same root

Returns:

  • (Boolean)


159
160
161
# File 'lib/closure_tree/model.rb', line 159

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

#leaf?Boolean

Returns true if this node has no children.

Returns:

  • (Boolean)


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

def leaf?
  children.empty?
end

#leavesObject



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

def leaves
  self_and_descendants.leaves
end

#parent_of?(node) ⇒ Boolean

node’s parent is this record

Returns:

  • (Boolean)


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

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

#rootObject

Returns the farthest ancestor, or self if root?



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

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)


54
55
56
57
58
# 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) ⇒ Boolean

node’s root is this record

Returns:

  • (Boolean)


139
140
141
# File 'lib/closure_tree/model.rb', line 139

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

#self_and_ancestors_idsObject



94
95
96
# File 'lib/closure_tree/model.rb', line 94

def self_and_ancestors_ids
  _ct.ids_from(self_and_ancestors)
end

#self_and_descendant_idsObject



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

def self_and_descendant_ids
  _ct.ids_from(self_and_descendants)
end

#self_and_siblingsObject



121
122
123
# 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_idsObject



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

def sibling_ids
  _ct.ids_from(siblings)
end

#siblingsObject



125
126
127
# File 'lib/closure_tree/model.rb', line 125

def siblings
  without_self(self_and_siblings)
end