Skip to content

Instantly share code, notes, and snippets.

@Yoshyn
Last active April 5, 2019 13:49
Show Gist options
  • Save Yoshyn/d631f387ca5d3d09723859f228bb83fb to your computer and use it in GitHub Desktop.
Save Yoshyn/d631f387ca5d3d09723859f228bb83fb to your computer and use it in GitHub Desktop.
Give backtraces for all callback related to self and inherited rails controller where the module is included.
module DebugControllerCallbackChain
def self.included(base)
class << base
prepend ClassMethods
end
end
module ClassMethods
def inherited(base)
super
base.class_eval do
_process_action_callbacks.each_with_index do |callback, index|
define_method(callback.filter) do |*args|
if base == self.class
if index == 0
Rails.logger.debug("=============================================================================================================")
Rails.logger.debug("=============================================================================================================")
Rails.logger.debug("#{request.uuid} : #{request.path} called the action #{controller_name}##{action_name} | ssl?(#{request.ssl?})")
Rails.logger.debug("#{request.uuid} : Headers are : (#{request.headers.reject { |key| key.to_s.include?('.') }})")
callback_list = _process_action_callbacks.inject(Hash.new {|h,k| h[k]=[]}) do |acc, callback|
acc[callback.kind] << callback.filter; acc
end
Rails.logger.debug("#{request.uuid} : Controller #{base} will execute the following callbacks : #{callback_list}")
Rails.logger.debug("#{request.uuid} : Params(#{params})")
Rails.logger.debug("#{request.uuid} : Cookies(#{request.cookies})")
Rails.logger.debug("#{request.uuid} : Session id(#{request.session_options[:id]}) Values(#{session})")
end
callback_kind = callback.kind.to_s.classify
callback_meth = method(callback.filter).to_s
Rails.logger.debug("#{request.uuid} : <Callback#{callback_kind}Begin => #{callback_meth}>")
return_value = super(*args)
Rails.logger.debug("#{request.uuid} : </Callback#{callback_kind}End => #{callback_meth} (return=#{return_value})>")
if index == (_process_action_callbacks.count - 1 )
Rails.logger.debug("#{request.uuid} : Params(#{params})")
Rails.logger.debug("#{request.uuid} : Cookies(#{request.cookies})")
Rails.logger.debug("#{request.uuid} : Session id(#{request.session_options[:id]}) Values(#{session})")
Rails.logger.debug("=============================================================================================================")
Rails.logger.debug("=============================================================================================================")
end
return_value
else
super(*args)
end
end
end
end
end
end
end
class ApplicationController < ActionController::Base
include DebugControllerCallbackChain
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment