diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index d87769d3e..4b9b36ad2 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -32,3 +32,6 @@ body { padding-bottom: @navbarHeight + 10px; } +ul.inline > li.first { + padding-left: 0px; +} diff --git a/app/views/posts/_single.html.haml b/app/views/posts/_single.html.haml index f07856c04..abc2fcc5f 100644 --- a/app/views/posts/_single.html.haml +++ b/app/views/posts/_single.html.haml @@ -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) + diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index 7813e8501..f74ce6d81 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -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 } diff --git a/spec/views/comments/edit.html.haml_spec.rb b/spec/views/comments/edit.html.haml_spec.rb index d872d33d4..beee78013 100644 --- a/spec/views/comments/edit.html.haml_spec.rb +++ b/spec/views/comments/edit.html.haml_spec.rb @@ -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 diff --git a/spec/views/comments/new.html.haml_spec.rb b/spec/views/comments/new.html.haml_spec.rb index beb01c9d0..59fe03d7f 100644 --- a/spec/views/comments/new.html.haml_spec.rb +++ b/spec/views/comments/new.html.haml_spec.rb @@ -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 diff --git a/spec/views/posts/_single.html.haml_spec.rb b/spec/views/posts/_single.html.haml_spec.rb new file mode 100644 index 000000000..eebac11f4 --- /dev/null +++ b/spec/views/posts/_single.html.haml_spec.rb @@ -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 + + + + diff --git a/spec/views/posts/index.html.haml_spec.rb b/spec/views/posts/index.html.haml_spec.rb index fd73a3a9f..43b24b006 100644 --- a/spec/views/posts/index.html.haml_spec.rb +++ b/spec/views/posts/index.html.haml_spec.rb @@ -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 diff --git a/spec/views/posts/show.html.haml_spec.rb b/spec/views/posts/show.html.haml_spec.rb index 4785412d5..aadf31a29 100644 --- a/spec/views/posts/show.html.haml_spec.rb +++ b/spec/views/posts/show.html.haml_spec.rb @@ -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