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:
@@ -7,4 +7,11 @@ class ArtistCommentaryVersion < ApplicationRecord
|
|||||||
q = q.search_attributes(params, :post, :updater, :original_title, :original_description, :translated_title, :translated_description)
|
q = q.search_attributes(params, :post, :updater, :original_title, :original_description, :translated_title, :translated_description)
|
||||||
q.apply_default_order(params)
|
q.apply_default_order(params)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ class ArtistVersion < ApplicationRecord
|
|||||||
extend SearchMethods
|
extend SearchMethods
|
||||||
|
|
||||||
def previous
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ class NoteVersion < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def previous
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -108,7 +108,10 @@ class PoolArchive < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def previous
|
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
|
end
|
||||||
|
|
||||||
def pretty_name
|
def pretty_name
|
||||||
|
|||||||
@@ -92,13 +92,16 @@ class PostArchive < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def previous
|
def previous
|
||||||
# HACK: if all the post versions for this post have already been preloaded,
|
@previous ||= begin
|
||||||
# we can use that to avoid a SQL query.
|
# HACK: if all the post versions for this post have already been preloaded,
|
||||||
if association(:post).loaded? && post && post.association(:versions).loaded?
|
# we can use that to avoid a SQL query.
|
||||||
post.versions.sort_by(&:version).reverse.find { |v| v.version < version }
|
if association(:post).loaded? && post && post.association(:versions).loaded?
|
||||||
else
|
ver = [post.versions.sort_by(&:version).reverse.find { |v| v.version < version }]
|
||||||
PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").first
|
else
|
||||||
|
ver = PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").limit(1).to_a
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@previous.first
|
||||||
end
|
end
|
||||||
|
|
||||||
def visible?
|
def visible?
|
||||||
|
|||||||
@@ -23,7 +23,10 @@ class WikiPageVersion < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def previous
|
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
|
end
|
||||||
|
|
||||||
def category_name
|
def category_name
|
||||||
|
|||||||
Reference in New Issue
Block a user