without_callback.rb 1.6 KB

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