From 136c1e73ed780d43aa2ea480bf5332f575478429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 3 Sep 2015 16:32:54 +0200 Subject: [PATCH] uses ancestry to retrieve debate comments --- app/controllers/debates_controller.rb | 7 ++++--- app/helpers/comments_helper.rb | 5 +++++ app/models/comment.rb | 9 --------- app/views/comments/_comment.html.erb | 6 +++--- app/views/debates/show.html.erb | 4 ++-- spec/features/comments_spec.rb | 2 +- spec/models/comment_spec.rb | 29 --------------------------- 7 files changed, 15 insertions(+), 47 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 865eadced..a9e7325eb 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -15,9 +15,10 @@ class DebatesController < ApplicationController def show set_debate_votes(@debate) @commentable = @debate - @comments = @debate.comments.roots.recent.page(params[:page]).for_render - # TODO limit this list to the paginated root comment's children once we have ancestry - all_visible_comments = @debate.comments + @root_comments = @debate.comments.roots.recent.page(params[:page]).per(10).for_render + @comments = @root_comments.inject([]){|all, root| all + root.descendants} + + all_visible_comments = @root_comments + @comments set_comment_flags(all_visible_comments) end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 300a07921..314518e0b 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -12,4 +12,9 @@ module CommentsHelper parent_id.blank? ? dom_id(commentable) : "comment_#{parent_id}" end + def children_of_in(parent, comments) + return [] if comments.blank? + comments.select{|c| c.parent_id == parent.id} + end + end \ No newline at end of file diff --git a/app/models/comment.rb b/app/models/comment.rb index fe57ee95f..422d39f8d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,4 @@ class Comment < ActiveRecord::Base - #acts_as_nested_set scope: [:commentable_id, :commentable_type], counter_cache: :children_count acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases acts_as_votable @@ -80,14 +79,6 @@ class Comment < ActiveRecord::Base moderator_id.present? end - # TODO: faking counter cache since there is a bug with acts_as_nested_set :counter_cache - # Remove when https://github.com/collectiveidea/awesome_nested_set/issues/294 is fixed - # and reset counters using - # > Comment.find_each { |comment| Comment.reset_counters(comment.id, :children) } - def children_count - children.count - end - def after_hide commentable_type.constantize.reset_counters(commentable_id, :comments) end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index ba9e99151..37a80d830 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -2,7 +2,7 @@
<% if comment.hidden? || comment.user.hidden? %> - <% if comment.children_count > 0 %> + <% if children_of_in(comment, @comments).size > 0 %>

<%= t("debates.comment.deleted") %>

@@ -79,7 +79,7 @@
- <%= t("debates.comment.responses", count: comment.children_count) %> + <%= t("debates.comment.responses", count: children_of_in(comment, @comments).size) %> <% if user_signed_in? %>  |  @@ -95,7 +95,7 @@ <% end %>
- <%= render comment.children.for_render.reorder('id DESC') %> + <%= render children_of_in(comment, @comments) if @comments.present? %>
diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index d5bce5bef..1e29894c4 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -92,8 +92,8 @@ <% end %> - <%= render @comments %> - <%= paginate @comments %> + <%= render @root_comments %> + <%= paginate @root_comments %> diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index cb847e40e..b7a53eeb7 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -21,7 +21,7 @@ feature 'Comments' do scenario 'Paginated comments' do debate = create(:debate) - per_page = Kaminari.config.default_per_page + per_page = 10 (per_page + 2).times { create(:comment, commentable: debate)} visit debate_path(debate) diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index dbb254063..490a5cfd1 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -17,35 +17,6 @@ describe Comment do expect(debate.reload.comments_count).to eq(0) end - describe "#children_count" do - let(:comment) { create(:comment) } - let(:debate) { comment.debate } - - it "should count first level children" do - parent = comment - - 3.times do - create(:comment, commentable: debate, parent: parent) - parent = parent.children.first - end - - expect(comment.children_count).to eq(1) - expect(debate.comments.count).to eq(4) - end - - it "should increase children count" do - expect do - create(:comment, commentable: debate, parent: comment) - end.to change { comment.children_count }.from(0).to(1) - end - - it "should decrease children count" do - new_comment = create(:comment, commentable: debate, parent: comment) - - expect { new_comment.destroy }.to change { comment.children_count }.from(1).to(0) - end - end - describe "#as_administrator?" do it "should be true if comment has administrator_id, false otherway" do expect(comment).not_to be_as_administrator