diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 6a017fb..242b2d6 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -99,9 +99,7 @@ class ContentViewerController < ApplicationController end end - comments = @page.comments.without_spam - @comments = comments.as_thread - @comments_count = comments.count + @comments = @page.comments.without_spam.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) if params[:slideshow] render :action => 'slideshow', :layout => 'slideshow' end diff --git a/app/models/comment.rb b/app/models/comment.rb index ff2c2d8..74272fb 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -17,6 +17,7 @@ class Comment < ActiveRecord::Base belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' named_scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false] + named_scope :without_reply, :conditions => ['reply_of_id IS NULL'] named_scope :spam, :conditions => ['spam = ?', true] # unauthenticated authors: @@ -152,21 +153,6 @@ class Comment < ActiveRecord::Base @replies = comments_list end - def self.as_thread - result = {} - root = [] - order(:id).each do |c| - c.replies = [] - result[c.id] ||= c - if result[c.reply_of_id] - result[c.reply_of_id].replies << c - else - root << c - end - end - root - end - include ApplicationHelper def reported_version(options = {}) comment = self diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index f7621cb..7aaca0f 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -88,18 +88,19 @@
> + <% if @page.accept_comments? || @comments.count > 0 %> +
> <%= number_of_comments(@page) %>
<% end %> - <% if @page.accept_comments? && @comments_count > 1 %> + <% if @page.accept_comments? && @comments.count > 1 %> <%= link_to(_('Post a comment'), '#', :class => 'display-comment-form', :id => 'top-post-comment-button', :onclick => "jQuery('#page-comment-form .display-comment-form').first().click();") %> <% end %><%= render :partial => 'comment/comment', :collection => @comments %> + <%= pagination_links @comments, :param_name => 'comment_page' %>
<% if @page.accept_comments? %> diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index c269993..f996833 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1084,13 +1084,13 @@ class ContentViewerControllerTest < ActionController::TestCase article.save! comment1 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello') comment1.save! - comment2 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello', :reply_of_id => comment1.id) + comment2 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello') comment2.save! get :view_page, :profile => 'testuser', :page => [ 'test' ] assert_tag :tag => 'a', :attributes => { :id => 'top-post-comment-button' } end - should 'store number of comments' do + should 'not show a post comment button on top if there are one comment and one reply' do profile = create_user('testuser').person article = profile.articles.build(:name => 'test') article.save! @@ -1099,7 +1099,7 @@ class ContentViewerControllerTest < ActionController::TestCase comment2 = article.comments.build(:author => profile, :title => 'hi', :body => 'hello', :reply_of_id => comment1.id) comment2.save! get :view_page, :profile => 'testuser', :page => [ 'test' ] - assert_equal 2, assigns(:comments_count) + assert_no_tag :tag => 'a', :attributes => { :id => 'top-post-comment-button' } end should 'suggest article link displayed into article-actions div' do @@ -1178,11 +1178,22 @@ class ContentViewerControllerTest < ActionController::TestCase should 'not display comments marked as spam' do article = fast_create(Article, :profile_id => profile.id) - ham = fast_create(Comment, :source_id => article.id, :source_type => 'Article') - spam = fast_create(Comment, :source_id => article.id, :source_type => 'Article', :spam => true) + ham = fast_create(Comment, :source_id => article.id, :source_type => 'Article', :title => 'some content') + spam = fast_create(Comment, :source_id => article.id, :source_type => 'Article', :spam => true, :title => 'this is a spam') get 'view_page', :profile => profile.identifier, :page => article.path.split('/') - assert_equal 1, assigns(:comments_count) + assert_no_tag :tag => 'h4', :content => /spam/ end + should 'display pagination links of comments' do + article = fast_create(Article, :profile_id => profile.id) + for n in 1..15 + article.comments.create!(:author => profile, :title => "some title #{n}", :body => 'some body #{n}') + end + assert_equal 15, article.comments.count + + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') + assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{article.path}?comment_page=2", :rel => 'next' } + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 531c428..fdd48f0 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -285,21 +285,6 @@ class CommentTest < ActiveSupport::TestCase assert_equal [c1,c3], c.reload.children end - should "return comments as a thread" do - a = fast_create(Article) - c0 = fast_create(Comment, :source_id => a.id) - c1 = fast_create(Comment, :reply_of_id => c0.id, :source_id => a.id) - c2 = fast_create(Comment, :reply_of_id => c1.id, :source_id => a.id) - c3 = fast_create(Comment, :reply_of_id => c0.id, :source_id => a.id) - c4 = fast_create(Comment, :source_id => a.id) - result = a.comments.as_thread - assert_equal c0.id, result[0].id - assert_equal [c1.id, c3.id], result[0].replies.map(&:id) - assert_equal [c2.id], result[0].replies[0].replies.map(&:id) - assert_equal c4.id, result[1].id - assert result[1].replies.empty? - end - should "return activities comments as a thread" do person = fast_create(Person) a = TextileArticle.create!(:profile => person, :name => 'My article', :body => 'Article body') @@ -515,15 +500,6 @@ class CommentTest < ActiveSupport::TestCase assert_equal c, SpamNotification.marked_as_ham end - should 'ignore spam when constructing threads' do - original = create_comment - response = create_comment(:reply_of_id => original.id) - original.spam! - - assert_equivalent [response], Comment.without_spam.as_thread - end - - should 'store User-Agent' do c = Comment.new(:user_agent => 'foo') assert_equal 'foo', c.user_agent @@ -700,6 +676,15 @@ class CommentTest < ActiveSupport::TestCase assert_equal c1, c1.comment_root end + should 'be able to select non-reply comments' do + c1 = fast_create(Comment) + c2 = fast_create(Comment, :reply_of_id => c1.id) + c3 = fast_create(Comment, :reply_of_id => c2.id) + c4 = fast_create(Comment) + + assert_equivalent [c1,c4], Comment.without_reply + end + private def create_comment(args = {}) -- libgit2 0.21.2