Module: Locomotive::Extensions::Page::Tree::ClassMethods

Defined in:
app/models/locomotive/extensions/page/tree.rb

Instance Method Summary collapse

Instance Method Details

#_quick_tree(current_page, pages) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/locomotive/extensions/page/tree.rb', line 52

def _quick_tree(current_page, pages)
  i, children = 0, []

  while !pages.empty?
    page = pages[i]

    break if page.nil?

    if page.parent_id == current_page.id
      page = pages.delete_at(i)

      children << _quick_tree(page, pages)
    else
      i += 1
    end
  end

  current_page.instance_eval do
    def children=(list); @children = list; end
    def children; @children || []; end
  end

  current_page.children = children

  current_page
end

#quick_tree(site, minimal_attributes = true) ⇒ Array

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.

Parameters:

Returns:

  • (Array)

    The first array of pages (depth = 0)



39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/locomotive/extensions/page/tree.rb', line 39

def quick_tree(site, minimal_attributes = true)
  pages = (minimal_attributes ? site.pages.unscoped.minimal_attributes : site.pages.unscoped).order_by([:depth.asc, :position.asc]).to_a

  tmp = []

  while !pages.empty?
    tmp << _quick_tree(pages.delete_at(0), pages)
  end

  tmp
end