Module: PagesCore::PageModel::Tree::InstanceMethods

Defined in:
app/models/concerns/pages_core/page_model/tree.rb

Instance Method Summary collapse

Instance Method Details

#all_subpagesObject

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

#ancestorsObject

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_subpagesObject



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_siblingObject

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(_options = nil)
  localized_subpages.published
end

#parentObject

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_siblingObject

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

#rootObject

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_ancestorsObject

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

#siblingsObject

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

#subpagesObject



128
129
130
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 128

def subpages
  children.order(content_order)
end