merge_response_content

in presenters/thread.rb [95:121]


  def merge_response_content(content)
    top_level = []
    ancestry = []
    content.each do |item|
      item_hash = item.to_hash.merge!("children" => [])
      if item.parent_id.nil?
        top_level << item_hash
        ancestry = [item_hash]
      else
        while ancestry.length > 0 do
          if item.parent_id == ancestry.last["id"]
            ancestry.last["children"] << item_hash
            ancestry << item_hash
            break
          else
            ancestry.pop
            next
          end
        end
        if ancestry.empty? 
          next
        end
      end
    end
    top_level
  end