Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. def topic_nested_list(topics_list)
  2. get_nested_list(topics_list, topics_list.first)
  3. end
  4.  
  5. def get_nested_list(topics, parent)
  6.  
  7. ul_contents = ""
  8. ul_contents << "<ul>"
  9. childs = get_topic_childs(topics,parent.id)
  10.  
  11. if childs.blank?
  12. ul_contents << "<li>" << parent.title << "</li>"
  13. else
  14. for child in topics
  15. ul_contents << get_nested_list(topics, child)
  16. end
  17. end
  18. ul_contents << "</ul>"
  19. end
  20.  
  21. def get_topic_childs(topic_list, id)
  22.  
  23. childs = []
  24. topic_list.each do |topic|
  25. if topic.parent_id == id
  26. childs.push(topic)
  27. end
  28. end
  29.  
  30. return childs
  31. end
  32.  
  33. def process_topics topics_list
  34. topics_list.each do |t|
  35. # do something
  36. process_topics children_of_topic(topic_list, t)
  37. # or do something
  38. end
  39. end
  40.  
  41. def children_of_topic(topic_list, topic)
  42. topic_list.select(|t| t.id == topic.parent_id)
  43. end
  44.  
  45. get_nested_list(topics_list, topics_list.first)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement