hello everyone,
i have a model class Page in my rails application which is defined like
this:
class Page < ActiveRecord::Base
attr_accessor :children
# relationships here..
end
and a NodeList class to sort this pages based on their parent_id
attributes: http://pastie.org/private/rbq05vzkc7wgyan8pcdrw
here is a simple example of how i'm using them:
node_list = NodeList.new(Page.all())
@pages_list = node_list.get_nodes()
i also have a helper method to print the tree:
def print_children(nodes)
retval = "<ul>"
nodes.each do |node|
retval += "<li id=\"page_#{node.id}\"><a
href=\"/documents/#{node.document_id}/pages/#{node.id}\">#{node.name}</a>"
if node.children && node.children.size > 0
retval += print_children(node.children)
end
retval += "</li>"
end
retval += "</ul>"
return retval
end
currently it is giving me what i need:
- Parent 1
-- Child 1
--- Sub Child 1
--- Sub Child 2
-- Child 2
-- Child 3
- Parent 2
-- Child 4
-- Child 5
--- Sub Child 3
so, here are my two questions:
- how can i make this better performing and rubyish?
- as this is looping through every element of @page_list, after i'm done
with the element, i would like to remove it with @page_list.delete(obj) in
get_children method after the line "tmp << obj" however this is causing me
lose data so instead of the above structure i get something like this:
- Parent 1
-- Child 1
--- Sub Child 1
-- Child 2
- Parent 2
-- Child 4
why is this happening?
thanks in advance.
Muhammet S. AYDIN
http://mengu.net
http://compector.com