Skip to content

Instantly share code, notes, and snippets.

@gabetax
Created February 12, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabetax/1a01d4543c3d6e538529 to your computer and use it in GitHub Desktop.
Save gabetax/1a01d4543c3d6e538529 to your computer and use it in GitHub Desktop.
Log which callback is being run before running it
ActiveSupport::Callbacks::Callback
class ActiveSupport::Callbacks::Callback
def start(key=nil, object=nil)
return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?")
# options[0] is the compiled form of supplied conditions
# options[1] is the "end" for the conditional
#
case @kind
when :before
# if condition # before_save :filter_name, :if => :condition
# filter_name
# end
<<-RUBY_EVAL
if !halted && #{@compiled_options}
# This double assignment is to prevent warnings in 1.9.3 as
# the `result` variable is not always used except if the
# terminator code refers to it.
unless #{name.inspect} == :initialize
Rails.logger.debug ''
Rails.logger.debug ''
Rails.logger.debug 'before ' + #{name.to_s.inspect} + ' ' + inspect
Rails.logger.debug "#{@filter}"
end
result = result = #{@filter}
halted = (#{chain.config[:terminator]})
if halted
halted_callback_hook(#{@raw_filter.inspect.inspect})
end
end
RUBY_EVAL
when :around
# Compile around filters with conditions into proxy methods
# that contain the conditions.
#
# For `around_save :filter_name, :if => :condition':
#
# def _conditional_callback_save_17
# if condition
# filter_name do
# yield self
# end
# else
# yield self
# end
# end
#
name = "_conditional_callback_#{@kind}_#{next_id}"
@klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{name}(halted)
if #{@compiled_options} && !halted
unless #{name.inspect} == :initialize
Rails.logger.debug ''
Rails.logger.debug ''
Rails.logger.debug 'around ' + #{name.to_s.inspect} + ' ' + inspect
Rails.logger.debug "#{@filter}"
end
#{@filter} do
yield self
end
else
yield self
end
end
RUBY_EVAL
"#{name}(halted) do"
end
end
# This will supply contents for around and after filters, but not
# before filters (for the backward pass).
def end(key=nil, object=nil)
return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?")
case @kind
when :after
# after_save :filter_name, :if => :condition
<<-RUBY_EVAL
if #{@compiled_options}
unless #{name.inspect} == :initialize || #{name.inspect} == :find
Rails.logger.debug ''
Rails.logger.debug ''
Rails.logger.debug 'after ' + #{name.to_s.inspect} + ' ' + inspect
Rails.logger.debug "#{@filter}"
end
#{@filter}
end
RUBY_EVAL
when :around
<<-RUBY_EVAL
value
end
RUBY_EVAL
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment