Created
June 4, 2011 20:08
-
-
Save dblock/1008302 to your computer and use it in GitHub Desktop.
Suppress ActiveSupport callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am aware that ActiveSupport has a new clever method of dealing with this, but it has always confused me.