Module: Forester::Views

Included in:
TreeNode
Defined in:
lib/forester/tree_node_ext/views.rb

Instance Method Summary collapse

Instance Method Details

#as_root_hash(options = {}) ⇒ Object



4
5
6
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
# File 'lib/forester/tree_node_ext/views.rb', line 4

def as_root_hash(options = {})
  default_options = {
    fields_to_include: :all,
    max_level:         :last,
    children_key:      :children,
    stringify_keys:    false,
    symbolize_keys:    false
  }
  options = default_options.merge(options)

  hash = content.to_hash(options)

  children_key = options[:children_key]
  children_key = children_key.to_s if options[:stringify_keys]

  max_level = options[:max_level]
  max_level = -1 if max_level == :last

  next_children =
    if max_level == 0
      []
    else
      next_options = options.merge(max_level: max_level - 1)
      children.map { |node| node.as_root_hash(next_options) }
    end

  hash.merge({ children_key => next_children })
end