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

Add the gem to your Gemfile:

gem "muck-comments"

Usage

Comment model

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

class Comment < ActiveRecord::Base
  include MuckComments::Models::MuckComment
end

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 'include MuckComments::Models::MuckComment' can override 'can_comment?' to 
  # change how comment permissions are handled.
  def has_permission_to_comment(user, parent)
    parent.can_comment?(user)
  end

end

Attach comments to a model:

For a given model add 'include MuckComments::Models::MuckComment'

class Blog < ActiveRecord::Base
  include MuckComments::Models::MuckComment
end

'include MuckComments::Models::MuckComment' will automatically track the count, but you will need to add a column to your table called comment_count:

class AddCommentCache < ActiveRecord::Migration
  def self.up
    add_column :blogs, :comment_count, :integer, :default => 0
  end

  def self.down
    remove_column :blogs, :comment_count
  end
end

Then you will be able to do this to get a comment count:

@blog.comment_count

Configuration

Create an initializer with the following:

MuckComments.configure do |config|
  config.send_email_for_new_comments = true # If true this will send out an email to each user that has participated in a comment thread.  The default email is basic and only includes the body
                                            # of the comment.  Add new email views to provide a better email for you users.  They can be found in app/views/comment_mailer/new_comment.html.erb
                                            # and app/views/comment_mailer/new_comment.text.erb
  config.sanitize_content = true            # Turns sanitize off/on for comments. We highly recommend leaving this on.
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 include MuckComments::Models::MuckCommentable 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-2010 Tatemai, released under the MIT license