Commit 951fcb57484a69911997572a2bc6d52b27ccbc6b

Authored by Eduardo Vital
1 parent baa2faf8

Fix article comments ordering bug

Signed-off-by: Eduardo Vital <vitaldu@gmail.com>
Signed-off-by: Pedro de Lyra <pedrodelyra@gmail.com>
app/controllers/public/content_viewer_controller.rb
... ... @@ -64,11 +64,7 @@ class ContentViewerController &lt; 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]
... ... @@ -272,8 +268,12 @@ class ContentViewerController &lt; ApplicationController
272 268 @comments = @page.comments.without_spam
273 269 @comments = @plugins.filter(:unavailable_comments, @comments)
274 270 @comments_count = @comments.count
275   - @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] )
276 271 @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order]
  272 + @comments = @comments.without_reply
  273 + if @comment_order == 'newest'
  274 + @comments = @comments.reverse
  275 + end
  276 + @comments = @comments.paginate(:per_page => per_page, :page => params[:comment_page] )
277 277 end
278 278  
279 279 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
... ... @@ -329,6 +329,27 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
329 329 assert_tag :content => /list my comment/
330 330 end
331 331  
  332 + should 'order comments according to comments ordering option' do
  333 + article = fast_create(Article, :profile_id => profile.id)
  334 + for n in 1..24
  335 + article.comments.create!(:author => profile, :title => "some title #{n}", :body => "some body #{n}")
  336 + end
  337 +
  338 + get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
  339 +
  340 + for i in 1..12
  341 + assert_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i}" }
  342 + assert_no_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i + 12}" }
  343 + end
  344 +
  345 + xhr :get, :view_page, :profile => profile.identifier, :page => article.path.split('/'), :comment_page => 1, :comment_order => 'newest'
  346 +
  347 + for i in 1..12
  348 + assert_no_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i}" }
  349 + assert_tag :tag => 'div', :attributes => { :class => 'comment-details' }, :descendant => { :tag => 'h4', :content => "some title #{i + 12}" }
  350 + end
  351 + end
  352 +
332 353 should 'redirect to new article path under an old path' do
333 354 p = create_user('test_user').person
334 355 a = p.articles.create(:name => 'old-name')
... ... @@ -1364,7 +1385,7 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
1364 1385 assert_equal 15, article.comments.count
1365 1386  
1366 1387 get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
1367   - assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{article.path}?comment_page=2", :rel => 'next' }
  1388 + assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{article.path}?comment_order=oldest&amp;comment_page=2", :rel => 'next' }
1368 1389 end
1369 1390  
1370 1391 should 'not escape acceptable HTML in list of blog posts' do
... ...