Module: PagesCore::PageModel::Tree::InstanceMethods
- Defined in:
- app/models/concerns/pages_core/page_model/tree.rb
Instance Method Summary collapse
-
#all_subpages ⇒ Object
Returns all children, recursively.
-
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
- #localized_subpages ⇒ Object
-
#next_sibling ⇒ Object
Finds the page’s next sibling.
-
#pages(_options = nil) ⇒ Object
Get subpages.
-
#parent ⇒ Object
Returns the pages parent.
-
#previous_sibling ⇒ Object
Finds the page’s next sibling.
-
#root ⇒ Object
Returns the root node of the tree.
-
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
-
#siblings ⇒ Object
Returns all siblings, including self.
- #subpages ⇒ Object
Instance Method Details
#all_subpages ⇒ Object
Returns all children, recursively
67 68 69 70 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 67 def all_subpages return nil unless subpages.any? localized_subpages.map { |p| [p, p.all_subpages] }.flatten.compact end |
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
59 60 61 62 63 64 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 59 def ancestors node = self nodes = [] nodes << node = node.parent while node.parent nodes end |
#localized_subpages ⇒ Object
72 73 74 75 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 72 def localized_subpages return subpages unless locale? subpages.localized(locale) end |
#next_sibling ⇒ Object
Finds the page’s next sibling. Returns nil if there isn’t one.
78 79 80 81 82 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 78 def next_sibling return unless siblings.any? siblings[(siblings.to_a.index(self) + 1)...siblings.length] .try(&:first) end |
#pages(_options = nil) ⇒ Object
Get subpages
90 91 92 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 90 def pages( = nil) localized_subpages.published end |
#parent ⇒ Object
Returns the pages parent
85 86 87 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 85 def parent super.try { |node| node.localize(locale) } end |
#previous_sibling ⇒ Object
Finds the page’s next sibling. Returns nil if there isn’t one.
95 96 97 98 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 95 def previous_sibling return unless siblings.any? siblings[0...siblings.to_a.index(self)].try(&:last) end |
#root ⇒ Object
Returns the root node of the tree.
101 102 103 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 101 def root self_and_ancestors.last end |
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
108 109 110 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 108 def self_and_ancestors [self] + ancestors end |
#siblings ⇒ Object
Returns all siblings, including self.
113 114 115 116 117 118 119 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 113 def siblings if parent parent.pages else self.class.roots.map { |node| node.localize(locale) } end end |
#subpages ⇒ Object
121 122 123 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 121 def subpages children.order(content_order) end |