Merge pull request #2912 from evazion/opt-post-archive
Fix N+1 queries issues on /post_versions
This commit is contained in:
@@ -3,7 +3,7 @@ class PostVersionsController < ApplicationController
|
|||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@post_versions = PostArchive.search(params[:search]).order("updated_at desc, id desc").paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
|
@post_versions = PostArchive.includes(:post, :updater).search(params[:search]).order("updated_at desc, id desc").paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
|
||||||
respond_with(@post_versions) do |format|
|
respond_with(@post_versions) do |format|
|
||||||
format.xml do
|
format.xml do
|
||||||
render :xml => @post_versions.to_xml(:root => "post-versions")
|
render :xml => @post_versions.to_xml(:root => "post-versions")
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
class PostArchive < ActiveRecord::Base
|
class PostArchive < ActiveRecord::Base
|
||||||
extend Memoist
|
extend Memoist
|
||||||
|
|
||||||
|
belongs_to :post
|
||||||
|
belongs_to :updater, class_name: "User"
|
||||||
|
|
||||||
def self.enabled?
|
def self.enabled?
|
||||||
Danbooru.config.aws_sqs_archives_url.present?
|
Danbooru.config.aws_sqs_archives_url.present?
|
||||||
end
|
end
|
||||||
@@ -92,18 +95,10 @@ class PostArchive < ActiveRecord::Base
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def post
|
|
||||||
Post.where(id: post_id).first
|
|
||||||
end
|
|
||||||
|
|
||||||
def previous
|
def previous
|
||||||
PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").first
|
PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").first
|
||||||
end
|
end
|
||||||
|
|
||||||
def updater
|
|
||||||
User.find(updater_id)
|
|
||||||
end
|
|
||||||
|
|
||||||
def diff(version = nil)
|
def diff(version = nil)
|
||||||
if post.nil?
|
if post.nil?
|
||||||
latest_tags = tag_array
|
latest_tags = tag_array
|
||||||
@@ -237,13 +232,9 @@ class PostArchive < ActiveRecord::Base
|
|||||||
post.save!
|
post.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
def updater
|
|
||||||
User.find_by_id(updater_id)
|
|
||||||
end
|
|
||||||
|
|
||||||
def method_attributes
|
def method_attributes
|
||||||
super + [:obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags, :updater_name]
|
super + [:obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags, :updater_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
memoize :previous, :post, :tag_array, :changes, :added_tags_with_fields, :removed_tags_with_fields, :obsolete_removed_tags, :obsolete_added_tags, :unchanged_tags
|
memoize :previous, :tag_array, :changes, :added_tags_with_fields, :removed_tags_with_fields, :obsolete_removed_tags, :obsolete_added_tags, :unchanged_tags
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,7 +37,7 @@
|
|||||||
<% if CurrentUser.is_member? %>
|
<% if CurrentUser.is_member? %>
|
||||||
<td>
|
<td>
|
||||||
<% if post_version.post.visible? %>
|
<% if post_version.post.visible? %>
|
||||||
<% if post_version.id != post_version.post.versions.first.id %>
|
<% if post_version.version != 1 %>
|
||||||
<%= link_to "Undo", undo_post_version_path(post_version), :method => :put, :remote => true %> |
|
<%= link_to "Undo", undo_post_version_path(post_version), :method => :put, :remote => true %> |
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= link_to "Revert to", revert_post_path(post_version.post_id, :version_id => post_version.id), :method => :put, :remote => true %>
|
<%= link_to "Revert to", revert_post_path(post_version.post_id, :version_id => post_version.id), :method => :put, :remote => true %>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<div id="a-index">
|
<div id="a-index">
|
||||||
<h1>Changes</h1>
|
<h1>Changes</h1>
|
||||||
|
|
||||||
<% if @post_versions.empty? %>
|
<% if @post_versions.length == 0 %>
|
||||||
<%= render "post_sets/blank" %>
|
<%= render "post_sets/blank" %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= render "listing", :post_versions => @post_versions %>
|
<%= render "listing", :post_versions => @post_versions %>
|
||||||
|
|||||||
Reference in New Issue
Block a user