Parole
Parole adds the ability to comment on ActiveRecord records.


Installation

Add this line to your application's Gemfile:

gem 'parole'

Then run the task to generate the migration:

$ rails generate parole:install

Usage

You should now be able to mark models as commentable:

class Article < ActiveRecord::Base
  acts_as_commentable
end

You’re pretty much done after that. You’re now able to do this:

user = User.find(1)
article = Article.find(1)

article.comments.create(commenter: user, comment: 'Hello world!')
article.comments.count # => 1

You can also provide roles for comments, so you can have multiple type of comments per record.

class Article < ActiveRecord::Base
  acts_as_commentable roles: [:photos, :videos]
end

user = User.find(1)
article = Article.find(1)

article.photos_comments.create(commenter: user, comment: 'Hello world!')
article.photos_comments.count # => 1
article.comments.count # => 1

Cache counters

Whenever a comment is created or destroyed, Parole looks into the commentable record and check if there’s a <role>_comments_count column and/or a comments_count column. If so, it updates them so they reflect the total number of comments and the number of comments of this role for the record.

So let’s say the Article model has the following columns: photos_comments_count, videos_comments_count and comments_count.

class Article < ActiveRecord::Base
  acts_as_commentable roles: [:photos, :videos]
end

user = User.find(1)
article = Article.find(1)

article.photos_comments.create(commenter: user, comment: 'Hello world!')
article.photos_comments_count # => 1
article.videos_comments_count # => 0
article.comments_count # => 1

article.videos_comments.create(commenter: user, comment: 'Hello world again!')
article.photos_comments_count # => 1
article.videos_comments_count # => 1
article.comments_count # => 2

License

Parole is © 2013 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for iPhone, iPad, Android, Blackberry, Windows Phone and Windows 8 in beautiful Quebec City.

We also love open-source software and we try to extract as much code as possible from our projects to give back to the community.