Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix handling WillPaginate::InvalidPage exceptions in Rails
They are automatically rescued as 404 in production.

fixes mislav#152 [ci skip]
  • Loading branch information
mislav committed Aug 11, 2011
1 parent b385f38 commit dc588e6
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/will_paginate/railtie.rb
@@ -1,4 +1,5 @@
require 'will_paginate'
require 'will_paginate/page_number'
require 'will_paginate/collection'
require 'will_paginate/i18n'

Expand All @@ -21,11 +22,38 @@ class Railtie < Rails::Railtie
end

def self.setup_actioncontroller
ActionDispatch::ShowExceptions.rescue_responses['WillPaginate::InvalidPage'] = :not_found
ActionDispatch::ShowExceptions.send :include, ShowExceptionsPatch
ActionController::Base.extend ControllerRescuePatch
end

def self.add_locale_path(config)
config.i18n.railties_load_path.unshift(*WillPaginate::I18n.load_path)
end

# Extending the exception handler middleware so it properly detects
# WillPaginate::InvalidPage regardless of it being a tag module.
module ShowExceptionsPatch
extend ActiveSupport::Concern
included { alias_method_chain :status_code, :paginate }
private
def status_code_with_paginate(exception)
if exception.is_a?(WillPaginate::InvalidPage) or
(exception.respond_to?(:original_exception) &&
exception.original_exception.is_a?(WillPaginate::InvalidPage))
Rack::Utils.status_code(:not_found)
else
status_code_without_paginate(exception)
end
end
end

module ControllerRescuePatch
def rescue_from(*args, &block)
if idx = args.index(WillPaginate::InvalidPage)
args[idx] = args[idx].name
end
super(*args, &block)
end
end
end
end

0 comments on commit dc588e6

Please sign in to comment.