Merge pull request #119 from pozorvlak/commentcount

Counts of comments and links in posts/_single
This commit is contained in:
Skud
2013-03-05 02:04:39 -08:00
8 changed files with 172 additions and 12 deletions

View File

@@ -32,3 +32,6 @@
body {
padding-bottom: @navbarHeight + 10px;
}
ul.inline > li.first {
padding-left: 0px;
}

View File

@@ -20,4 +20,16 @@
.post-body
:markdown
#{ strip_tags post.body }
- unless defined?(hide_comments)
.post-comments
%ul.inline
%li.first= link_to pluralize(post.comments.count, "comment"),
post_path(post, :anchor => 'comments')
-if can? :create, Comment
%li= link_to "Reply", new_comment_path(:post_id => post.id)
%li= link_to "Permalink", post
-if can? :edit, post
%li= link_to "Edit", edit_post_path(post)

View File

@@ -1,6 +1,6 @@
= content_for :title, @post.subject
= render :partial => "single", :locals => { :post => @post, :subject => false }
= render :partial => "single", :locals => { :post => @post, :subject => false, :hide_comments => true }
- if can? :edit, @post or can? :destroy, @post
.post-actions
@@ -11,11 +11,10 @@
data: { confirm: 'Are you sure?' }, :class => 'btn'
%a{:name => "comments"}
- if @post.comments.length > 0
%h2
=@post.comments.length
=@post.comments.length == 1 ? 'comment' : 'comments'
=pluralize(@post.comments.length, "comment")
- @post.comments.each do |c|
= render :partial => "comments/single", :locals => { :comment => c }

View File

@@ -2,6 +2,7 @@ require 'spec_helper'
describe "comments/edit" do
before(:each) do
controller.stub(:current_user) { nil }
assign(:comment, FactoryGirl.create(:comment))
end

View File

@@ -2,6 +2,7 @@ require 'spec_helper'
describe "comments/new" do
before(:each) do
controller.stub(:current_user) { nil }
assign(:comment, FactoryGirl.create(:comment))
end

View File

@@ -0,0 +1,120 @@
require 'spec_helper'
describe "posts/_single" do
def render_post()
render :partial => "single", :locals => { :post => @post }
end
before(:each) do
@post = FactoryGirl.create(:post)
controller.stub(:current_user) { nil }
end
context "when the number of comments doesn't matter" do
before(:each) do
render_post
end
it "contains a permanent link to post" do
assert_select "a[href=#{post_path @post}]", "Permalink"
end
it "doesn't contain a link to new comment" do
assert_select "a[href=#{new_comment_path(:post_id => @post.id)}]", false
end
end
context "when logged in" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
controller.stub(:current_user) { @member }
render_post
end
it "contains link to new comment" do
assert_select "a[href=#{new_comment_path(:post_id => @post.id)}]", "Reply"
end
it "does not contain an edit link" do
assert_select "a[href=#{edit_post_path(@post)}]", false
end
end
context "when logged in as post author" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
controller.stub(:current_user) { @member }
@post = FactoryGirl.create(:post, :author => @member)
render_post
end
it "contains an edit link" do
assert_select "a[href=#{edit_post_path(@post)}]", "Edit"
end
end
context "when there are no comments" do
before(:each) do
render_post
end
it "renders the number of comments" do
assert_select "a[href=#{post_path(@post)}\#comments]", "0 comments"
end
end
context "when there is 1 comment" do
before(:each) do
@comment = FactoryGirl.create(:comment, :post => @post)
render_post
end
it "renders the number of comments" do
assert_select "a[href=#{post_path(@post)}\#comments]", "1 comment"
end
end
context "when there are 2 comments" do
before(:each) do
@comment = FactoryGirl.create(:comment, :post => @post)
@comment2 = FactoryGirl.create(:comment, :post => @post)
render_post
end
it "renders the number of comments" do
assert_select "a[href=#{post_path(@post)}\#comments]", "2 comments"
end
end
context "when comments should be hidden" do
before(:each) do
@member = FactoryGirl.create(:member)
sign_in @member
controller.stub(:current_user) { @member }
@comment = FactoryGirl.create(:comment, :post => @post)
render :partial => "single", :locals => {
:post => @post, :hide_comments => true
}
end
it "renders no value of comments" do
rendered.should_not contain "1 comment"
end
it "does not contain link to post" do
assert_select "a[href=#{post_path @post}]", false
end
it "does not contain link to new comment" do
assert_select "a[href=#{new_comment_path(:post_id => @post.id)}]", false
end
end
end

View File

@@ -4,8 +4,9 @@ describe "posts/index" do
before(:each) do
controller.stub(:current_user) { nil }
@author = FactoryGirl.create(:member)
@post1 = FactoryGirl.build(:post, :author => @author)
@post2 = FactoryGirl.build(:post, :author => @author)
# We use create (= build+save) to generate slugs and hence paths for posts
@post1 = FactoryGirl.create(:post, :author => @author)
@post2 = FactoryGirl.create(:post, :author => @author)
assign(:posts, [@post1, @post2])
render
end

View File

@@ -34,12 +34,32 @@ describe "posts/show" do
rendered.should_not match(/a href="http:\/\/evil.com"/)
end
it "shows comments" do
it 'has an anchor to the comments' do
@post = assign(:post,
FactoryGirl.create(:html_post, :author => @author))
@comment = FactoryGirl.create(:comment, :post => @post)
FactoryGirl.create(:post, :author => @author))
render
rendered.should contain @comment.body
assert_select 'a[name=comments]'
end
context "when there is one comment" do
before(:each) do
@post = assign(:post,
FactoryGirl.create(:html_post, :author => @author))
@comment = FactoryGirl.create(:comment, :post => @post)
render
end
it 'shows comment count only 1' do
assert_select "div.post_comments", false
end
it "shows comments" do
rendered.should contain @comment.body
end
it 'has an anchor to the comments' do
assert_select 'a[name=comments]'
end
end
context "forum post" do
@@ -50,7 +70,7 @@ describe "posts/show" do
rendered.should contain "in #{@post.forum.name}"
end
end
context "signed in" do
before(:each) do
sign_in @author
@@ -62,8 +82,11 @@ describe "posts/show" do
it 'shows a comment button' do
assert_select "a[href=#{new_comment_path(:post_id => @post.id)}]", "Comment"
end
end
end
end