module CanvasSync # Helper/Hack class to allow calling ActiveSupport callbacks on a class instead of just on instances class ClassCallbackExecutor include ActiveSupport::Callbacks attr_reader :callback_class delegate :__callbacks, to: :callback_class delegate_missing_to :callback_class def initialize(cls, env) @callback_class = cls env.keys.each do |k| define_singleton_method(k) do env[k] end end end def clazz callback_class end def self.run_callbacks(cls, callback, env={}, &blk) new(cls, env).run_callbacks(callback, &blk) end def self.run_if_defined(cls, callback, *args, &blk) if cls.respond_to?(:"_#{callback}_callbacks") run_callbacks(cls, callback, *args, &blk) else blk.call end end end end