Show previous comments when creating a comment.

- Move "show previous comments" into a partial
 - invoke said partial from the new comment form
 - add tests.
This commit is contained in:
Miles Gould
2013-04-29 21:30:26 +01:00
parent c6f34d387a
commit 2b84fa8bad
7 changed files with 41 additions and 16 deletions

View File

@@ -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 }

View File

@@ -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?

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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