From 3b16ae8ab938cc04b880cd6f67df697865f78d2d Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 29 Nov 2017 17:39:38 +0100 Subject: [PATCH] Added Mixin to disable rails Model callbacks via block for e.g. creating a User but not fetching the avatar. --- .../common/model/mixin/without_callback.rb | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/sequencer/unit/import/common/model/mixin/without_callback.rb diff --git a/lib/sequencer/unit/import/common/model/mixin/without_callback.rb b/lib/sequencer/unit/import/common/model/mixin/without_callback.rb new file mode 100644 index 000000000..27f9b0a83 --- /dev/null +++ b/lib/sequencer/unit/import/common/model/mixin/without_callback.rb @@ -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