Skip to content

Instantly share code, notes, and snippets.

@dblock
Created June 4, 2011 20:08
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 dblock/1008302 to your computer and use it in GitHub Desktop.
Save dblock/1008302 to your computer and use it in GitHub Desktop.
Suppress ActiveSupport callbacks
module ActiveSupport::Callbacks::ClassMethods
def without_callbacks(callbacks, &block)
saved = {}
callbacks.each do |callback|
saved[callback] = instance_method(callback)
remove_method(callback) if respond_to?(callback)
define_method(callback){ true }
end
begin
result = yield
ensure
callbacks.each do |callback|
remove_method(callback)
define_method(callback, saved[callback])
end
end
result
end
end
@dblock
Copy link
Author

dblock commented Jun 4, 2011

I am aware that ActiveSupport has a new clever method of dealing with this, but it has always confused me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment