Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # In order to build the tree structure, we select comments that are only root nodes,
- # i.e. comments that are not a reply. From there, we can build the tree structure based
- # on their children, which we collected earlier.
- transform_node_index(node_index.select { |node_id, data| data[:parent].nil? }.values)
- # This is a recursive method that will traverse the comment structure, and run for
- # the children of each comment. In the end, once the recursion finishes, a tree pops out.
- def transform_node_index(nodes)
- nodes.map do |data|
- {
- comment: data[:node],
- reply_to: data[:reply_to],
- children: transform_node_index(data[:children])
- }
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement