Rails acts_as_threaded Plugin 12
While tinkering around with the acts_as_nested_set act of ActiveRecord, I tried to build a threaded forum with it. I ran into some difficulties in that a nested_set is designed to only have one root. Obviously, in a threaded forum, each thread would be its own root. So after wasting a couple hours trying to make nested_set work, I decided to use what I consider to be the best part of Rails: plugins. "acts_as_threaded" was born 1 hour later.
While I used it to build a threaded forum in this demo, it's design was inspired by a different project at work. The acts_as_tree behavior could have worked, but I needed a better way of containing my children (much like in real life).
I've included an 11 minute screencast of building an ugly looking Threaded Forum, but the concepts are there. I can still remember spending many man hours with my programming team at Bravenet to design and build the forum product we offered there. If Rails existed back then, we could have built it in a day. (Leave me a comment on how the screencast turned out.)
Click the image to view the screencast of acts_as_threaded in use: (8 MB)
As usual, you can download the plugin here. I will submit it as a patch when I solidify it and create the tests.
If you are following the tutorial, here is the display code in full.
def display_threads(threads)
content = ''
for thread in threads
content << content_tag('div',
content_tag('div',"* " + link_to("#{h thread.title}", {:action => 'show', :id => thread.id}, {'style' => 'color: #00f'}) + " " +
content_tag('span', " by #{h thread.name} · #{thread.created_at.strftime('%b, %d %Y - %I:%M %p')}", 'style' => 'font:10px tahoma;color:#666;')),
'style' => "margin:5px 0px;padding-left:#{thread.depth*20}")
end
content
end
UPDATE: See how to do this without a plugin using native Rails functionality.
http://www.railtie.net/articles/2006/03/31/implement-acts_as_threaded-without-a-plugin