Class: Locomotive::PageTreeService

Inherits:
Struct
  • Object
show all
Defined in:
app/services/locomotive/page_tree_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#siteObject

Returns the value of attribute site

Returns:

  • (Object)

    the current value of site



3
4
5
# File 'app/services/locomotive/page_tree_service.rb', line 3

def site
  @site
end

Instance Method Details

#build_treeArray

Returns the tree of pages from the site with the most minimal amount of queries. This method should only be used for read-only purpose since the mongodb returns the minimal set of required attributes to build the tree.

Returns:

  • (Array)

    The first array of pages (index + page not found + pages with depth == 1)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/locomotive/page_tree_service.rb', line 12

def build_tree
  pages, page_not_found = pages_with_minimun_attributes, nil

  [].tap do |tree|
    while page = pages.shift
      if page.not_found?
        # move the "page not found" (404) at the end of the array
        page_not_found = page
      elsif page.index?
        # make the index page without children
        tree << [page, nil]
      elsif !page.is_layout_or_related?
        tree << _build_tree(page, pages)
      end
    end

    tree << [page_not_found, nil]
  end
end