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:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/locomotive/extensions/page/tree.rb', line 75

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_variable_set(:@children, children || [])

  current_page
end

#order_by_depth_and_positionObject



49
50
51
# File 'app/models/locomotive/extensions/page/tree.rb', line 49

def order_by_depth_and_position
  order_by(:depth.asc, :position.asc)
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)



62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/locomotive/extensions/page/tree.rb', line 62

def quick_tree(site, minimal_attributes = true)
  pages = (minimal_attributes ? site.pages.unscoped.minimal_attributes : site.pages.unscoped).order_by_depth_and_position.to_a

  tmp = []

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

  tmp
end