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
70 71 72 73 74 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 70 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]
62 63 64 65 66 67 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 62 def ancestors node = self nodes = [] nodes << node = node.parent while node.parent nodes end |
#localized_subpages ⇒ Object
76 77 78 79 80 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 76 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.
83 84 85 86 87 88 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 83 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
96 97 98 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 96 def pages( = nil) localized_subpages.published end |
#parent ⇒ Object
Returns the pages parent
91 92 93 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 91 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.
101 102 103 104 105 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 101 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.
108 109 110 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 108 def root self_and_ancestors.last end |
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
115 116 117 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 115 def self_and_ancestors [self] + ancestors end |
#siblings ⇒ Object
Returns all siblings, including self.
120 121 122 123 124 125 126 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 120 def siblings if parent parent.pages else self.class.roots.map { |node| node.localize(locale) } end end |
#subpages ⇒ Object
128 129 130 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 128 def subpages children.order(content_order) end |