Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add comment controller. Add comment form and view on Article#show

  • Loading branch information...
commit db6ccb0e65de349c8c3fa8f1b474a39fe4c83fa9 1 parent 61da2e1
Terence Ponce authored
3  app/assets/javascripts/comments.js.coffee
... ... @@ -0,0 +1,3 @@
  1 +# Place all the behaviors and hooks related to the matching controller here.
  2 +# All this logic will automatically be available in application.js.
  3 +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3  app/assets/stylesheets/comments.css.scss
... ... @@ -0,0 +1,3 @@
  1 +// Place all the styles related to the Comments controller here.
  2 +// They will automatically be included in application.css.
  3 +// You can use Sass (SCSS) here: http://sass-lang.com/
2  app/controllers/articles_controller.rb
@@ -6,6 +6,8 @@ def index
6 6 end
7 7
8 8 def show
  9 + @comment = @article.comments.build
  10 + @comments = @article.comments.paginate(:page => params[:page])
9 11 end
10 12
11 13 def new
31 app/controllers/comments_controller.rb
... ... @@ -0,0 +1,31 @@
  1 +class CommentsController < ApplicationController
  2 + before_filter :build_comment
  3 + load_and_authorize_resource
  4 +
  5 + def create
  6 + @comment.user = current_user
  7 + if @comment.save
  8 + flash[:success] = "Successfully submitted comment!"
  9 + redirect_to :back
  10 + else
  11 + flash[:error] = "Failed to submit comment"
  12 + redirect_to :back
  13 + end
  14 + end
  15 +
  16 + private
  17 +
  18 + def build_comment
  19 + @commentable = find_commentable
  20 + @comment = @commentable.comments.build(params[:comment])
  21 + end
  22 +
  23 + def find_commentable
  24 + params.each do |name, value|
  25 + if name =~ /(.+)_id$/
  26 + return $1.classify.constantize.find(value)
  27 + end
  28 + end
  29 + nil
  30 + end
  31 +end
2  app/helpers/comments_helper.rb
... ... @@ -0,0 +1,2 @@
  1 +module CommentsHelper
  2 +end
8 app/models/ability.rb
@@ -9,6 +9,14 @@ def initialize(user)
9 9 else
10 10 can :read, :all
11 11
  12 + can :create, Comment
  13 + can :update, Comment do |comment|
  14 + comment.try(:user) == user
  15 + end
  16 + can :destroy, Comment do |comment|
  17 + comment.try(:user) == user
  18 + end
  19 +
12 20 if user.role?(:author)
13 21 can :create, Article
14 22 can :update, Article do |article|
21 app/views/articles/show.html.erb
@@ -21,3 +21,24 @@
21 21 <% if can? :destroy, @article %>
22 22 <%= link_to 'Destroy', article_path(@article), :confirm => 'Are you sure?', :method => :delete %>
23 23 <% end %>
  24 +<h2>Comments</h2>
  25 +<div>
  26 + <% if @article.comments.any? %>
  27 + <ul class="comments">
  28 + <%= render @comments %>
  29 + </ul>
  30 + <%= will_paginate @comments %>
  31 + <% end %>
  32 + <% if can? :create, @comment %>
  33 + <%= simple_form_for([@article, @comment]) do |f| %>
  34 + <%= render 'shared/error_messages', :object => f.object %>
  35 + <%= f.input :content, :label => 'Comment' %>
  36 +
  37 + <div class="form-actions">
  38 + <%= f.button :submit,
  39 + 'Submit comment',
  40 + :class => 'btn btn-large btn-primary' %>
  41 + </div>
  42 + <% end %>
  43 + <% end %>
  44 +</div>
7 app/views/comments/_comment.html.erb
... ... @@ -0,0 +1,7 @@
  1 +<li>
  2 + <span class="commenter"><%= comment.user.name %></span>
  3 + <span class="content"><%= comment.content %></span>
  4 + <span class="timestamp">
  5 + Posted <%= time_ago_in_words(comment.created_at) %> ago.
  6 + </span>
  7 +</li>
4 config/routes.rb
@@ -9,7 +9,9 @@
9 9 get 'settings', :to => 'devise/registrations#edit', :as => :edit_user_registration
10 10 end
11 11
12   - resources :articles
  12 + resources :articles do
  13 + resources :comments
  14 + end
13 15 resources :categories
14 16
15 17 match '/contact', :to => 'static_pages#contact'
2  spec/fabricators/comment_fabricator.rb
... ... @@ -1,6 +1,6 @@
1 1 Fabricator(:comment) do
2 2 content "This is a comment"
3   - user
  3 + user!
4 4 end
5 5
6 6 Fabricator(:article_comment, :from => :comment) do
59 spec/requests/articles_pages_spec.rb
@@ -11,7 +11,50 @@
11 11 before { visit articles_path }
12 12
13 13 it { should have_page_title 'Articles' }
14   - it { should_not have_page_heading 'Post article' }
  14 + it { should have_page_heading 'Articles' }
  15 + it { should_not have_link 'New article' }
  16 + it { should_not have_link 'Edit' }
  17 + it { should_not have_link 'Destroy' }
  18 + end
  19 +
  20 + describe 'in the show page' do
  21 +
  22 + before do
  23 + @article = Fabricate(:article)
  24 + visit article_path(@article)
  25 + end
  26 +
  27 + it { should have_page_title @article.title }
  28 + it { should have_page_heading @article.title }
  29 + it { should have_link 'Back to articles' }
  30 + it { should_not have_link 'Edit' }
  31 + it { should_not have_link 'Destroy' }
  32 +
  33 + describe 'comments section' do
  34 + it { should have_content 'Comments' }
  35 + it { should_not have_button 'Submit comment' }
  36 + end
  37 + end
  38 +
  39 + describe 'in the new page' do
  40 +
  41 + before { visit new_article_path }
  42 +
  43 + it { should have_error_message 'Access denied' }
  44 + it { should have_page_title '' }
  45 + it { should have_page_heading 'Developers Connect' }
  46 + end
  47 +
  48 + describe 'in the edit page' do
  49 +
  50 + before do
  51 + @article = Fabricate(:article)
  52 + visit edit_article_path(@article)
  53 + end
  54 +
  55 + it { should have_error_message 'Access denied' }
  56 + it { should have_page_title '' }
  57 + it { should have_page_heading 'Developers Connect' }
15 58 end
16 59 end
17 60
@@ -48,6 +91,20 @@
48 91 it { should have_link 'Back to articles' }
49 92 it { should_not have_link 'Edit' }
50 93 it { should_not have_link 'Destroy' }
  94 +
  95 + describe 'comments section' do
  96 + it { should have_content 'Comments' }
  97 +
  98 + describe 'on posting comments' do
  99 + before do
  100 + fill_in 'Comment', :with => 'Foobar'
  101 + click_button 'Submit comment'
  102 + end
  103 +
  104 + it { should have_content 'Foobar' }
  105 + it { should have_content @user.name }
  106 + end
  107 + end
51 108 end
52 109
53 110 describe 'in the new page' do

0 comments on commit db6ccb0

Please sign in to comment.
Something went wrong with that request. Please try again.