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 previous 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 89 90 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 83 def next_sibling return unless persisted? && siblings.any? index = siblings.to_a.index(self) return unless index siblings[(index + 1)...siblings.length].try(&:first) end |
#pages(_options = nil) ⇒ Object
Get subpages
98 99 100 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 98 def pages( = nil) localized_subpages.published end |
#parent ⇒ Object
Returns the pages parent
93 94 95 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 93 def parent super.try { |node| node.localize(locale) } end |
#previous_sibling ⇒ Object
Finds the page’s previous sibling. Returns nil if there isn’t one.
103 104 105 106 107 108 109 110 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 103 def previous_sibling return unless persisted? && siblings.any? index = siblings.to_a.index(self) return unless index siblings[0...index].try(&:last) end |
#root ⇒ Object
Returns the root node of the tree.
113 114 115 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 113 def root self_and_ancestors.last end |
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
120 121 122 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 120 def self_and_ancestors [self] + ancestors end |
#siblings ⇒ Object
Returns all siblings, including self.
125 126 127 128 129 130 131 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 125 def siblings if parent parent.pages else self.class.roots.map { |node| node.localize(locale) } end end |
#subpages ⇒ Object
133 134 135 |
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 133 def subpages children.order(content_order) end |