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:


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

def _ct
  self.class._ct
end

#_ct_idObject


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

def _ct_id
  read_attribute(_ct.model_class.primary_key)
end

#_ct_parent_idObject


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

def _ct_parent_id
  read_attribute(_ct.parent_column_sym)
end

#_ct_quoted_idObject


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

def _ct_quoted_id
  _ct.quoted_value(_ct_id)
end

#_ct_quoted_parent_idObject


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

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.


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

def add_child(child_node)
  children << child_node
  child_node
end

#ancestor_idsObject


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

def ancestor_ids
  _ct.ids_from(ancestors)
end

#ancestorsObject

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


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

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}


99
100
101
# File 'lib/closure_tree/model.rb', line 99

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

#child?Boolean

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

Returns:

  • (Boolean)

59
60
61
# File 'lib/closure_tree/model.rb', line 59

def child?
  !root?
end

#child_idsObject


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

def child_ids
  _ct.ids_from(children)
end

#depthObject Also known as: level


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

def depth
  ancestors.size
end

#descendant_idsObject


115
116
117
# File 'lib/closure_tree/model.rb', line 115

def descendant_ids
  _ct.ids_from(descendants)
end

#descendantsObject


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

def descendants
  without_self(self_and_descendants)
end

#leaf?Boolean

Returns true if this node has no children.

Returns:

  • (Boolean)

64
65
66
# File 'lib/closure_tree/model.rb', line 64

def leaf?
  children.empty?
end

#leavesObject


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

def leaves
  self_and_descendants.leaves
end

#rootObject

Returns the farthest ancestor, or self if root?


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

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)

52
53
54
55
56
# File 'lib/closure_tree/model.rb', line 52

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

#self_and_ancestors_idsObject


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

def self_and_ancestors_ids
  _ct.ids_from(self_and_ancestors)
end

#self_and_descendant_idsObject


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

def self_and_descendant_ids
  _ct.ids_from(self_and_descendants)
end

#self_and_siblingsObject


119
120
121
# File 'lib/closure_tree/model.rb', line 119

def self_and_siblings
  _ct.scope_with_order(_ct.base_class.where(_ct.parent_column_sym => _ct_parent_id))
end

#sibling_idsObject


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

def sibling_ids
  _ct.ids_from(siblings)
end

#siblingsObject


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

def siblings
  without_self(self_and_siblings)
end