diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 53e453410..ed90a6213 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -29,6 +29,7 @@ class CommentsController < ApplicationController @post = Post.find_by_id(params[:post_id]) if @post + @comments = @post.comments respond_to do |format| format.html # new.html.erb format.json { render json: @comment } diff --git a/app/views/comments/_form.html.haml b/app/views/comments/_form.html.haml index e023bc968..06b6d0f8b 100644 --- a/app/views/comments/_form.html.haml +++ b/app/views/comments/_form.html.haml @@ -1,5 +1,7 @@ = render :partial => "posts/single", :locals => { :post => @post || @comment.post, :subject => true } += render :partial => "posts/comments" + %h2 Your comment = form_for @comment do |f| - if @comment.errors.any? diff --git a/app/views/posts/_comments.html.haml b/app/views/posts/_comments.html.haml new file mode 100644 index 000000000..f68ed96c0 --- /dev/null +++ b/app/views/posts/_comments.html.haml @@ -0,0 +1,11 @@ +%a{:name => "comments"} +- if !@comments.empty? + %h2 + =pluralize(@comments.length, "comment") + - @comments.each do |c| + = render :partial => "comments/single", :locals => { :comment => c } + +- else + %h2 There are no comments yet + + diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index 532b93b15..607e9f1f7 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -10,16 +10,7 @@ = link_to 'Delete Post', @post, method: :delete, | data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini' - -%a{:name => "comments"} -- if @comments - %h2 - =pluralize(@comments.length, "comment") - - @comments.each do |c| - = render :partial => "comments/single", :locals => { :comment => c } - -- else - %h2 There are no comments yet += render :partial => "comments", :locals => { :post => @post } - if can? :create, Comment .post-actions diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index a3b085553..02cc2960e 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -38,6 +38,13 @@ describe CommentsController do assigns(:post).should eq(post) end + it "assigns the old comments as @comments" do + post = FactoryGirl.create(:post) + old_comment = FactoryGirl.create(:comment, :post => post) + get :new, {:post_id => post.id} + assigns(:comments).should eq [old_comment] + end + it "dies if no post specified" do get :new response.should redirect_to(root_url) diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb index 404ac5313..b1a88a7a6 100644 --- a/spec/factories/comments.rb +++ b/spec/factories/comments.rb @@ -2,6 +2,7 @@ FactoryGirl.define do factory :comment do post author - body "OMG LOL" + sequence(:body) { |n| "OMG LOL #{n}" } # because our commenters are more + # polite than YouTube's end end diff --git a/spec/views/comments/new.html.haml_spec.rb b/spec/views/comments/new.html.haml_spec.rb index 1fe66ef5f..90b5e1cb3 100644 --- a/spec/views/comments/new.html.haml_spec.rb +++ b/spec/views/comments/new.html.haml_spec.rb @@ -3,20 +3,32 @@ require 'spec_helper' describe "comments/new" do before(:each) do controller.stub(:current_user) { nil } - assign(:comment, FactoryGirl.create(:comment)) + @post = FactoryGirl.create(:post) + @previous_comment = FactoryGirl.create(:comment, :post => @post) + assign(:comment, FactoryGirl.create(:comment, :post => @post)) + assign(:comments, [@previous_comment]) + render + end + + it "shows the text of the post under discussion" do + rendered.should contain @post.body + end + + it "shows previous comments" do + rendered.should contain @previous_comment.body + end + + it "shows the correct comment count" do + rendered.should contain "1 comment" end it "renders new comment form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "form", :action => comments_path, :method => "post" do assert_select "textarea#comment_body", :name => "comment[body]" end end it 'shows markdown help' do - render rendered.should contain 'Markdown' end