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:
@@ -3,7 +3,7 @@ class ArtistCommentariesController < ApplicationController
|
|||||||
before_action :member_only, only: [:create_or_update, :revert]
|
before_action :member_only, only: [:create_or_update, :revert]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@commentaries = ArtistCommentary.search(search_params).paginate(params[:page], :limit => params[:limit])
|
@commentaries = ArtistCommentary.paginated_search(params)
|
||||||
respond_with(@commentaries)
|
respond_with(@commentaries)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ class ArtistCommentaryVersionsController < ApplicationController
|
|||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@commentary_versions = ArtistCommentaryVersion.search(search_params).paginate(params[:page], :limit => params[:limit])
|
@commentary_versions = ArtistCommentaryVersion.paginated_search(params)
|
||||||
respond_with(@commentary_versions)
|
respond_with(@commentary_versions)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class ArtistUrlsController < ApplicationController
|
|||||||
before_action :member_only, except: [:index]
|
before_action :member_only, except: [:index]
|
||||||
|
|
||||||
def 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|
|
respond_with(@artist_urls) do |format|
|
||||||
format.json { render json: @artist_urls.to_json(include: "artist",) }
|
format.json { render json: @artist_urls.to_json(include: "artist",) }
|
||||||
format.xml { render xml: @artist_urls.to_xml(include: "artist", root: "artist-urls") }
|
format.xml { render xml: @artist_urls.to_xml(include: "artist", root: "artist-urls") }
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ class ArtistVersionsController < ApplicationController
|
|||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@artist_versions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class BansController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@bans = Ban.search(search_params).paginate(params[:page], :limit => params[:limit])
|
@bans = Ban.paginated_search(params)
|
||||||
respond_with(@bans) do |fmt|
|
respond_with(@bans) do |fmt|
|
||||||
fmt.html { @bans = @bans.includes(:user, :banner) }
|
fmt.html { @bans = @bans.includes(:user, :banner) }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class BulkUpdateRequestsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@bulk_update_requests)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ private
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index_by_comment
|
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|
|
respond_with(@comments) do |format|
|
||||||
format.atom do
|
format.atom do
|
||||||
@comments = @comments.includes(:post, :creator).load
|
@comments = @comments.includes(:post, :creator).load
|
||||||
|
|||||||
@@ -19,8 +19,7 @@ class DmailsController < ApplicationController
|
|||||||
if params[:folder] && params[:set_default_folder]
|
if params[:folder] && params[:set_default_folder]
|
||||||
cookies.permanent[:dmail_folder] = params[:folder]
|
cookies.permanent[:dmail_folder] = params[:folder]
|
||||||
end
|
end
|
||||||
@query = Dmail.active.visible.search(search_params)
|
@dmails = Dmail.active.visible.paginated_search(params)
|
||||||
@dmails = @query.paginate(params[:page], :limit => params[:limit])
|
|
||||||
respond_with(@dmails)
|
respond_with(@dmails)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class FavoriteGroupsController < ApplicationController
|
|||||||
respond_to :html, :xml, :json, :js
|
respond_to :html, :xml, :json, :js
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@favorite_groups)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,7 @@ class ForumPostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@query = ForumPost.search(search_params)
|
@forum_posts = ForumPost.paginated_search(params).includes(:topic)
|
||||||
@forum_posts = @query.includes(:topic).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
|
|
||||||
respond_with(@forum_posts)
|
respond_with(@forum_posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ class ForumTopicsController < ApplicationController
|
|||||||
params[:search] ||= {}
|
params[:search] ||= {}
|
||||||
params[:search][:order] ||= "sticky" if request.format == Mime::Type.lookup("text/html")
|
params[:search][:order] ||= "sticky" if request.format == Mime::Type.lookup("text/html")
|
||||||
|
|
||||||
@query = ForumTopic.active.search(search_params)
|
@forum_topics = ForumTopic.active.paginated_search(params)
|
||||||
@forum_topics = @query.paginate(params[:page], :limit => per_page, :search_count => params[:search])
|
|
||||||
@forum_topics = @forum_topics.includes(:creator, :updater).load if request.format.html?
|
@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?
|
@forum_topics = @forum_topics.includes(:creator, :original_post).load if request.format.atom?
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class IpBansController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@ip_bans)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class JanitorTrialsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@janitor_trials = JanitorTrial.search(search_params).paginate(params[:page], :limit => params[:limit])
|
@janitor_trials = JanitorTrial.paginated_search(params)
|
||||||
respond_with(@janitor_trials)
|
respond_with(@janitor_trials)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ class ModActionsController < ApplicationController
|
|||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@mod_actions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ module Moderator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@post_disapprovals)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ class NoteVersionsController < ApplicationController
|
|||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
|
|
||||||
def index
|
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?
|
@note_versions = @note_versions.includes(:updater) if request.format.html?
|
||||||
respond_with(@note_versions)
|
respond_with(@note_versions)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class NotesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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?
|
@notes = @notes.includes(:creator) if request.format.html?
|
||||||
respond_with(@notes)
|
respond_with(@notes)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ class PoolVersionsController < ApplicationController
|
|||||||
@pool = Pool.find(params[:search][:pool_id])
|
@pool = Pool.find(params[:search][:pool_id])
|
||||||
end
|
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)
|
respond_with(@pool_versions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class PoolsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@pools)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ class PostAppealsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@post_appeals = PostAppeal.includes(:creator).search(search_params).includes(post: [:appeals, :uploader, :approver])
|
@post_appeals = PostAppeal.includes(:creator).paginated_search(params).includes(post: [:appeals, :uploader, :approver])
|
||||||
@post_appeals = @post_appeals.paginate(params[:page], limit: params[:limit])
|
|
||||||
respond_with(@post_appeals)
|
respond_with(@post_appeals)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ class PostApprovalsController < ApplicationController
|
|||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@post_approvals)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ class PostFlagsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@post_flags = PostFlag.search(search_params).includes(:creator, post: [:flags, :uploader, :approver])
|
@post_flags = PostFlag.paginated_search(params).includes(:creator, post: [:flags, :uploader, :approver])
|
||||||
@post_flags = @post_flags.paginate(params[:page], limit: params[:limit])
|
|
||||||
respond_with(@post_flags)
|
respond_with(@post_flags)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class PostReplacementsController < ApplicationController
|
|||||||
|
|
||||||
def index
|
def index
|
||||||
params[:search][:post_id] = params.delete(:post_id) if params.has_key?(:post_id)
|
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)
|
respond_with(@post_replacements)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class PostVersionsController < ApplicationController
|
|||||||
respond_to :js, only: [:undo]
|
respond_to :js, only: [:undo]
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@post_versions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ class SavedSearchesController < ApplicationController
|
|||||||
respond_to :html, :xml, :json, :js
|
respond_to :html, :xml, :json, :js
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@saved_searches)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class TagAliasesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@tag_aliases)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class TagImplicationsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@tag_implications)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class TagsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class UploadsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@uploads)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,7 @@ class UserFeedbacksController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@search = UserFeedback.visible.search(search_params)
|
@user_feedbacks = UserFeedback.visible.paginated_search(params)
|
||||||
@user_feedbacks = @search.paginate(params[:page], :limit => params[:limit])
|
|
||||||
respond_with(@user_feedbacks)
|
respond_with(@user_feedbacks)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class UsersController < ApplicationController
|
|||||||
@user = User.find_by_name!(params[:name])
|
@user = User.find_by_name!(params[:name])
|
||||||
redirect_to user_path(@user)
|
redirect_to user_path(@user)
|
||||||
else
|
else
|
||||||
@users = User.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
|
@users = User.paginated_search(params)
|
||||||
respond_with(@users)
|
respond_with(@users)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class WikiPageVersionsController < ApplicationController
|
|||||||
layout "sidebar"
|
layout "sidebar"
|
||||||
|
|
||||||
def index
|
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)
|
respond_with(@wiki_page_versions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class WikiPagesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
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|
|
respond_with(@wiki_pages) do |format|
|
||||||
format.html do
|
format.html do
|
||||||
if params[:page].nil? || params[:page].to_i == 1
|
if params[:page].nil? || params[:page].to_i == 1
|
||||||
|
|||||||
@@ -6,6 +6,11 @@ class ApplicationRecord < ActiveRecord::Base
|
|||||||
def paginate(*options)
|
def paginate(*options)
|
||||||
extending(PaginationExtension).paginate(*options)
|
extending(PaginationExtension).paginate(*options)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user