Skip to content
Browse files

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

  • Loading branch information...
1 parent 61da2e1 commit db6ccb0e65de349c8c3fa8f1b474a39fe4c83fa9 @terenceponce committed Apr 16, 2012
View
3 app/assets/javascripts/comments.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3 app/assets/stylesheets/comments.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Comments controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
2 app/controllers/articles_controller.rb
@@ -6,6 +6,8 @@ def index
end
def show
+ @comment = @article.comments.build
+ @comments = @article.comments.paginate(:page => params[:page])
end
def new
View
31 app/controllers/comments_controller.rb
@@ -0,0 +1,31 @@
+class CommentsController < ApplicationController
+ before_filter :build_comment
+ load_and_authorize_resource
+
+ def create
+ @comment.user = current_user
+ if @comment.save
+ flash[:success] = "Successfully submitted comment!"
+ redirect_to :back
+ else
+ flash[:error] = "Failed to submit comment"
+ redirect_to :back
+ end
+ end
+
+ private
+
+ def build_comment
+ @commentable = find_commentable
+ @comment = @commentable.comments.build(params[:comment])
+ end
+
+ def find_commentable
+ params.each do |name, value|
+ if name =~ /(.+)_id$/
+ return $1.classify.constantize.find(value)
+ end
+ end
+ nil
+ end
+end
View
2 app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
View
8 app/models/ability.rb
@@ -9,6 +9,14 @@ def initialize(user)
else
can :read, :all
+ can :create, Comment
+ can :update, Comment do |comment|
+ comment.try(:user) == user
+ end
+ can :destroy, Comment do |comment|
+ comment.try(:user) == user
+ end
+
if user.role?(:author)
can :create, Article
can :update, Article do |article|
View
21 app/views/articles/show.html.erb
@@ -21,3 +21,24 @@
<% if can? :destroy, @article %>
<%= link_to 'Destroy', article_path(@article), :confirm => 'Are you sure?', :method => :delete %>
<% end %>
+<h2>Comments</h2>
+<div>
+ <% if @article.comments.any? %>
+ <ul class="comments">
+ <%= render @comments %>
+ </ul>
+ <%= will_paginate @comments %>
+ <% end %>
+ <% if can? :create, @comment %>
+ <%= simple_form_for([@article, @comment]) do |f| %>
+ <%= render 'shared/error_messages', :object => f.object %>
+ <%= f.input :content, :label => 'Comment' %>
+
+ <div class="form-actions">
+ <%= f.button :submit,
+ 'Submit comment',
+ :class => 'btn btn-large btn-primary' %>
+ </div>
+ <% end %>
+ <% end %>
+</div>
View
7 app/views/comments/_comment.html.erb
@@ -0,0 +1,7 @@
+<li>
+ <span class="commenter"><%= comment.user.name %></span>
+ <span class="content"><%= comment.content %></span>
+ <span class="timestamp">
+ Posted <%= time_ago_in_words(comment.created_at) %> ago.
+ </span>
+</li>
View
4 config/routes.rb
@@ -9,7 +9,9 @@
get 'settings', :to => 'devise/registrations#edit', :as => :edit_user_registration
end
- resources :articles
+ resources :articles do
+ resources :comments
+ end
resources :categories
match '/contact', :to => 'static_pages#contact'
View
2 spec/fabricators/comment_fabricator.rb
@@ -1,6 +1,6 @@
Fabricator(:comment) do
content "This is a comment"
- user
+ user!
end
Fabricator(:article_comment, :from => :comment) do
View
59 spec/requests/articles_pages_spec.rb
@@ -11,7 +11,50 @@
before { visit articles_path }
it { should have_page_title 'Articles' }
- it { should_not have_page_heading 'Post article' }
+ it { should have_page_heading 'Articles' }
+ it { should_not have_link 'New article' }
+ it { should_not have_link 'Edit' }
+ it { should_not have_link 'Destroy' }
+ end
+
+ describe 'in the show page' do
+
+ before do
+ @article = Fabricate(:article)
+ visit article_path(@article)
+ end
+
+ it { should have_page_title @article.title }
+ it { should have_page_heading @article.title }
+ it { should have_link 'Back to articles' }
+ it { should_not have_link 'Edit' }
+ it { should_not have_link 'Destroy' }
+
+ describe 'comments section' do
+ it { should have_content 'Comments' }
+ it { should_not have_button 'Submit comment' }
+ end
+ end
+
+ describe 'in the new page' do
+
+ before { visit new_article_path }
+
+ it { should have_error_message 'Access denied' }
+ it { should have_page_title '' }
+ it { should have_page_heading 'Developers Connect' }
+ end
+
+ describe 'in the edit page' do
+
+ before do
+ @article = Fabricate(:article)
+ visit edit_article_path(@article)
+ end
+
+ it { should have_error_message 'Access denied' }
+ it { should have_page_title '' }
+ it { should have_page_heading 'Developers Connect' }
end
end
@@ -48,6 +91,20 @@
it { should have_link 'Back to articles' }
it { should_not have_link 'Edit' }
it { should_not have_link 'Destroy' }
+
+ describe 'comments section' do
+ it { should have_content 'Comments' }
+
+ describe 'on posting comments' do
+ before do
+ fill_in 'Comment', :with => 'Foobar'
+ click_button 'Submit comment'
+ end
+
+ it { should have_content 'Foobar' }
+ it { should have_content @user.name }
+ end
+ end
end
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.