comments: minimize sql queries.

Certain parts of comment rendering triggered sql queries that we didn't
really need to do. Rework things to avoid this.

* Preload comment creators in order to display commenter names with link_to_user.

* Preload comment votes in order to display "undo vote" links. Only preload
  votes for members since anonymous users can't vote and don't have "undo
  vote" links.

* Rework various conditionals to do the filtering in Ruby so that we
  avoid issuing any extra queries in sql.

* Avoid issuing any queries at all when the post doesn't have any
  comments (when last_commented_at is blank).
This commit is contained in:
evazion
2019-08-20 20:53:40 -05:00
parent 2cf929ad6e
commit b283281e5e
9 changed files with 30 additions and 60 deletions

View File

@@ -252,26 +252,6 @@ class CommentTest < ActiveSupport::TestCase
end
end
context "that is below the score threshold" do
should "be hidden if not stickied" do
user = FactoryBot.create(:user, :comment_threshold => 0)
post = FactoryBot.create(:post)
comment = FactoryBot.create(:comment, :post => post, :score => -5)
assert_equal([comment], post.comments.hidden(user))
assert_equal([], post.comments.visible(user))
end
should "be visible if stickied" do
user = FactoryBot.create(:user, :comment_threshold => 0)
post = FactoryBot.create(:post)
comment = FactoryBot.create(:comment, :post => post, :score => -5, :is_sticky => true)
assert_equal([], post.comments.hidden(user))
assert_equal([comment], post.comments.visible(user))
end
end
context "that is quoted" do
should "strip [quote] tags correctly" do
comment = FactoryBot.create(:comment, body: <<-EOS.strip_heredoc)