Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. # In order to build the tree structure, we select comments that are only root nodes,
  2. # i.e. comments that are not a reply. From there, we can build the tree structure based
  3. # on their children, which we collected earlier.
  4. transform_node_index(node_index.select { |node_id, data| data[:parent].nil? }.values)
  5.  
  6. # This is a recursive method that will traverse the comment structure, and run for
  7. # the children of each comment. In the end, once the recursion finishes, a tree pops out.
  8. def transform_node_index(nodes)
  9. nodes.map do |data|
  10. {
  11. comment: data[:node],
  12. reply_to: data[:reply_to],
  13. children: transform_node_index(data[:children])
  14. }
  15. end
  16. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement