def tandem_navigation_tag(active_page, pages_collection = nil, html_options = {})
      html_options, pages_collection = pages_collection, nil if pages_collection.is_a?(Hash)

      html_options[:class] ||= 'nav'

      page_groups = (pages_collection || Page.all).inject({}) do |groups, page|
        if groups[page.parent_id.to_s]
          groups[page.parent_id.to_s] << page
        else
          groups[page.parent_id.to_s] = [page]
        end
        groups
      end

      # generate must be in scope for the iterate proc declaration, but must be defined below iterate, so that iterate is recursively in scope
      generate = nil

      iterate = Proc.new do |parent_id|
        #very important to delete the group from the collection, or it is possible users can create looping relationships
        (page_groups.delete(parent_id.to_s) || {}).inject(''.html_safe) do |buffer, page|
          buffer + generate.call(page)
        end
      end

      generate = Proc.new do |page|
        content_tag_for(:li, page, :tandem, class: "#{page == active_page ? 'active' : ''}") do
          link_to(page.link_label, tandem.page_path(page), class: "#{page == active_page ? 'active' : ''}") +
          content_tag(:ul) do
            iterate.call(page.id)
          end
        end
      end

      content_tag(:ul, html_options) do
        iterate.call(page_groups.keys.first)
      end
    end
def tandem_navigation_tag(active_page, pages_collection = nil, html_options = {})
      html_options, pages_collection = pages_collection, nil if pages_collection.is_a?(Hash)

      html_options[:class] ||= 'nav'

      page_groups = (pages_collection || Page.all).inject({}) do |groups, page|
        if groups[page.parent_id.to_s]
          groups[page.parent_id.to_s] << page
        else
          groups[page.parent_id.to_s] = [page]
        end
        groups
      end

      # generate must be in scope for the iterate proc declaration, but must be defined below iterate, so that iterate is recursively in scope
      generate = nil

      iterate = Proc.new do |parent_id|
        #very important to delete the group from the collection, or it is possible users can create looping relationships
        (page_groups.delete(parent_id.to_s) || {}).inject(''.html_safe) do |buffer, page|
          buffer + generate.call(page)
        end
      end

      generate = Proc.new do |page|
        content_tag_for(:li, page, :tandem, class: "#{page == active_page ? 'active' : ''}") do
          link_to(page.link_label, tandem.page_path(page), class: "#{page == active_page ? 'active' : ''}") +
          content_tag(:ul) do
            iterate.call(page.id)
          end
        end
      end

      content_tag(:ul, html_options) do
        iterate.call(page_groups.keys.first)
      end
    end
todo... document this