Created
May 27, 2009 19:26
-
-
Save ctdk/118846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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