Last active
August 29, 2015 14:17
-
-
Save arturictus/085872a048137ee94802 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SkippableCallbacks | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def _skippable_callbacks | |
[ | |
:after_initialize, | |
:after_build, | |
:before_validation, | |
:after_validation, | |
:before_create, | |
:around_create, | |
:after_create, | |
:after_find, | |
:before_update, | |
:around_update, | |
:after_update, | |
:before_upsert, | |
:around_upsert, | |
:after_upsert, | |
:before_save, | |
:around_save, | |
:after_save, | |
:before_destroy, | |
:around_destroy, | |
:after_destroy | |
] | |
end | |
def _skippable_names | |
_skippable_callbacks.map{ |sym| sym.to_s.split('_')[1] }.uniq | |
end | |
# create_callbacks = send(:get_callbacks, :create) | |
# send(:reset_callbacks, :create) | |
# create_callbacks == send(:get_callbacks, :create) | |
# => false | |
# send(:set_callbacks, :create, create_callbacks) | |
# create_callbacks == send(:get_callbacks, :create) | |
# => true | |
def disable_callback | |
create_callbacks = send(:get_callbacks, :create) | |
send(:reset_callbacks, :create) | |
send(:set_callbacks, :create, create_callbacks) | |
end | |
end | |
end |
This file contains hidden or 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
# another example | |
module ActiveSupport::Callbacks::ClassMethods | |
def without_callback(*args, &block) | |
skip_callback(*args) | |
yield | |
set_callback(*args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment