Refactor comment tree calculation using a PORO

This commit is contained in:
kikito
2015-10-30 17:00:32 +01:00
parent bfd89ed15a
commit 60b3c65b05
6 changed files with 42 additions and 22 deletions

View File

@@ -16,11 +16,8 @@ module CommentableActions
def show def show
set_resource_votes(resource) set_resource_votes(resource)
@commentable = resource @commentable = resource
@root_comments = resource.comments.roots.send("sort_by_#{@current_order}").page(params[:page]).per(10).for_render @comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
@comments = @root_comments.inject([]){|all, root| all + Comment.descendants_of(root).for_render} set_comment_flags(@comment_tree.comments)
@all_visible_comments = @root_comments + @comments
set_comment_flags(@all_visible_comments)
set_resource_instance set_resource_instance
end end

View File

@@ -12,14 +12,9 @@ module CommentsHelper
parent_id.blank? ? dom_id(commentable) : "comment_#{parent_id}" parent_id.blank? ? dom_id(commentable) : "comment_#{parent_id}"
end end
def select_children(comments, parent) def child_comments_of(parent)
return [] if comments.blank? return [] unless @comment_tree
comments.select{|c| c.parent_id == parent.id} @comment_tree.children_of(parent)
end
def count_children(comments, parent)
return 0 if comments.blank?
comments.count{ |c| c.parent_id == parent.id }
end end
def user_level_class(comment) def user_level_class(comment)

View File

@@ -3,7 +3,7 @@
<div id="<%= dom_id(comment) %>" class="comment small-12 column"> <div id="<%= dom_id(comment) %>" class="comment small-12 column">
<% if comment.hidden? || comment.user.hidden? %> <% if comment.hidden? || comment.user.hidden? %>
<% if count_children(@comments, comment) > 0 %> <% if child_comments_of(comment).size > 0 %>
<div class="is-deleted"> <div class="is-deleted">
<p><%= t("comments.comment.deleted") %></p> <p><%= t("comments.comment.deleted") %></p>
</div> </div>
@@ -73,7 +73,7 @@
</span> </span>
<div class="reply"> <div class="reply">
<%= t("comments.comment.responses", count: count_children(@comments, comment)) %> <%= t("comments.comment.responses", count: child_comments_of(comment).size) %>
<% if user_signed_in? %> <% if user_signed_in? %>
<span class="divider">&nbsp;|&nbsp;</span> <span class="divider">&nbsp;|&nbsp;</span>
@@ -88,7 +88,7 @@
</div> </div>
<% end %> <% end %>
<div class="comment-children"> <div class="comment-children">
<% select_children(@comments, comment).each do |child| %> <% child_comments_of(comment).each do |child| %>
<%= render 'comments/comment', comment: child %> <%= render 'comments/comment', comment: child %>
<% end %> <% end %>
</div> </div>

View File

@@ -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 %>
<section class="row-full comments"> <section class="row-full comments">
<div class="row"> <div class="row">
<div id="comments" class="small-12 column"> <div id="comments" class="small-12 column">
@@ -21,10 +21,10 @@
</div> </div>
<% end %> <% end %>
<% @root_comments.each do |comment| %> <% @comment_tree.root_comments.each do |comment| %>
<%= render 'comments/comment', comment: comment %> <%= render 'comments/comment', comment: comment %>
<% end %> <% end %>
<%= paginate @root_comments %> <%= paginate @comment_tree.root_comments %>
</div> </div>
</div> </div>
</section> </section>

View File

@@ -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 %>
<section class="row-full comments"> <section class="row-full comments">
<div class="row"> <div class="row">
<div id="comments" class="small-12 column"> <div id="comments" class="small-12 column">
@@ -22,10 +22,10 @@
</div> </div>
<% end %> <% end %>
<% @root_comments.each do |comment| %> <% @comment_tree.root_comments.each do |comment| %>
<%= render 'comments/comment', comment: comment %> <%= render 'comments/comment', comment: comment %>
<% end %> <% end %>
<%= paginate @root_comments %> <%= paginate @comment_tree.root_comments %>
</div> </div>
</div> </div>
</section> </section>

28
lib/comment_tree.rb Normal file
View File

@@ -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