Class: Alchemy::PageTreeSerializer

Inherits:
BaseSerializer show all
Defined in:
app/serializers/alchemy/page_tree_serializer.rb

Instance Attribute Summary

Attributes inherited from BaseSerializer

#object, #opts

Instance Method Summary collapse

Methods inherited from BaseSerializer

#initialize

Constructor Details

This class inherits a constructor from Alchemy::BaseSerializer

Instance Method Details

#attributesObject



3
4
5
# File 'app/serializers/alchemy/page_tree_serializer.rb', line 3

def attributes
  {'pages' => nil}
end

#pagesObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/serializers/alchemy/page_tree_serializer.rb', line 7

def pages
  tree = []
  path = [{id: object.parent_id, children: tree}]
  page_list = object.self_and_descendants
  skip_branch = false
  base_level = object.level - 1

  page_list.each_with_index do |page, i|
    has_children = page_list[i + 1] && page_list[i + 1].parent_id == page.id
    folded = has_children && page.folded?(opts[:user])

    if skip_branch
      next if page.parent_id == path.last[:children].last[:id]

      skip_branch = false
    end

    # Do not walk my children if I'm folded and you don't need to have the
    # full tree.
    if folded && !opts[:full]
      skip_branch = true
    end

    if page.parent_id != path.last[:id]
      if path.map { |o| o[:id] }.include?(page.parent_id) # Lower level
        path.pop while path.last[:id] != page.parent_id
      else # One level up
        path << path.last[:children].last
      end
    end

    level = path.count + base_level

    path.last[:children] << page_hash(page, has_children, level, folded)
  end

  tree
end