diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 4ac83c1cc..4aa343a35 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -11,8 +11,8 @@ module CommentsHelper comment.deleted ? 0 : comment.user_id end - def tree_for(comment, commentable) - nested_comments(tree: comment.subtree.includes(:user).arrange, commentable: commentable, is_view_root: true) + def tree_for(comment, sub_comments, commentable) + nested_comments(tree: { comment => sub_comments }, commentable: commentable, is_view_root: true) end private diff --git a/app/models/comment.rb b/app/models/comment.rb index 1e5f6fa3d..961fd7e0e 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -128,6 +128,10 @@ class Comment < ApplicationRecord commentable_type: commentable_type) end + def self.tree_for(commentable, limit = 0) + commentable.comments.includes(:user).arrange(order: "score DESC").to_a[0..limit - 1].to_h + end + def path "/#{user.username}/comment/#{id_code_generated}" rescue StandardError diff --git a/app/views/articles/_full_comment_area.html.erb b/app/views/articles/_full_comment_area.html.erb index 7c745532c..975f27286 100644 --- a/app/views/articles/_full_comment_area.html.erb +++ b/app/views/articles/_full_comment_area.html.erb @@ -1,6 +1,6 @@ -<% cache("whole-comment-area-#{@article.id}-#{@article.last_comment_at}-#{@article.show_comments}", :expires_in => 2.hours) do %> +<% cache("whole-comment-area-#{@article.id}-#{@article.last_comment_at}-#{@article.show_comments}", expires_in: 2.hours) do %>
- <% if @article.show_comments%> + <% if @article.show_comments %>
- <% if @article.comments_count > 0 %> - <% Comment.rooted_on(@article.id, "Article").order("score DESC").limit(@comments_to_show_count).each_with_index do |comment, i| %> + <% if @article.comments_count > 0 %> + <% Comment.tree_for(@article, @comments_to_show_count).each do |comment, sub_comments| %> <% cache ["comment_root_cached_tree", comment] do %> - <%= tree_for(comment, @article) %> + <%= tree_for(comment, sub_comments, @article) %> <% end %> <% end %> <% end %>
<% end %> diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb index 9fc6eb81d..036ca93da 100644 --- a/app/views/comments/index.html.erb +++ b/app/views/comments/index.html.erb @@ -1,31 +1,31 @@ <% if @root_comment.present? %> <% title truncate(strip_tags(@root_comment.processed_html), length: 100) + " — DEV" %> <% else %> - <% title "Discussion of "+ @commentable.title + " — DEV" %> + <% title "Discussion of " + @commentable.title + " — DEV" %> <% end %> <%= content_for :page_meta do %> - "> + "> - - " /> + + " /> - - - "> + + + "> <% if @root_comment.present? %> - - - - + + + + <% else %> - - - - + + + + <% end %> <% if @commentable.class.name == "Article" %> @@ -33,66 +33,65 @@ <% end %> <% end %> - <% if @root_comment %> -
- <% else %> -
- - <% if @commentable.main_image.present? %> -
- <% else %> -
- <% end %> -

<%= @commentable.title %>

-

- <%= @commentable.user.name %> - <%= "on "+@commentable.published_at.strftime("%B %d, %Y") if @commentable.published_at %> -

-
- <% if @commentable.processed_html.present? %> - <% if @commentable.processed_html.size < 350 %> -
- <%= sanitize_rendered_markdown(@commentable.processed_html.html_safe) %> -
- <% else %> -
- <%= truncate(strip_tags(@commentable.processed_html), length:150).html_safe %> [Read Full] -
- <% end %> +<% if @root_comment %> +
+<% else %> +
+ + <% if @commentable.main_image.present? %> +
+ <% else %> +
<% end %> -
- <% end %> +

<%= @commentable.title %>

+

+ <%= @commentable.user.name %> + <%= "on " + @commentable.published_at.strftime("%B %d, %Y") if @commentable.published_at %> +

+
+ <% if @commentable.processed_html.present? %> + <% if @commentable.processed_html.size < 350 %> +
+ <%= sanitize_rendered_markdown(@commentable.processed_html.html_safe) %> +
+ <% else %> +
+ <%= truncate(strip_tags(@commentable.processed_html), length: 150).html_safe %> [Read Full] +
+ <% end %> + <% end %> +
+<% end %>
<% if @root_comment %>
-

re: <%= @commentable.title %> VIEW POST

+

re: <%= @commentable.title %> VIEW POST

<% unless @root_comment.is_root? %> VIEW PARENT COMMENT <% end %> - VIEW FULL DISCUSSION + VIEW FULL DISCUSSION
<% else %> <%= render "form", commentable: @commentable, - commentable_type: "Article" - %> + commentable_type: "Article" %> <% end %>
<% if @root_comment.present? %>
<% cache ["comment_root-view-root_#{user_signed_in?}", @root_comment] do %> - <%= tree_for(@root_comment, @commentable) %> + <%= tree_for(@root_comment, @root_comment.subtree.arrange[@root_comment], @commentable) %> <% end %>
<% else %> - <% Comment.rooted_on(@commentable.id, @commentable_type).order("score DESC").each do |comment| %> + <% Comment.tree_for(@commentable).each do |comment, sub_comments| %> <% cache ["comment_root_#{user_signed_in?}", comment] do %> - <%= tree_for(comment, @commentable) %> + <%= tree_for(comment, sub_comments, @commentable) %> <% end %> <% end %> <% end %> @@ -102,7 +101,7 @@
<% if has_vid?(@commentable) %> - <%= render 'articles/fitvids' %> + <%= render "articles/fitvids" %> <% end %>