Module: ActiveRecord::Acts::TreeWithDottedIds::InstanceMethods

Defined in:
lib/active_record/acts/tree_with_dotted_ids.rb

Instance Method Summary collapse

Instance Method Details

#all_childrenObject

Returns all children of the current node root.all_children # => [child1, subchild1, subchild2]



163
164
165
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 163

def all_children
  find_all_children_with_dotted_ids
end

#ancestor_of?(node) ⇒ Boolean

root.ancestor_of?(subchild1) # => true subchild1.ancestor_of?(child1) # => false



150
151
152
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 150

def ancestor_of?(node)
  node.dotted_ids.length > self.dotted_ids.length && node.dotted_ids.starts_with?(self.dotted_ids)
end

#ancestorsObject

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]


105
106
107
108
109
110
111
112
113
114
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 105

def ancestors
  if self.dotted_ids
    ids = self.dotted_ids.split('.')[0...-1]
    self.class.where(:id => ids).order('dotted_ids DESC')
  else
    node, nodes = self, []
    nodes << node = node.parent while node.parent
    nodes
  end
end

#depthObject

Returns the depth of the node, root nodes have a depth of 0



174
175
176
177
178
179
180
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 174

def depth
  if self.dotted_ids.present?
    self.dotted_ids.scan(/\./).size
  else
    (self.parent.try(:depth) || -1) + 1
  end
end

#descendant_of?(node) ⇒ Boolean

subchild1.descendant_of?(child1) # => true root.descendant_of?(subchild1) # => false



157
158
159
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 157

def descendant_of?(node)
  self.dotted_ids.length > node.dotted_ids.length && self.dotted_ids.starts_with?(node.dotted_ids)
end

#rootObject

Returns the root node of the tree.



122
123
124
125
126
127
128
129
130
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 122

def root
  if self.dotted_ids
    self.class.find(self.dotted_ids.split('.').first)
  else
    node = self
    node = node.parent while node.parent
    node
  end
end

#self_and_all_childrenObject

Returns all children of the current node root.self_and_all_children # => [root, child1, subchild1, subchild2]



169
170
171
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 169

def self_and_all_children
  [self] + all_children
end

#self_and_ancestorsObject



117
118
119
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 117

def self_and_ancestors
  [self] + ancestors
end

#self_and_siblingsObject

Returns all siblings and a reference to the current node.

subchild1.self_and_siblings # => [subchild1, subchild2]


142
143
144
145
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 142

def self_and_siblings
  #parent ? parent.children : self.class.roots
  self.class.where(:parent_id => self.parent_id)
end

#siblingsObject

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]


135
136
137
# File 'lib/active_record/acts/tree_with_dotted_ids.rb', line 135

def siblings
  self_and_siblings - [self]
end