* Reduce the number of sql queries for articles comments * Refactor comments tree display * Refactor retrieving the comments tree * Remove comments tree view object * Update score in comments spec for consistent order
27 lines
846 B
Ruby
27 lines
846 B
Ruby
module CommentsHelper
|
|
def comment_class(comment, is_view_root = false)
|
|
if comment.root? || is_view_root
|
|
"root"
|
|
else
|
|
"child"
|
|
end
|
|
end
|
|
|
|
def comment_user_id_unless_deleted(comment)
|
|
comment.deleted ? 0 : comment.user_id
|
|
end
|
|
|
|
def tree_for(comment, sub_comments, commentable)
|
|
nested_comments(tree: { comment => sub_comments }, commentable: commentable, is_view_root: true)
|
|
end
|
|
|
|
private
|
|
|
|
def nested_comments(tree:, commentable:, is_view_root: false)
|
|
tree.map do |comment, sub_comments|
|
|
render("comments/comment", comment: comment, commentable: commentable,
|
|
is_view_root: is_view_root, is_childless: sub_comments.empty?,
|
|
subtree_html: nested_comments(tree: sub_comments, commentable: commentable))
|
|
end.join.html_safe
|
|
end
|
|
end
|