Module: Hierarchy::InstanceMethods

Defined in:
lib/hierarchy.rb

Overview

Methods added to instances of the class this module is included into.

Instance Method Summary collapse

Instance Method Details

#ancestors(options = {}) ⇒ Array

Returns an array of ancestors above this object. Note that a) this array is ordered with the most senior ancestor at the beginning of the list, and b) this is an array, not a relation. For that reason, you can pass any additional scope options to the method.

Parameters:

  • options (Hash) (defaults to: {})

    Additional finder options.

Returns:

  • (Array)

    The objects above this one in the hierarchy.



92
93
94
95
96
# File 'lib/hierarchy.rb', line 92

def ancestors(options={})
  return [] if top_level?
  objects = self.class.ancestors_of(self).scoped(options).group_by(&:id)
  index_path.map { |id| objects[id].first }
end

#bottom_level?true, false

Returns Whether or not this object has no children. Makes a database call.

Returns:

  • (true, false)

    Whether or not this object has no children. Makes a database call.



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

def bottom_level?
  children.empty?
end

#childrenActiveRecord::Relation

Returns The objects directly below this one in the hierarchy.

Returns:

  • (ActiveRecord::Relation)

    The objects directly below this one in the hierarchy.



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

def children
  self.class.children_of self
end

#descendantsActiveRecord::Relation

Returns The objects below this one in the hierarchy.

Returns:

  • (ActiveRecord::Relation)

    The objects below this one in the hierarchy.



101
102
103
# File 'lib/hierarchy.rb', line 101

def descendants
  self.class.descendants_of self
end

#index_pathObject



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

def index_path
  IndexPath.from_ltree path.to_s
end

#my_pathObject



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

def my_path
  path.blank? ? id.to_s : "#{path}.#{id}"
end

#parentActiveRecord::Base

Returns The object directly above this one in the hierarchy.

Returns:

  • (ActiveRecord::Base)

    The object directly above this one in the hierarchy.



108
109
110
# File 'lib/hierarchy.rb', line 108

def parent
  top_level? ? nil : self.class.parent_of(self).first
end

#parent=(parent) ⇒ Object

Sets the object above this one in the hierarchy.

Parameters:

  • parent (ActiveRecord::Base)

    The parent object.

Raises:

  • (ArgumentError)

    If @parent@ is an unsaved record with no primary key.



79
80
81
82
# File 'lib/hierarchy.rb', line 79

def parent=(parent)
  raise ArgumentError, "Parent cannot be a new record" if parent.try(:new_record?)
  self.path = parent.try(:my_path)
end

#siblingsArray

Returns The objects at the same hierarchical level of this one.

Returns:

  • (Array)

    The objects at the same hierarchical level of this one.



121
122
123
# File 'lib/hierarchy.rb', line 121

def siblings
  self.class.siblings_of(self) - [ self ]
end

#top_level?true, false

Returns Whether or not this object has no parents.

Returns:

  • (true, false)

    Whether or not this object has no parents.



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

def top_level?
  path.blank?
end