module CollectiveIdea::Acts::NestedSet::Model::Relatable
Public Instance Methods
ancestors()
click to toggle source
Returns an collection of all parents
# File lib/awesome_nested_set/model/relatable.rb 8 def ancestors 9 without_self self_and_ancestors 10 end
descendants()
click to toggle source
Returns a collection including all of its children and nested children
# File lib/awesome_nested_set/model/relatable.rb 43 def descendants 44 without_self self_and_descendants 45 end
is_ancestor_of?(other)
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 61 def is_ancestor_of?(other) 62 within_node?(self, other) && same_scope?(other) 63 end
is_descendant_of?(other)
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 53 def is_descendant_of?(other) 54 within_node?(other, self) && same_scope?(other) 55 end
is_or_is_ancestor_of?(other)
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 65 def is_or_is_ancestor_of?(other) 66 (self == other || within_node?(self, other)) && same_scope?(other) 67 end
is_or_is_descendant_of?(other)
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 57 def is_or_is_descendant_of?(other) 58 (other == self || within_node?(other, self)) && same_scope?(other) 59 end
leaves()
click to toggle source
Returns a set of all of its nested children which do not have children
# File lib/awesome_nested_set/model/relatable.rb 30 def leaves 31 descendants.where( 32 "#{quoted_right_column_full_name} - #{quoted_left_column_full_name} = 1" 33 ) 34 end
left_sibling()
click to toggle source
Find the first sibling to the left
# File lib/awesome_nested_set/model/relatable.rb 77 def left_sibling 78 siblings.left_of(left).last 79 end
level()
click to toggle source
Returns the level of this object in the tree root level is 0
# File lib/awesome_nested_set/model/relatable.rb 38 def level 39 parent_id.nil? ? 0 : compute_level 40 end
right_sibling()
click to toggle source
Find the first sibling to the right
# File lib/awesome_nested_set/model/relatable.rb 82 def right_sibling 83 siblings.right_of(left).first 84 end
root()
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 86 def root 87 return self_and_ancestors.children_of(nil).first if persisted? 88 89 if parent_id && current_parent = nested_set_scope.where(primary_column_name => parent_id).first! 90 current_parent.root 91 else 92 self 93 end 94 end
roots()
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 96 def roots 97 nested_set_scope.where(parent_id: nil) 98 end
same_scope?(other)
click to toggle source
Check if other model is in the same scope
# File lib/awesome_nested_set/model/relatable.rb 70 def same_scope?(other) 71 Array(acts_as_nested_set_options[:scope]).all? do |attr| 72 self.send(attr) == other.send(attr) 73 end 74 end
self_and_ancestors()
click to toggle source
Returns the collection of all parents and self
# File lib/awesome_nested_set/model/relatable.rb 13 def self_and_ancestors 14 nested_set_scope. 15 where(arel_table[left_column_name].lteq(left)). 16 where(arel_table[right_column_name].gteq(right)) 17 end
self_and_descendants()
click to toggle source
Returns a collection including itself and all of its nested children
# File lib/awesome_nested_set/model/relatable.rb 48 def self_and_descendants 49 # using _left_ for both sides here lets us benefit from an index on that column if one exists 50 nested_set_scope.right_of(left).left_of(right) 51 end
self_and_siblings()
click to toggle source
Returns the collection of all children of the parent, including self
# File lib/awesome_nested_set/model/relatable.rb 25 def self_and_siblings 26 nested_set_scope.children_of parent_id 27 end
siblings()
click to toggle source
Returns the collection of all children of the parent, except self
# File lib/awesome_nested_set/model/relatable.rb 20 def siblings 21 without_self self_and_siblings 22 end
Protected Instance Methods
compute_level()
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 102 def compute_level 103 node, nesting = determine_depth 104 105 node == self ? ancestors.count : node.level + nesting 106 end
determine_depth(node = self, nesting = 0)
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 108 def determine_depth(node = self, nesting = 0) 109 while (association = node.association(:parent)).loaded? && association.target 110 nesting += 1 111 node = node.parent 112 end if node.respond_to?(:association) 113 114 [node, nesting] 115 end
within_node?(node, within)
click to toggle source
# File lib/awesome_nested_set/model/relatable.rb 117 def within_node?(node, within) 118 node.left < within.left && within.left < node.right 119 end