diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index 525586f18..9eb27226b 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -16,11 +16,8 @@ module CommentableActions def show set_resource_votes(resource) @commentable = resource - @root_comments = resource.comments.roots.send("sort_by_#{@current_order}").page(params[:page]).per(10).for_render - @comments = @root_comments.inject([]){|all, root| all + Comment.descendants_of(root).for_render} - @all_visible_comments = @root_comments + @comments - - set_comment_flags(@all_visible_comments) + @comment_tree = CommentTree.new(@commentable, params[:page], @current_order) + set_comment_flags(@comment_tree.comments) set_resource_instance end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 7fef232c5..9ac29e4da 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -12,14 +12,9 @@ module CommentsHelper parent_id.blank? ? dom_id(commentable) : "comment_#{parent_id}" end - def select_children(comments, parent) - return [] if comments.blank? - comments.select{|c| c.parent_id == parent.id} - end - - def count_children(comments, parent) - return 0 if comments.blank? - comments.count{ |c| c.parent_id == parent.id } + def child_comments_of(parent) + return [] unless @comment_tree + @comment_tree.children_of(parent) end def user_level_class(comment) diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index ac54238a0..1ec571633 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -3,7 +3,7 @@
<% if comment.hidden? || comment.user.hidden? %> - <% if count_children(@comments, comment) > 0 %> + <% if child_comments_of(comment).size > 0 %>

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

@@ -73,7 +73,7 @@
- <%= t("comments.comment.responses", count: count_children(@comments, comment)) %> + <%= t("comments.comment.responses", count: child_comments_of(comment).size) %> <% if user_signed_in? %>  |  @@ -88,7 +88,7 @@
<% end %>
- <% select_children(@comments, comment).each do |child| %> + <% child_comments_of(comment).each do |child| %> <%= render 'comments/comment', comment: child %> <% end %>
diff --git a/app/views/debates/_comments.html.erb b/app/views/debates/_comments.html.erb index 73405705a..538847a62 100644 --- a/app/views/debates/_comments.html.erb +++ b/app/views/debates/_comments.html.erb @@ -1,4 +1,4 @@ -<% cache [locale_and_user_status, @current_order, commentable_cache_key(@debate), @all_visible_comments, @all_visible_comments.map(&:author), @debate.comments_count, @comment_flags] do %> +<% cache [locale_and_user_status, @current_order, commentable_cache_key(@debate), @comment_tree.comments, @comment_tree.comment_authors, @debate.comments_count, @comment_flags] do %>
@@ -21,10 +21,10 @@
<% end %> - <% @root_comments.each do |comment| %> + <% @comment_tree.root_comments.each do |comment| %> <%= render 'comments/comment', comment: comment %> <% end %> - <%= paginate @root_comments %> + <%= paginate @comment_tree.root_comments %>
diff --git a/app/views/proposals/_comments.html.erb b/app/views/proposals/_comments.html.erb index 47713c8d5..eb54f9796 100644 --- a/app/views/proposals/_comments.html.erb +++ b/app/views/proposals/_comments.html.erb @@ -1,4 +1,4 @@ -<% cache [locale_and_user_status, commentable_cache_key(@proposal), @current_order, @all_visible_comments, @all_visible_comments.map(&:author), @proposal.comments_count, @comment_flags] do %> +<% cache [locale_and_user_status, @current_order, commentable_cache_key(@proposal), @comment_tree.comments, @comment_tree.comment_authors, @proposal.comments_count, @comment_flags] do %>
@@ -22,10 +22,10 @@
<% end %> - <% @root_comments.each do |comment| %> + <% @comment_tree.root_comments.each do |comment| %> <%= render 'comments/comment', comment: comment %> <% end %> - <%= paginate @root_comments %> + <%= paginate @comment_tree.root_comments %>
diff --git a/lib/comment_tree.rb b/lib/comment_tree.rb new file mode 100644 index 000000000..818a02006 --- /dev/null +++ b/lib/comment_tree.rb @@ -0,0 +1,28 @@ +class CommentTree + + ROOT_COMMENTS_PER_PAGE = 10 + + attr_accessor :root_comments, :comments + + def initialize(commentable, page, order = 'confidence_score') + @root_comments = commentable.comments.roots.send("sort_by_#{order}").page(page).per(ROOT_COMMENTS_PER_PAGE).for_render + + root_descendants = @root_comments.each_with_object([]) do |root, col| + col += Comment.descendants_of(root).send("sort_by_#{order}").for_render.to_a + end + + @comments = root_comments + root_descendants + + @comments_by_parent_id = @comments.each_with_object({}) do |comment, col| + (col[comment.parent_id] ||= []) << comment + end + end + + def children_of(parent) + @comments_by_parent_id[parent.id] || [] + end + + def comment_authors + comments.map(&:author) + end +end