Fixes an N+1 queries problem in the /moderator/post/queue view by
prefetching disapprovals and uploaders.
Also the way disapproval messages were previously rendered triggered a bunch
of sql queries for each post:
SELECT COUNT(*) FROM "post_disapprovals" WHERE "post_disapprovals"."post_id" = $1 [["post_id", 52]]
SELECT COUNT(*) FROM "post_disapprovals" WHERE "post_disapprovals"."post_id" = $1 AND "post_disapprovals"."reason" = $2 [["post_id", 52], ["reason", "breaks_rules"]]
SELECT COUNT(*) FROM "post_disapprovals" WHERE "post_disapprovals"."post_id" = $1 AND "post_disapprovals"."reason" = $2 [["post_id", 52], ["reason", "poor_quality"]]
SELECT COUNT(*) FROM "post_disapprovals" WHERE "post_disapprovals"."post_id" = $1 AND "post_disapprovals"."reason" IN ('disinterest', 'legacy') [["post_id", 52]]
SELECT COUNT(*) FROM "post_disapprovals" WHERE "post_disapprovals"."post_id" = $1 AND (message is not null and message <> '') [["post_id", 52]]
SELECT "post_disapprovals".* FROM "post_disapprovals" WHERE "post_disapprovals"."post_id" = $1 AND (message is not null and message <> '') [["post_id", 52]]
This refactors to bring it down to one:
SELECT "post_disapprovals".* FROM "post_disapprovals" WHERE "post_disapprovals"."post_id" = $1 [["post_id", 52]]
53 lines
1.5 KiB
Ruby
53 lines
1.5 KiB
Ruby
module Moderator
|
|
module Post
|
|
class QueuesController < ApplicationController
|
|
RANDOM_COUNT = 12
|
|
|
|
respond_to :html, :json
|
|
before_filter :approver_only
|
|
skip_before_filter :api_check
|
|
|
|
def show
|
|
cookies.permanent[:moderated] = Time.now.to_i
|
|
|
|
if params[:per_page]
|
|
cookies.permanent["mq_per_page"] = params[:per_page]
|
|
end
|
|
|
|
::Post.without_timeout do
|
|
@posts = ::Post.includes(:disapprovals, :uploader).order("posts.id asc").pending_or_flagged.available_for_moderation(params[:hidden]).tag_match(params[:query]).paginate(params[:page], :limit => per_page)
|
|
@posts.each # hack to force rails to eager load
|
|
end
|
|
respond_with(@posts)
|
|
end
|
|
|
|
def random
|
|
cookies.permanent[:moderated] = Time.now.to_i
|
|
|
|
::Post.without_timeout do
|
|
@posts = ::Post.includes(:disapprovals, :uploader).order("posts.id asc").pending_or_flagged.available_for_moderation(false).reorder("random()").limit(RANDOM_COUNT)
|
|
@posts.each # hack to force rails to eager load
|
|
|
|
if @posts.empty?
|
|
flash[:notice] = "Nothing left to moderate!"
|
|
redirect_to(params[:return_to] || posts_path)
|
|
return
|
|
end
|
|
end
|
|
|
|
respond_with(@posts)
|
|
end
|
|
|
|
protected
|
|
|
|
def show_moderation_notice?
|
|
false
|
|
end
|
|
|
|
def per_page
|
|
cookies["mq_per_page"] || params[:per_page] || 25
|
|
end
|
|
end
|
|
end
|
|
end
|