Coder Social home page Coder Social logo

sximba / acts_as_commentable_with_reply Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 144 KB

Plugin/gem that provides comment functionality with one-level nesting of comment replies (based on acts_as_commentable)

License: MIT License

Ruby 99.46% Shell 0.54%

acts_as_commentable_with_reply's Introduction

Acts As Commentable with Reply

Provides a single Comment model that can be attached to any model(s) within your app. It creates Comment and CommentReply models and handles the plumbing between those models and any models that you declare to be commentable models. The replies are limited to one-level nesting. If you need no nesting then check out acts_as_commentable. If you need deeper nesting then checkout acts_as_commentable_with_threading.

The advantage of this gem over acts_as_commentable_with_threading is that it does not use the nested set model and this leads to faster writes. Check this Wikipedia article for further reading on the nested set model.

Installation :

Add the following line to your Gemfile

Rails 4

gem 'acts_as_commentable_with_reply'

Generator

Rails 4

rails g comment

Then migrate your database to add the comments and comment_replies models:

rake db:migrate

Usage

Make the desired ActiveRecord model act as commentable:

class Post < ActiveRecord::Base
  acts_as_commentable
end

Add a comment to a model instance by adding the following to the controller:

commentable = Post.create
comment = commentable.comments.create
comment.title = "First comment."
comment.comment = "This is the first comment."
comment.save

Add a reply to a comment instance by adding the following to the controller:

comment = Comment.find x # Any other way of obtaining a comment instance
reply = comment.comment_replies.create :reply => "This is the first reply."

Fetch comments for the commentable model by adding the following to the view:

commentable = Post.find(1)
comments = commentable.comments.recent.limit(10).all

Fetch replies for a comment instance by adding the following to the controller/view:

comment = Comment.find x # Any other way of obtaining a comment instance
replies = CommentReply.find_comment_replies comment

You can also add different types of comments to a model:

class Todo < ActiveRecord::Base
  acts_as_commentable :public, :private
end

To fetch the different types of comments for this model:

public_comments = Todo.find(1).public_comments
private_comments = Todo.find(1).private_comments

By default, ‘acts_as_commentable` will assume you are using a `Comment` model. To change this, or change any other join options, pass in an options hash:

class Todo < ActiveRecord::Base
  acts_as_commentable class_name: 'MyComment'
end

This also works in conjunction with comment types:

class Todo < ActiveRecord::Base
  acts_as_commentable :public, :private, { class_name: 'MyComment' }
end

Credits

Jack Dempsey - This plugin is heavily influenced by Acts As Commentable.

Contributors

Lungelo Ximba

acts_as_commentable_with_reply's People

Contributors

sximba avatar

Stargazers

Michael avatar Michael Nowak avatar

Watchers

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.