Skip to content

Instantly share code, notes, and snippets.

@arturictus
Last active August 29, 2015 14:17
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 arturictus/085872a048137ee94802 to your computer and use it in GitHub Desktop.
Save arturictus/085872a048137ee94802 to your computer and use it in GitHub Desktop.
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
# 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