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:
parent
9677e1aee7
commit
3b16ae8ab9
1 changed files with 55 additions and 0 deletions
|
@ -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
|
Loading…
Reference in a new issue