Commit ea20a3dc19a595f36b9c153b6a897485a792b53f

Authored by Rodrigo Souto
2 parents 1c3dc68d 951fcb57

Merge branch 'fix-article-comments-ordering' into 'master'

Fix article comments ordering

Fix article's comments ordering bug (had wrong logic before correction) and pagination missing bug (js was overwriting pagination after ajax request).

References:

https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/issues/630
https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/issues/632

See merge request !792
app/controllers/public/content_viewer_controller.rb
... ... @@ -64,11 +64,7 @@ class ContentViewerController < ApplicationController
64 64 process_comments(params)
65 65  
66 66 if request.xhr? and params[:comment_order]
67   - if @comment_order == 'newest'
68   - @comments = @comments.reverse
69   - end
70   -
71   - return render :partial => 'comment/comment', :collection => @comments
  67 + return render :partial => 'comment/comments_with_pagination'
72 68 end
73 69  
74 70 if params[:slideshow]
... ... @@ -275,8 +271,12 @@ class ContentViewerController < ApplicationController
275 271 @comments = @page.comments.without_spam
276 272 @comments = @plugins.filter(:unavailable_comments, @comments)
277 273 @comments_count = @comments.count
278   - @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] )
279 274 @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order]
  275 + @comments = @comments.without_reply
  276 + if @comment_order == 'newest'
  277 + @comments = @comments.reverse
  278 + end
  279 + @comments = @comments.paginate(:per_page => per_page, :page => params[:comment_page] )
280 280 end
281 281  
282 282 private
... ...
app/views/comment/_comments_with_pagination.html.erb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +<% if @comments.present? %>
  2 + <%= render :partial => 'comment/comment', :collection => @comments %>
  3 + <%= pagination_links @comments, :param_name => 'comment_page', :params => { :comment_order => @comment_order } %>
  4 +<% end %>
... ...
app/views/content_viewer/view_page.html.erb
... ... @@ -81,7 +81,7 @@
81 81 <ul class="article-comments-list">
82 82 <% if @comments.present? %>
83 83 <%= render :partial => 'comment/comment', :collection => @comments %>
84   - <%= pagination_links @comments, :param_name => 'comment_page' %>
  84 + <%= pagination_links @comments, :param_name => 'comment_page', :params => { :comment_order => @comment_order } %>
85 85 <% end %>
86 86 </ul>
87 87  
... ...
public/javascripts/comment_order.js
... ... @@ -10,10 +10,9 @@ function send_order(order, url) {
10 10 });
11 11 }
12 12  
13   -
14 13 jQuery(document).ready(function(){
15 14 jQuery("#comment_order").change(function(){
16   - var url = jQuery("#page_url").val();
  15 + var url = window.location.href;
17 16 send_order(this.value, url);
18 17 });
19   -});
20 18 \ No newline at end of file
  19 +});
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -328,6 +328,27 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
328 328 assert_tag :content => /list my comment/
329 329 end
330 330  
  331 + should 'order comments according to comments ordering option' do
  332 + article = fast_create(Article, :profile_id => profile.id)
  333 + for n in 1..24
  334 + article.comments.create!(:author => profile, :title => "some title #{n}", :body => "some body #{n}")
  335 + end
  336 +
  337 + get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
  338 +
  339 + for i in 1..12
  340 + assert_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i}" }
  341 + assert_no_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i + 12}" }
  342 + end
  343 +
  344 + xhr :get, :view_page, :profile => profile.identifier, :page => article.path.split('/'), :comment_page => 1, :comment_order => 'newest'
  345 +
  346 + for i in 1..12
  347 + assert_no_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i}" }
  348 + assert_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i + 12}" }
  349 + end
  350 + end
  351 +
331 352 should 'redirect to new article path under an old path' do
332 353 p = create_user('test_user').person
333 354 a = p.articles.create(:name => 'old-name')
... ... @@ -1351,7 +1372,7 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
1351 1372 assert_equal 15, article.comments.count
1352 1373  
1353 1374 get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
1354   - assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{article.path}?comment_page=2", :rel => 'next' }
  1375 + assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{article.path}?comment_order=oldest&amp;comment_page=2", :rel => 'next' }
1355 1376 end
1356 1377  
1357 1378 should 'not escape acceptable HTML in list of blog posts' do
... ...