Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def topic_nested_list(topics_list)
- get_nested_list(topics_list, topics_list.first)
- end
- def get_nested_list(topics, parent)
- ul_contents = ""
- ul_contents << "<ul>"
- childs = get_topic_childs(topics,parent.id)
- if childs.blank?
- ul_contents << "<li>" << parent.title << "</li>"
- else
- for child in topics
- ul_contents << get_nested_list(topics, child)
- end
- end
- ul_contents << "</ul>"
- end
- def get_topic_childs(topic_list, id)
- childs = []
- topic_list.each do |topic|
- if topic.parent_id == id
- childs.push(topic)
- end
- end
- return childs
- end
- def process_topics topics_list
- topics_list.each do |t|
- # do something
- process_topics children_of_topic(topic_list, t)
- # or do something
- end
- end
- def children_of_topic(topic_list, topic)
- topic_list.select(|t| t.id == topic.parent_id)
- end
- get_nested_list(topics_list, topics_list.first)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement