Guest User

Untitled

a guest
Feb 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # Returns hierarchical data structure rooted at this node. When converting to
  2. # json make sure to remove :parent to avoid circular reference:
  3. # message.full_hierarchy.to_json(:only => [:id, :subject, :children, ...])
  4. def full_hierarchy
  5. the_full_set = full_set
  6. ancestry = [build_node(the_full_set.shift)]
  7. while !the_full_set.empty?
  8. next_node = build_node(the_full_set.shift)
  9. while next_node[:level] <= ancestry.last[:level]
  10. ancestry.pop
  11. end
  12. ancestry.last[:children] << next_node
  13. ancestry.last[:leaf] = false
  14. next_node[:parent] = ancestry.last
  15. ancestry << next_node
  16. end
  17. ancestry[0]
  18. end
  19.  
  20. private
  21.  
  22. # active record Message to node for hierarchy
  23. def build_node(message)
  24. ret = {}
  25. ret[:id] = message.id
  26. ret[:subject] = message.subject
  27. ret[:message] = message.message
  28. ret[:level] = message.level
  29. ret[:account_id] = message.account_id
  30. ret[:author] = message.account.login
  31. ret[:children] = []
  32. ret[:parent] = nil
  33. ret[:leaf] = true
  34. ret
  35. end
Add Comment
Please, Sign In to add comment