MuckComments

Add comments to any object. This gem is the muck version of acts_as_commentable_with_threading (github.com/elight/acts_as_commentable_with_threading/tree/master)

Setup

Install the the awesome_nested_set gem (github.com/collectiveidea/awesome_nested_set/tree/master)

gem sources -a http://gems.github.com
sudo gem install collectiveidea-awesome_nested_set

Then add it to your environment.rb:

config.gem "collectiveidea-awesome_nested_set", :lib => 'awesome_nested_set'

Usage

Comment model

Create a model called comment in your project and add the following:

class Comment < ActiveRecord::Base
  acts_as_muck_comment
end

This let’s you add any other methods to the comment model that you see fit.

Comment controller

Override the comments controller to change the the security model. For example:

class CommentsController < Muck::CommentsController

  before_filter :login_required # require the user to be logged in to make a comment

  # Modify this method to change how permissions are checked to see if a user can comment.
  # Each model that implements 'acts_as_muck_comment' can override can_comment? to 
  # change how comment permissions are handled.
  def has_permission_to_comment(user, parent)
    parent.can_comment?(user)
  end

end

Comment partial

When calling create in the comments controller with :format => ‘json’ the resulting json will include an ‘html’ field that contains a rendered version of the comment html. To facilitate this process be sure to create a partial called ‘_comment.html.erb’ under a directory with the same name as the parent object. The partial will be passed an object ‘comment_owner’ that references the parent object.

For example, for an object ‘activity’ that acts_as_commentable create a partial ‘activities/_comment.html.erb’. The contents might look like this:

<div id="<%= comment.dom_id %>">
  <span class="user"><%= link_to comment_owner.user, comment_owner.user %></span>
  <p><%= h comment.body %></p>
</div>

Copyright © 2009 Justin Ball, released under the MIT license