Permalink
Browse files

Add article feature to Article#show. Implements #6 and fixes #17

  • Loading branch information...
1 parent db6ccb0 commit 299641ede2af861d95504ca3c2c2a38aebd51f48 @terenceponce committed Apr 18, 2012
@@ -1,8 +1,20 @@
class CommentsController < ApplicationController
- before_filter :build_comment
- load_and_authorize_resource
+ before_filter :authenticate_user!, :only => [:create]
+
+ def index
+ @commentable = find_commentable
+ @comments = @commentable.comments
+
+ if @commentable.respond_to? :title
+ @title = @commentable.title
+ end
+ end
def create
+ @commentable = find_commentable
+ @comment = @commentable.comments.build(params[:comment])
+ unauthorized! if cannot? :create, @comment
+
@comment.user = current_user
if @comment.save
flash[:success] = "Successfully submitted comment!"
@@ -15,11 +27,6 @@ def create
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$/
View
@@ -5,4 +5,5 @@ class Comment < ActiveRecord::Base
belongs_to :user
validates :content, :presence => true
+ validates :user, :presence => true
end
@@ -29,7 +29,7 @@
</ul>
<%= will_paginate @comments %>
<% end %>
- <% if can? :create, @comment %>
+ <% if user_signed_in? %>
<%= simple_form_for([@article, @comment]) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= f.input :content, :label => 'Comment' %>
@@ -40,5 +40,7 @@
:class => 'btn btn-large btn-primary' %>
</div>
<% end %>
+ <% else %>
+ <p>You must be <%= link_to 'logged in', new_user_session_path %> to comment</p>
<% end %>
</div>
@@ -0,0 +1,8 @@
+<%= simple_form_for([@commentable, Comment.new]) do |f| %>
+ <%= f.input :content, :label => 'Comment' %>
+
+ <div class="form-actions">
+ <%= f.button :submit,
+ :class => 'btn btn-large btn-primary' %>
+ </div>
+<% end %>
@@ -0,0 +1,13 @@
+<% provide(:title, "Comments on #{@title}") %>
+
+<section id="info">
+ <%= link_to @title, @commentable %>
+</section>
+
+<ul id="comments">
+ <%= render @comments %>
+</ul>
+
+<% if can? :create, Comment %>
+ <%= render 'form' %>
+<% end %>
@@ -0,0 +1,10 @@
+<%= simple_form_for() do |f| %>
+ <%= render 'shared/error_messages', :object => f.object %>
+ <%= f.input :content %>
+
+ <div class="form-actions">
+ <%= f.button :submit,
+ 'Submit comment',
+ :class => 'btn btn-large btn-primary' %>
+ </div>
+<% end %>
View
@@ -10,7 +10,7 @@
end
resources :articles do
- resources :comments
+ resources :comments, :except => [:new]
end
resources :categories
@@ -9,7 +9,7 @@
subject { @comment }
it { should respond_to(:content) }
- it { should respond_to(:user_id) }
+ it { should respond_to(:user) }
it { should respond_to(:commentable) }
it { should be_valid }
@@ -19,6 +19,11 @@
it { should_not be_valid }
end
+ describe 'with no user' do
+ before { @comment.user = nil }
+ it { should_not be_valid }
+ end
+
describe 'accessible attributes' do
it 'should not allow access to user_id' do
expect do
@@ -32,6 +32,7 @@
describe 'comments section' do
it { should have_content 'Comments' }
+ it { should have_content 'You must be logged in to comment' }
it { should_not have_button 'Submit comment' }
end
end
@@ -94,6 +95,7 @@
describe 'comments section' do
it { should have_content 'Comments' }
+ it { should_not have_content 'You must be logged in to comment' }
describe 'on posting comments' do
before do
@@ -134,14 +134,26 @@
describe 'when submitting to the destroy action' do
before do
-
@category = Fabricate(:category)
delete category_path(@category)
end
specify { response.should redirect_to(root_path) }
end
end
+
+ describe 'in the Comments controller' do
+
+ describe 'when submitting to the create action' do
+
+ before do
+ @article = Fabricate(:article)
+ post article_comments_path @article
+ end
+
+ specify { response.should redirect_to(new_user_session_path) }
+ end
+ end
end
end
end

0 comments on commit 299641e

Please sign in to comment.