Class: Alchemy::PageTreeSerializer
Instance Attribute Summary
#object, #opts
Instance Method Summary
collapse
#initialize
Instance Method Details
#attributes ⇒ Object
3
4
5
|
# File 'app/serializers/alchemy/page_tree_serializer.rb', line 3
def attributes
{'pages' => nil}
end
|
#pages ⇒ Object
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
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) path.pop while path.last[:id] != page.parent_id
else 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
|