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