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
-
#ancestors(options = {}) ⇒ Array
Returns an array of ancestors above this object.
-
#bottom_level? ⇒ true, false
Whether or not this object has no children.
-
#children ⇒ ActiveRecord::Relation
The objects directly below this one in the hierarchy.
-
#descendants ⇒ ActiveRecord::Relation
The objects below this one in the hierarchy.
- #index_path ⇒ Object
- #my_path ⇒ Object
-
#parent ⇒ ActiveRecord::Base
The object directly above this one in the hierarchy.
-
#parent=(parent) ⇒ Object
Sets the object above this one in the hierarchy.
-
#siblings ⇒ Array
The objects at the same hierarchical level of this one.
-
#top_level? ⇒ true, false
Whether or not this object has no parents.
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.
92 93 94 95 96 |
# File 'lib/hierarchy.rb', line 92 def ancestors(={}) return [] if top_level? objects = self.class.ancestors_of(self).scoped().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.
134 135 136 |
# File 'lib/hierarchy.rb', line 134 def bottom_level? children.empty? end |
#children ⇒ ActiveRecord::Relation
Returns 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 |
#descendants ⇒ ActiveRecord::Relation
Returns 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_path ⇒ Object
144 145 146 |
# File 'lib/hierarchy.rb', line 144 def index_path IndexPath.from_ltree path.to_s end |
#my_path ⇒ Object
139 140 141 |
# File 'lib/hierarchy.rb', line 139 def my_path path.blank? ? id.to_s : "#{path}.#{id}" end |
#parent ⇒ ActiveRecord::Base
Returns 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.
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 |
#siblings ⇒ Array
Returns 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.
127 128 129 |
# File 'lib/hierarchy.rb', line 127 def top_level? path.blank? end |