Commit 69f653b0 authored by evazion's avatar evazion

forum_topics/show: fix N+1 query patterns.

parent db4c67ee
......@@ -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
......
......@@ -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)
......
......@@ -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 %>
......@@ -4,7 +4,7 @@
%>
<li class="vote-score-<%= vote.vote_type %>">
<% 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 %>
......
......@@ -40,7 +40,7 @@
<% if forum_post.reportable_by?(CurrentUser.user) %>
<li><%= 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" %></li>
<% end %>
<% if forum_post.votable? %>
<% if forum_post.bulk_update_request.present? %>
<ul class="votes" id="forum-post-votes-for-<%= forum_post.id %>">
<%= render "forum_post_votes/list", votes: forum_post.votes, forum_post: forum_post %>
</ul>
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment