Skip to content

Commit

Permalink
Use prepend when defined instead of alias_method_chain
Browse files Browse the repository at this point in the history
This is to get the ball rolling on upgrading to Module#prepend,
since `alias_method_chain` is deprecated in Rails 5.0.

This PR is probably not an ideal implementation.

A simpler migration would be to replace alias_method_chain with two
alias_methods.

Opening the discussion…
  • Loading branch information
jonatack committed Apr 2, 2015
1 parent 5d17025 commit 2b8a74a
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions lib/web_console/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
ActionDispatch::DebugExceptions.class_eval do
def render_exception_with_web_console(env, exception)
render_exception_without_web_console(env, exception).tap do
error = ActionDispatch::ExceptionWrapper.new(env, exception).exception
if defined?(Module.prepend) && Rails.version >= '4'
module RenderExceptionsWithWebConsole
def render_exception(env, exception)
super.tap do
error = ActionDispatch::ExceptionWrapper.new(env, exception).exception

# Get the original exception if ExceptionWrapper decides to follow it.
env['web_console.exception'] = error
# Get the original exception if ExceptionWrapper decides to follow it.
env['web_console.exception'] = error

# ActionView::Template::Error bypass ExceptionWrapper original
# exception following. The backtrace in the view is generated from
# reaching out to original_exception in the view.
if error.is_a?(ActionView::Template::Error)
env['web_console.exception'] = error.original_exception
# ActionView::Template::Error bypass ExceptionWrapper original
# exception following. The backtrace in the view is generated from
# reaching out to original_exception in the view.
if error.is_a?(ActionView::Template::Error)
env['web_console.exception'] = error.original_exception
end
end
end
end

alias_method_chain :render_exception, :web_console
module ActionDispatch
class DebugExceptions
prepend RenderExceptionsWithWebConsole
end
end
else
# Legacy code that duplicates the module above, using `alias_method_chain`.
# TODO: Remove me when support for Ruby < 2 && Rails < 4 is dropped.
ActionDispatch::DebugExceptions.class_eval do
def render_exception_with_web_console(env, exception)
render_exception_without_web_console(env, exception).tap do
error = ActionDispatch::ExceptionWrapper.new(env, exception).exception
env['web_console.exception'] = error
if error.is_a?(ActionView::Template::Error)
env['web_console.exception'] = error.original_exception
end
end
end
alias_method_chain :render_exception, :web_console
end
end

0 comments on commit 2b8a74a

Please sign in to comment.