mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-04-03 22:54:21 -04:00
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:
@@ -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 }
|
||||
|
||||
@@ -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?
|
||||
|
||||
11
app/views/posts/_comments.html.haml
Normal file
11
app/views/posts/_comments.html.haml
Normal 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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user