Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. # This tree utility methods assume that the models have appropriate relationships already defined.
  2. # For example, `Node` class should have `children` associations pointing to children nodes.
  3.  
  4. def traverse(root_node)
  5. puts root_node.name
  6. root_node.children.each {|c| traverse(c)} unless root_node.children.empty?
  7. end
  8.  
  9. def copy_tree(root_node)
  10. root_copy = root_node.dup
  11. root_copy.save! #if rails, set next available id instead
  12. root_node.children.each {|c| root_copy.children << copy_tree(c)} unless root_node.children.empty?
  13. root_copy
  14. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement