Alter previous method on all versions models

This is to prevent redoing the same SQL query which wasn't being cached.
This commit is contained in:
BrokenEagle
2020-02-08 06:46:58 +00:00
parent d2021256f0
commit 76dcccb7de
6 changed files with 32 additions and 10 deletions

View File

@@ -7,4 +7,11 @@ class ArtistCommentaryVersion < ApplicationRecord
q = q.search_attributes(params, :post, :updater, :original_title, :original_description, :translated_title, :translated_description)
q.apply_default_order(params)
end
def previous
@previous ||= begin
ArtistCommentaryVersion.where("post_id = ? and updated_at < ?", post_id, updated_at).order("updated_at desc").limit(1).to_a
end
@previous.first
end
end

View File

@@ -24,6 +24,9 @@ class ArtistVersion < ApplicationRecord
extend SearchMethods
def previous
ArtistVersion.where("artist_id = ? and created_at < ?", artist_id, created_at).order("created_at desc").first
@previous ||= begin
ArtistVersion.where("artist_id = ? and created_at < ?", artist_id, created_at).order("created_at desc").limit(1).to_a
end
@previous.first
end
end

View File

@@ -13,6 +13,9 @@ class NoteVersion < ApplicationRecord
end
def previous
NoteVersion.where("note_id = ? and updated_at < ?", note_id, updated_at).order("updated_at desc").first
@previous ||= begin
NoteVersion.where("note_id = ? and updated_at < ?", note_id, updated_at).order("updated_at desc").limit(1).to_a
end
@previous.first
end
end

View File

@@ -108,7 +108,10 @@ class PoolArchive < ApplicationRecord
end
def previous
PoolArchive.where("pool_id = ? and version < ?", pool_id, version).order("version desc").first
@previous ||= begin
PoolArchive.where("pool_id = ? and version < ?", pool_id, version).order("version desc").limit(1).to_a
end
@previous.first
end
def pretty_name

View File

@@ -92,13 +92,16 @@ class PostArchive < ApplicationRecord
end
def previous
# HACK: if all the post versions for this post have already been preloaded,
# we can use that to avoid a SQL query.
if association(:post).loaded? && post && post.association(:versions).loaded?
post.versions.sort_by(&:version).reverse.find { |v| v.version < version }
else
PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").first
@previous ||= begin
# HACK: if all the post versions for this post have already been preloaded,
# we can use that to avoid a SQL query.
if association(:post).loaded? && post && post.association(:versions).loaded?
ver = [post.versions.sort_by(&:version).reverse.find { |v| v.version < version }]
else
ver = PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").limit(1).to_a
end
end
@previous.first
end
def visible?

View File

@@ -23,7 +23,10 @@ class WikiPageVersion < ApplicationRecord
end
def previous
WikiPageVersion.where("wiki_page_id = ? and id < ?", wiki_page_id, id).order("id desc").first
@previous ||= begin
WikiPageVersion.where("wiki_page_id = ? and id < ?", wiki_page_id, id).order("id desc").limit(1).to_a
end
@previous.first
end
def category_name