💬 Parole adds the ability to comment on ActiveRecord records
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gemfiles
lib
spec
.gitignore
.rspec
.travis.yml
CHANGELOG.md
Gemfile
LICENSE.md
README.md
Rakefile
parole.gemspec

README.md

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 the commenter 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.

Note: The commenter model must implements a has_many relationship that fetch its comments.

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

class Article < ActiveRecord::Base
  acts_as_commentable roles: [:photos, :videos]
end
class User < ActiveRecord::Base
  has_many :comments, -> { where(commenter_type: 'User') }, foreign_key: :commenter_id
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
user.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
user.comments_count # => 2

License

Parole is © 2013-2015 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're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.