Code Monkey home page Code Monkey logo

muck-comments's Introduction

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

muck-comments's People

Contributors

jbasdf avatar

Stargazers

Mariano Crowe avatar  avatar Joel Duffin avatar  avatar

Watchers

 avatar Joel Duffin avatar  avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.