lib/cultural_network/contextual_tree_builder.rb
# node = a node with the methods: children, roots, parent, siblings
# context = used only by this method
#
node = a node with the methods: children, roots, parent, siblings
context = used only by this method
node = a node with the methods : children roots parent siblings context = used only by this method
def build(node, context={}, &block)
      child_list = ''
      if node.nil?
        send_to(@options[:model], :roots).collect do |node|
          if block_given?
            next unless yield(node)
          end
          child_list += new_list_item(node, nil)
      	end
      	return child_list.html_safe
      end
      context[:target] ||= node
      send_to(node, :children).each do |child|
        if block_given?
          next unless yield(child)
        end
        if child==context[:node]
          child_list += context[:list]
        else
          child_list += new_list_item(child, context[:target])
        end
      end
      child_list = new_list(child_list.html_safe, node, context[:target])
      if send_to(node, :siblings).size > 0 && ! send_to(node, :parent)
        list=''
        send_to(@options[:model], :roots).each do |root|
          if block_given?
            next unless yield(root)
          end
          if root == node
            list += new_list_item(root, context[:target]) + child_list
          else
            list += new_list_item(root, context[:target])
          end
        end
      else
        list = new_list_item(node, context[:target]) + child_list
      end
      if send_to(node, :parent)
        dont_skip = true
        if block_given?
          dont_skip = false unless yield(node)
        end
        if dont_skip
          context[:node]=node
          context[:list]=list
          if block_given?
            list = build(send_to(node, :parent), context) {|node| yield(node) }
          else
            list = build(send_to(node, :parent), context)
          end
        end
      end
      list
    end
def build ( node , context = { } , & block ) child_list = '' if node . nil? send_to ( @options [ :model ] , :roots ) . collect do | node | if block_given? next unless yield ( node ) end child_list += new_list_item ( node , nil ) end return child_list . html_safe end context [ :target ] ||= node send_to ( node , :children ) . each do | child | if block_given? next unless yield ( child ) end if child == context [ :node ] child_list += context [ :list ] else child_list += new_list_item ( child , context [ :target ] ) end end child_list = new_list ( child_list . html_safe , node , context [ :target ] ) if send_to ( node , :siblings ) . size > 0 && ! send_to ( node , :parent ) list = '' send_to ( @options [ :model ] , :roots ) . each do | root | if block_given? next unless yield ( root ) end if root == node list += new_list_item ( root , context [ :target ] ) + child_list else list += new_list_item ( root , context [ :target ] ) end end else list = new_list_item ( node , context [ :target ] ) + child_list end if send_to ( node , :parent ) dont_skip = true if block_given? dont_skip = false unless yield ( node ) end if dont_skip context [ :node ] = node context [ :list ] = list if block_given? list = build ( send_to ( node , :parent ) , context ) { | node | yield ( node ) } else list = build ( send_to ( node , :parent ) , context ) end end end list end
node = a node with the methods: children, roots, parent, siblings
context = used only by this method
node = a node with the methods : children roots parent siblings context = used only by this method
# Prepare to create the child list items # Save the target ID thru all iterations # loop thru children #node.children.each do |child| # if this child matches the previous iteration node # use the previously created list # build a list item # wrap the child list in a ul if needed # create the top-level list item, include the child list # if block_given? #           next unless yield(node) #         end # Added the child list, this is the target node! # parentless sibling # If there is a parent to this node # save the current node # save the current list # move up to the parent, passing it in the current node and list # be sure to wrap this list in a ul!
node
<not_specific>
context
<not_specific>
block
<not_specific>