pagination: avoid counting pages outside searches.

Replace this common pattern in controllers:

    @tags = Tag.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])

with this:

    @tags = Tag.paginated_search(params)

`search_count` is used to skip doing a full page count when we're not
doing a search (on the assumption that the number of results will be
high when not constrained by a search). We didn't do this consistently
though. Refactor to do this in every controller.
This commit is contained in:
evazion
2019-10-07 21:50:36 -05:00
parent 93dd952949
commit a5ab25d0ba
34 changed files with 38 additions and 39 deletions

View File

@@ -3,7 +3,7 @@ class ArtistCommentariesController < ApplicationController
before_action :member_only, only: [:create_or_update, :revert]
def index
@commentaries = ArtistCommentary.search(search_params).paginate(params[:page], :limit => params[:limit])
@commentaries = ArtistCommentary.paginated_search(params)
respond_with(@commentaries)
end

View File

@@ -2,7 +2,7 @@ class ArtistCommentaryVersionsController < ApplicationController
respond_to :html, :xml, :json
def index
@commentary_versions = ArtistCommentaryVersion.search(search_params).paginate(params[:page], :limit => params[:limit])
@commentary_versions = ArtistCommentaryVersion.paginated_search(params)
respond_with(@commentary_versions)
end
end

View File

@@ -3,7 +3,7 @@ class ArtistUrlsController < ApplicationController
before_action :member_only, except: [:index]
def index
@artist_urls = ArtistUrl.includes(:artist).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@artist_urls = ArtistUrl.includes(:artist).paginated_search(params)
respond_with(@artist_urls) do |format|
format.json { render json: @artist_urls.to_json(include: "artist",) }
format.xml { render xml: @artist_urls.to_xml(include: "artist", root: "artist-urls") }

View File

@@ -2,7 +2,7 @@ class ArtistVersionsController < ApplicationController
respond_to :html, :xml, :json
def index
@artist_versions = ArtistVersion.includes(:updater).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@artist_versions = ArtistVersion.includes(:updater).paginated_search(params)
respond_with(@artist_versions)
end

View File

@@ -12,7 +12,7 @@ class BansController < ApplicationController
end
def index
@bans = Ban.search(search_params).paginate(params[:page], :limit => params[:limit])
@bans = Ban.paginated_search(params)
respond_with(@bans) do |fmt|
fmt.html { @bans = @bans.includes(:user, :banner) }
end

View File

@@ -42,7 +42,7 @@ class BulkUpdateRequestsController < ApplicationController
end
def index
@bulk_update_requests = BulkUpdateRequest.search(search_params).paginate(params[:page], :limit => params[:limit])
@bulk_update_requests = BulkUpdateRequest.paginated_search(params)
respond_with(@bulk_update_requests)
end

View File

@@ -89,7 +89,7 @@ private
end
def index_by_comment
@comments = Comment.includes(:creator, :updater).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@comments = Comment.includes(:creator, :updater).paginated_search(params)
respond_with(@comments) do |format|
format.atom do
@comments = @comments.includes(:post, :creator).load

View File

@@ -19,8 +19,7 @@ class DmailsController < ApplicationController
if params[:folder] && params[:set_default_folder]
cookies.permanent[:dmail_folder] = params[:folder]
end
@query = Dmail.active.visible.search(search_params)
@dmails = @query.paginate(params[:page], :limit => params[:limit])
@dmails = Dmail.active.visible.paginated_search(params)
respond_with(@dmails)
end

View File

@@ -3,7 +3,7 @@ class FavoriteGroupsController < ApplicationController
respond_to :html, :xml, :json, :js
def index
@favorite_groups = FavoriteGroup.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@favorite_groups = FavoriteGroup.paginated_search(params)
respond_with(@favorite_groups)
end

View File

@@ -24,8 +24,7 @@ class ForumPostsController < ApplicationController
end
def index
@query = ForumPost.search(search_params)
@forum_posts = @query.includes(:topic).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@forum_posts = ForumPost.paginated_search(params).includes(:topic)
respond_with(@forum_posts)
end

View File

@@ -23,8 +23,7 @@ class ForumTopicsController < ApplicationController
params[:search] ||= {}
params[:search][:order] ||= "sticky" if request.format == Mime::Type.lookup("text/html")
@query = ForumTopic.active.search(search_params)
@forum_topics = @query.paginate(params[:page], :limit => per_page, :search_count => params[:search])
@forum_topics = ForumTopic.active.paginated_search(params)
@forum_topics = @forum_topics.includes(:creator, :updater).load if request.format.html?
@forum_topics = @forum_topics.includes(:creator, :original_post).load if request.format.atom?

View File

@@ -12,7 +12,7 @@ class IpBansController < ApplicationController
end
def index
@ip_bans = IpBan.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit])
@ip_bans = IpBan.includes(:creator).paginated_search(params)
respond_with(@ip_bans)
end

View File

@@ -13,7 +13,7 @@ class JanitorTrialsController < ApplicationController
end
def index
@janitor_trials = JanitorTrial.search(search_params).paginate(params[:page], :limit => params[:limit])
@janitor_trials = JanitorTrial.paginated_search(params)
respond_with(@janitor_trials)
end

View File

@@ -2,7 +2,7 @@ class ModActionsController < ApplicationController
respond_to :html, :xml, :json
def index
@mod_actions = ModAction.includes(:creator).search(search_params).paginate(params[:page], limit: params[:limit])
@mod_actions = ModAction.includes(:creator).paginated_search(params)
respond_with(@mod_actions)
end

View File

@@ -12,7 +12,7 @@ module Moderator
end
def index
@post_disapprovals = PostDisapproval.includes(:user).search(search_params).paginate(params[:page], limit: params[:limit])
@post_disapprovals = PostDisapproval.includes(:user).paginated_search(params)
respond_with(@post_disapprovals)
end

View File

@@ -2,7 +2,7 @@ class NoteVersionsController < ApplicationController
respond_to :html, :xml, :json
def index
@note_versions = NoteVersion.search(search_params).paginate(params[:page], :limit => params[:limit])
@note_versions = NoteVersion.paginated_search(params)
@note_versions = @note_versions.includes(:updater) if request.format.html?
respond_with(@note_versions)
end

View File

@@ -6,7 +6,7 @@ class NotesController < ApplicationController
end
def index
@notes = Note.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@notes = Note.includes(:creator).paginated_search(params)
@notes = @notes.includes(:creator) if request.format.html?
respond_with(@notes)
end

View File

@@ -8,7 +8,7 @@ class PoolVersionsController < ApplicationController
@pool = Pool.find(params[:search][:pool_id])
end
@pool_versions = PoolArchive.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@pool_versions = PoolArchive.paginated_search(params)
respond_with(@pool_versions)
end

View File

@@ -17,7 +17,7 @@ class PoolsController < ApplicationController
end
def index
@pools = Pool.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@pools = Pool.includes(:creator).paginated_search(search_params)
respond_with(@pools)
end

View File

@@ -8,8 +8,7 @@ class PostAppealsController < ApplicationController
end
def index
@post_appeals = PostAppeal.includes(:creator).search(search_params).includes(post: [:appeals, :uploader, :approver])
@post_appeals = @post_appeals.paginate(params[:page], limit: params[:limit])
@post_appeals = PostAppeal.includes(:creator).paginated_search(params).includes(post: [:appeals, :uploader, :approver])
respond_with(@post_appeals)
end

View File

@@ -2,7 +2,7 @@ class PostApprovalsController < ApplicationController
respond_to :html, :xml, :json
def index
@post_approvals = PostApproval.includes(:post, :user).search(search_params).paginate(params[:page], limit: params[:limit])
@post_approvals = PostApproval.includes(:post, :user).paginated_search(params)
respond_with(@post_approvals)
end
end

View File

@@ -8,8 +8,7 @@ class PostFlagsController < ApplicationController
end
def index
@post_flags = PostFlag.search(search_params).includes(:creator, post: [:flags, :uploader, :approver])
@post_flags = @post_flags.paginate(params[:page], limit: params[:limit])
@post_flags = PostFlag.paginated_search(params).includes(:creator, post: [:flags, :uploader, :approver])
respond_with(@post_flags)
end

View File

@@ -24,7 +24,7 @@ class PostReplacementsController < ApplicationController
def index
params[:search][:post_id] = params.delete(:post_id) if params.has_key?(:post_id)
@post_replacements = PostReplacement.search(search_params).paginate(params[:page], limit: params[:limit])
@post_replacements = PostReplacement.paginated_search(params)
respond_with(@post_replacements)
end

View File

@@ -6,7 +6,7 @@ class PostVersionsController < ApplicationController
respond_to :js, only: [:undo]
def index
@post_versions = PostArchive.includes(:updater, post: [:versions]).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@post_versions = PostArchive.includes(:updater, post: [:versions]).paginated_search(params)
respond_with(@post_versions)
end

View File

@@ -2,7 +2,7 @@ class SavedSearchesController < ApplicationController
respond_to :html, :xml, :json, :js
def index
@saved_searches = saved_searches.search(search_params).paginate(params[:page], limit: params[:limit])
@saved_searches = saved_searches.paginated_search(params)
respond_with(@saved_searches)
end

View File

@@ -22,7 +22,7 @@ class TagAliasesController < ApplicationController
end
def index
@tag_aliases = TagAlias.includes(:antecedent_tag, :consequent_tag, :approver).search(search_params).paginate(params[:page], :limit => params[:limit])
@tag_aliases = TagAlias.includes(:antecedent_tag, :consequent_tag, :approver).paginated_search(params)
respond_with(@tag_aliases)
end

View File

@@ -22,7 +22,7 @@ class TagImplicationsController < ApplicationController
end
def index
@tag_implications = TagImplication.includes(:antecedent_tag, :consequent_tag, :approver).search(search_params).paginate(params[:page], :limit => params[:limit])
@tag_implications = TagImplication.includes(:antecedent_tag, :consequent_tag, :approver).paginated_search(params)
respond_with(@tag_implications)
end

View File

@@ -9,7 +9,7 @@ class TagsController < ApplicationController
end
def index
@tags = Tag.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@tags = Tag.paginated_search(params)
respond_with(@tags)
end

View File

@@ -24,7 +24,7 @@ class UploadsController < ApplicationController
end
def index
@uploads = Upload.search(search_params).includes(:post, :uploader).paginate(params[:page], :limit => params[:limit])
@uploads = Upload.paginated_search(params).includes(:post, :uploader)
respond_with(@uploads)
end

View File

@@ -20,8 +20,7 @@ class UserFeedbacksController < ApplicationController
end
def index
@search = UserFeedback.visible.search(search_params)
@user_feedbacks = @search.paginate(params[:page], :limit => params[:limit])
@user_feedbacks = UserFeedback.visible.paginated_search(params)
respond_with(@user_feedbacks)
end

View File

@@ -29,7 +29,7 @@ class UsersController < ApplicationController
@user = User.find_by_name!(params[:name])
redirect_to user_path(@user)
else
@users = User.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@users = User.paginated_search(params)
respond_with(@users)
end
end

View File

@@ -3,7 +3,7 @@ class WikiPageVersionsController < ApplicationController
layout "sidebar"
def index
@wiki_page_versions = WikiPageVersion.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@wiki_page_versions = WikiPageVersion.paginated_search(params)
respond_with(@wiki_page_versions)
end

View File

@@ -16,7 +16,7 @@ class WikiPagesController < ApplicationController
end
def index
@wiki_pages = WikiPage.includes(:creator).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@wiki_pages = WikiPage.includes(:creator).paginated_search(params)
respond_with(@wiki_pages) do |format|
format.html do
if params[:page].nil? || params[:page].to_i == 1

View File

@@ -6,6 +6,11 @@ class ApplicationRecord < ActiveRecord::Base
def paginate(*options)
extending(PaginationExtension).paginate(*options)
end
def paginated_search(params)
search_params = params.fetch(:search, {}).permit!
search(search_params).paginate(params[:page], limit: params[:limit], search_count: params[:search])
end
end
end