From 69f653b0c4092498c1f430fb0ee8d296689223cc Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 5 Feb 2020 18:05:56 -0600 Subject: [PATCH] forum_topics/show: fix N+1 query patterns. --- app/controllers/forum_topics_controller.rb | 3 ++- app/models/forum_post.rb | 8 +------- app/views/forum_post_votes/_list.html.erb | 2 +- app/views/forum_post_votes/_vote.html.erb | 2 +- app/views/forum_posts/_forum_post.html.erb | 2 +- test/unit/forum_post_test.rb | 11 ----------- 6 files changed, 6 insertions(+), 22 deletions(-) diff --git a/app/controllers/forum_topics_controller.rb b/app/controllers/forum_topics_controller.rb index fd8622aa2..92462e30c 100644 --- a/app/controllers/forum_topics_controller.rb +++ b/app/controllers/forum_topics_controller.rb @@ -36,7 +36,8 @@ class ForumTopicsController < ApplicationController @forum_topic.mark_as_read!(CurrentUser.user) end @forum_posts = ForumPost.search(:topic_id => @forum_topic.id).reorder("forum_posts.id").paginate(params[:page]) - @forum_posts = @forum_posts.reverse_order.includes(:creator).load if request.format.atom? + @forum_posts = @forum_posts.includes(:creator, :bulk_update_request) + @forum_posts = @forum_posts.reverse_order.load if request.format.atom? respond_with(@forum_topic) end diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 7e29a4298..af3f26e03 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -90,18 +90,12 @@ class ForumPost < ApplicationRecord end end - def tag_change_request - bulk_update_request || tag_alias || tag_implication - end - def reportable_by?(user) visible?(user) && creator_id != user.id && !creator.is_moderator? end def votable? - TagAlias.where(forum_post_id: id).exists? || - TagImplication.where(forum_post_id: id).exists? || - BulkUpdateRequest.where(forum_post_id: id).exists? + bulk_update_request.present? && bulk_update_request.is_pending? end def voted?(user, score) diff --git a/app/views/forum_post_votes/_list.html.erb b/app/views/forum_post_votes/_list.html.erb index 77db71503..d866af2d0 100644 --- a/app/views/forum_post_votes/_list.html.erb +++ b/app/views/forum_post_votes/_list.html.erb @@ -11,6 +11,6 @@ <%= render "forum_post_votes/vote", vote: vote, forum_post: forum_post %> <% end %> -<% if forum_post.tag_change_request && forum_post.tag_change_request.is_pending? && !votes.by(CurrentUser.user.id).exists? %> +<% if forum_post.votable? && !votes.by(CurrentUser.user.id).exists? %> <%= render "forum_post_votes/add_vote", vote: votes.by(CurrentUser.user.id).first, forum_post: forum_post %> <% end %> diff --git a/app/views/forum_post_votes/_vote.html.erb b/app/views/forum_post_votes/_vote.html.erb index d46d56ab3..51dc051b1 100644 --- a/app/views/forum_post_votes/_vote.html.erb +++ b/app/views/forum_post_votes/_vote.html.erb @@ -4,7 +4,7 @@ %>
  • - <% if forum_post.tag_change_request && forum_post.tag_change_request.is_pending? && vote.creator_id == CurrentUser.id %> + <% if forum_post.votable? && vote.creator_id == CurrentUser.id %> <%= link_to content_tag(:i, nil, class: "far #{vote.fa_class}"), forum_post_vote_path(vote, format: "js"), remote: true, method: :delete %> <%= link_to_user vote.creator %> <% else %> diff --git a/app/views/forum_posts/_forum_post.html.erb b/app/views/forum_posts/_forum_post.html.erb index 47c2bb49e..6b22ef4cf 100644 --- a/app/views/forum_posts/_forum_post.html.erb +++ b/app/views/forum_posts/_forum_post.html.erb @@ -40,7 +40,7 @@ <% if forum_post.reportable_by?(CurrentUser.user) %>
  • <%= link_to "Report", new_moderation_report_path(moderation_report: { model_type: "ForumPost", model_id: forum_post.id }), remote: true, title: "Report this forum post to the moderators" %>
  • <% end %> - <% if forum_post.votable? %> + <% if forum_post.bulk_update_request.present? %> diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index df7240529..38e0119db 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -14,17 +14,6 @@ class ForumPostTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end - context "#votable?" do - setup do - @post = FactoryBot.build(:forum_post, :topic_id => @topic.id, :body => "[[aaa]] -> [[bbb]]") - @tag_alias = FactoryBot.create(:tag_alias, forum_post: @post) - end - - should "be true for a post associated with a tag alias" do - assert(@post.votable?) - end - end - context "that mentions a user" do context "in a quote block" do setup do