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


48
49
50
51
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 48

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]

40
41
42
43
44
45
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 40

def ancestors
  node = self
  nodes = []
  nodes << node = node.parent while node.parent
  nodes
end

#localized_subpagesObject


53
54
55
56
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 53

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.


59
60
61
62
63
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 59

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


71
72
73
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 71

def pages(_options = nil)
  localized_subpages.published
end

#parentObject

Returns the pages parent


66
67
68
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 66

def parent
  super.try { |node| node.localize(locale) }
end

#previous_siblingObject

Finds the page's next sibling. Returns nil if there isn't one.


76
77
78
79
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 76

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.


82
83
84
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 82

def root
  self_and_ancestors.last
end

#self_and_ancestorsObject

Returns ancestors and current node itself.

subchild1.self_and_ancestors # => [subchild1, child1, root]

89
90
91
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 89

def self_and_ancestors
  [self] + ancestors
end

#siblingsObject

Returns all siblings, including self.


94
95
96
97
98
99
100
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 94

def siblings
  if parent
    parent.pages
  else
    self.class.roots.map { |node| node.localize(locale) }
  end
end

#subpagesObject


102
103
104
# File 'app/models/concerns/pages_core/page_model/tree.rb', line 102

def subpages
  children.order(content_order)
end