Added Mixin to disable rails Model callbacks via block for e.g. creating a User but not fetching the avatar.

This commit is contained in:
Thorsten Eckel 2017-11-29 17:39:38 +01:00
parent 9677e1aee7
commit 3b16ae8ab9

View file

@ -0,0 +1,55 @@
class Sequencer
class Unit
module Import
module Common
module Model
module Mixin
module WithoutCallback
module ClassMethods
def without_callback(*callback)
@callbacks ||= []
@callbacks.push(callback)
end
def callbacks
Array(@callbacks)
end
end
def self.prepended(base)
base.extend(ClassMethods)
base.uses :model_class
end
def process
# keep the super call as the last or only entry point
entry_point = proc do
super
end
# loop over all registerd callbacks
self.class.callbacks.each do |callback|
# create a duplicate of the previous entry point
# to avoid an endless loop
previous_entry_point = entry_point.dup
# replace the previous entry point with a wrapped version
# which skips the current callback
entry_point = proc do
model_class.without_callback(*callback, &previous_entry_point)
end
end
# start at the last registerd entry point
# and go deep till we reach our super call
entry_point.call
end
end
end
end
end
end
end
end