From 951fcb57484a69911997572a2bc6d52b27ccbc6b Mon Sep 17 00:00:00 2001 From: Eduardo Vital Date: Tue, 16 Feb 2016 11:55:38 -0200 Subject: [PATCH] Fix article comments ordering bug --- app/controllers/public/content_viewer_controller.rb | 12 ++++++------ app/views/comment/_comments_with_pagination.html.erb | 4 ++++ app/views/content_viewer/view_page.html.erb | 2 +- public/javascripts/comment_order.js | 5 ++--- test/functional/content_viewer_controller_test.rb | 23 ++++++++++++++++++++++- 5 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 app/views/comment/_comments_with_pagination.html.erb diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index c3856a1..a937e2f 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -64,11 +64,7 @@ class ContentViewerController < ApplicationController process_comments(params) if request.xhr? and params[:comment_order] - if @comment_order == 'newest' - @comments = @comments.reverse - end - - return render :partial => 'comment/comment', :collection => @comments + return render :partial => 'comment/comments_with_pagination' end if params[:slideshow] @@ -272,8 +268,12 @@ class ContentViewerController < ApplicationController @comments = @page.comments.without_spam @comments = @plugins.filter(:unavailable_comments, @comments) @comments_count = @comments.count - @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order] + @comments = @comments.without_reply + if @comment_order == 'newest' + @comments = @comments.reverse + end + @comments = @comments.paginate(:per_page => per_page, :page => params[:comment_page] ) end private diff --git a/app/views/comment/_comments_with_pagination.html.erb b/app/views/comment/_comments_with_pagination.html.erb new file mode 100644 index 0000000..aef4def --- /dev/null +++ b/app/views/comment/_comments_with_pagination.html.erb @@ -0,0 +1,4 @@ +<% if @comments.present? %> + <%= render :partial => 'comment/comment', :collection => @comments %> + <%= pagination_links @comments, :param_name => 'comment_page', :params => { :comment_order => @comment_order } %> +<% end %> diff --git a/app/views/content_viewer/view_page.html.erb b/app/views/content_viewer/view_page.html.erb index f089768..82cd4fc 100644 --- a/app/views/content_viewer/view_page.html.erb +++ b/app/views/content_viewer/view_page.html.erb @@ -81,7 +81,7 @@ diff --git a/public/javascripts/comment_order.js b/public/javascripts/comment_order.js index b47b2a8..8ee8330 100644 --- a/public/javascripts/comment_order.js +++ b/public/javascripts/comment_order.js @@ -10,10 +10,9 @@ function send_order(order, url) { }); } - jQuery(document).ready(function(){ jQuery("#comment_order").change(function(){ - var url = jQuery("#page_url").val(); + var url = window.location.href; send_order(this.value, url); }); -}); \ No newline at end of file +}); diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 0a34c08..1cbee00 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -329,6 +329,27 @@ class ContentViewerControllerTest < ActionController::TestCase assert_tag :content => /list my comment/ end + should 'order comments according to comments ordering option' do + article = fast_create(Article, :profile_id => profile.id) + for n in 1..24 + article.comments.create!(:author => profile, :title => "some title #{n}", :body => "some body #{n}") + end + + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') + + for i in 1..12 + assert_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i}" } + assert_no_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i + 12}" } + end + + xhr :get, :view_page, :profile => profile.identifier, :page => article.path.split('/'), :comment_page => 1, :comment_order => 'newest' + + for i in 1..12 + assert_no_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i}" } + assert_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i + 12}" } + end + end + should 'redirect to new article path under an old path' do p = create_user('test_user').person a = p.articles.create(:name => 'old-name') @@ -1364,7 +1385,7 @@ class ContentViewerControllerTest < ActionController::TestCase 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' } + assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{article.path}?comment_order=oldest&comment_page=2", :rel => 'next' } end should 'not escape acceptable HTML in list of blog posts' do -- libgit2 0.21.2