clamp pagination

avoids a sql error when trying to offset a huge number
This commit is contained in:
joshua stein 2016-07-31 12:36:28 -05:00
parent b5f6ab36a8
commit 955c52b5bb
3 changed files with 16 additions and 9 deletions

View file

@ -201,9 +201,11 @@ class CommentsController < ApplicationController
@heading = @title = "Newest Comments" @heading = @title = "Newest Comments"
@cur_url = "/comments" @cur_url = "/comments"
@page = 1 @page = params[:page].to_i
if params[:page].to_i > 0 if @page == 0
@page = params[:page].to_i @page = 1
elsif @page < 0 || @page > (2 ** 32)
raise ActionController::RoutingError.new("page out of bounds")
end end
@comments = Comment.where( @comments = Comment.where(

View file

@ -224,7 +224,13 @@ private
end end
def page def page
params[:page].to_i > 0 ? params[:page].to_i : 1 p = params[:page].to_i
if p == 0
p = 1
elsif p < 0 || p > (2 ** 32)
raise ActionController::RoutingError.new("page out of bounds")
end
p
end end
def paginate(scope) def paginate(scope)

View file

@ -2,13 +2,12 @@ class ModerationsController < ApplicationController
def index def index
@title = t('.moderationlogtitle') @title = t('.moderationlogtitle')
@page = params[:page] ? params[:page].to_i : 0
@pages = (Moderation.count / 50).ceil @pages = (Moderation.count / 50).ceil
@page = params[:page].to_i
if @page < 1 if @page == 0
@page = 1 @page = 1
elsif @page > @pages elsif @page < 0 || @page > (2 ** 32) || @page > @pages
@page = @pages raise ActionController::RoutingError.new("page out of bounds")
end end
@moderations = Moderation.order("id desc").limit(50).offset((@page - 1) * @moderations = Moderation.order("id desc").limit(50).offset((@page - 1) *