Module: ActiveSupport::Callbacks::ClassMethods

Defined in:
lib/active_support/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#define_callbacks(*callbacks) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/active_support/callbacks.rb', line 205

def define_callbacks(*callbacks)
  callbacks.each do |callback|
    class_eval <<-"end_eval"
      def self.#{callback}(*methods, &block)                             # def self.before_save(*methods, &block)
        callbacks = CallbackChain.build(:#{callback}, *methods, &block)  #   callbacks = CallbackChain.build(:before_save, *methods, &block)
        @#{callback}_callbacks ||= CallbackChain.new                     #   @before_save_callbacks ||= CallbackChain.new
        @#{callback}_callbacks.concat callbacks                          #   @before_save_callbacks.concat callbacks
      end                                                                # end
                                                                         #
      def self.#{callback}_callback_chain                                # def self.before_save_callback_chain
        @#{callback}_callbacks ||= CallbackChain.new                     #   @before_save_callbacks ||= CallbackChain.new
                                                                         #
        if superclass.respond_to?(:#{callback}_callback_chain)           #   if superclass.respond_to?(:before_save_callback_chain)
          CallbackChain.new(                                             #     CallbackChain.new(
            superclass.#{callback}_callback_chain +                      #       superclass.before_save_callback_chain +
            @#{callback}_callbacks                                       #       @before_save_callbacks
          )                                                              #     )
        else                                                             #   else
          @#{callback}_callbacks                                         #     @before_save_callbacks
        end                                                              #   end
      end                                                                # end
    end_eval
  end
end