Skip to content

Instantly share code, notes, and snippets.

@ctdk
Created May 27, 2009 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctdk/118846 to your computer and use it in GitHub Desktop.
Save ctdk/118846 to your computer and use it in GitHub Desktop.
def self.treeify_comments(clist, pid = 0)
chash = Hash.new # comments grouped by parent_id
comhash = Hash.new # comments in a hash, w/ comment id as key
comments = Array.new # for the ordered comments
depth_tree = Hash.new # comment depth
clist.collect { |x| comhash[x.id] = x }
# ?
clist.collect do |c|
chash[c.parent_id] ||= Array.new
chash[c.parent_id] << c.id
end
cproc = lambda { |s| unless (chash[s].nil?); chash[s].collect { |i| comments << comhash[i]; cproc.call(i) } end }
# this might generalize it?
cproc.call(pid)
# better as collect? Maybe
comments.collect { |c| depth_tree[c.id] = (depth_tree[c.parent_id].nil?) ? 0 : depth_tree[c.parent_id] + 1 }
# send back
return [ comments, depth_tree ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment