0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Add comment support for ActiveRecord models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.8.7
~> 1.17
~> 2.6
~> 1.3

Runtime

 Project Readme

Commentable¶ ↑

This is a really simple ActiveRecord plugin that allows models to be commented.

Commentable is tightly coupled to an User model. If you don’t have it, there’s no game for you here.

Installation¶ ↑

gem install commentable

Usage¶ ↑

First, you’ll have to create the comments table. Run rails generate migration create_comments and add the following code:

class CreateComments < ActiveRecord::Migration
  def self.up
    create_table :comments do |t|
      t.text :body, :formatted_body
      t.references :commentable, :polymorphic => true
      t.references :user
      t.timestamps
    end

    add_index :comments, [:commentable_id, :commentable_type], :name => "index_on_commentable"
    add_index :comments, [:commentable_id, :commentable_type, :user_id], :name => "index_on_commentable_and_user"
  end

  def self.down
    drop_table :comments
  end
end

Run rake db:migrate to apply this new migration.

Now, add the commentable macro to all models that can be commented.

class Task < ActiveRecord::Base
  commentable
end

This will create an association called comments. You can now create comments like any ActiveRecord model.

comment = @task.comments.create(:body => "Awesome job!", :user => @user)

There’s a shortcut for that.

comment = @task.add_comment(:body => "Awesome job!", :user => @user)

You can have formatted comments. This is specially useful if you want to provide Markdown support, for instance. Just use the :format option passing any object that responds to the call method. The block’s return will be used as the formatted_body attribute.

class Task < ActiveRecord::Base
  commentable :format => proc {|comment| Redcarpet.new(comment.body.to_s).to_html}
end

Final note: remember to add a comments_count column to every single model that will be commentable. This is required by the Comment model.

Maintainer¶ ↑

License¶ ↑

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.