without_callback.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Sequencer::Unit::Import::Common::Model::Mixin::WithoutCallback
  3. module ClassMethods
  4. def without_callback(*callback)
  5. @callbacks ||= []
  6. @callbacks.push(callback)
  7. end
  8. def callbacks
  9. Array(@callbacks)
  10. end
  11. end
  12. def self.prepended(base)
  13. base.extend(ClassMethods)
  14. base.uses :model_class
  15. end
  16. def process
  17. # keep the super call as the last or only entry point
  18. entry_point = proc do
  19. super
  20. end
  21. # loop over all registerd callbacks
  22. self.class.callbacks.each do |callback|
  23. # create a duplicate of the previous entry point
  24. # to avoid an endless loop
  25. previous_entry_point = entry_point.dup
  26. # replace the previous entry point with a wrapped version
  27. # which skips the current callback
  28. entry_point = proc do
  29. model_class.without_callback(*callback, &previous_entry_point)
  30. end
  31. end
  32. # start at the last registerd entry point
  33. # and go deep till we reach our super call
  34. entry_point.call
  35. end
  36. end