A Social Network in Rails: Comments

This post is part of an ongoing feature about creating a social network in Rails.

This time, we're adding comments to our Photo model, as presented in the elegant permalinks post. First of all, I looked for a gem to do this job for us, and found acts_as_commentable. Its setup goes something like this:

Add the gem to our Gemfile, and run `bundle install`:
gem 'acts_as_commentable'
Generate the tables and migrate the DB:
$ rails g comment
$ rake db:migrate
Add the comments to our Photo model:
class Photo < ActiveRecord::Base
  acts_as_commentable
end
By doing this, we get the following interface to the comments in our photos:

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

commentable = Post.find(1)
comments = commentable.comments.recent.limit(10).all
However, this solution, while adding another dependency to a gem, does not give us some functionality we would like to have: the comments are not linked to the users, we have to set up the controller by hand, views and json interface is missing…

A simpler solution, without any gem, is creating the comments as a tipical ActiveRecord model, harnessing the (often overlooked) power of the Rails generators:
$ rails generate scaffold Comment text:text photo:belongs_to user:belongs_to
      invoke  active_record
      create    db/migrate/20141230125623_create_comments.rb
      create    app/models/comment.rb
      invoke  resource_route
       route    resources :comments
      invoke  responders_controller
      create    app/controllers/comments_controller.rb
      invoke    erb
      create      app/views/comments
      create      app/views/comments/index.html.erb
      create      app/views/comments/edit.html.erb
      create      app/views/comments/show.html.erb
      create      app/views/comments/new.html.erb
      create      app/views/comments/_form.html.erb
      invoke    helper
      create      app/helpers/comments_helper.rb
      invoke    jbuilder
      create      app/views/comments/index.json.jbuilder
      create      app/views/comments/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/comments.coffee
      invoke    scss
      create      app/assets/stylesheets/comments.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

$ rake db:migrate
Done! Now we have the comments, linked to the User and Photo, database table with indexes, controller and views. All this in 2 lines of bash. If we don't need a specific view or action (let's say we don't want users to delete posts) it's as simple as removing the corresponding file and action from the controller.

As I said on the original post, this guide is meant to make the most of the Rails gem ecosystem. In this case, though, creating the feature ourselves is actually faster, while not bringing a new dependency to our app, and giving us more control over our code.

Tune up next for how to add notifications to our social network, coming soon!

No comments:

Post a Comment