module ActiveRecord::Callbacks
def self.included(base) #:nodoc:
def self.included(base) #:nodoc: base.extend Observable [:create_or_update, :valid?, :create, :update, :destroy].each do |method| base.send :alias_method_chain, method, :callbacks end base.send :include, ActiveSupport::Callbacks base.define_callbacks *CALLBACKS end
def after_create() end
Note that this callback is still wrapped in the transaction around +save+. For example, if you
Is called _after_ Base.save on new objects that haven't been saved yet (no record exists).
def after_create() end
def after_destroy() end
after_destroy { |record| logger.info( "Contact #{record.id} was destroyed." ) }
class Contact < ActiveRecord::Base
Is called _after_ Base.destroy (and all the attributes have been frozen).
def after_destroy() end
def after_save() end
after_save { logger.info( 'New contact saved!' ) }
class Contact < ActiveRecord::Base
invoke an external indexer at this point it won't see the changes in the database.
Note that this callback is still wrapped in the transaction around +save+. For example, if you
Is called _after_ Base.save (regardless of whether it's a +create+ or +update+ save).
def after_save() end
def after_update() end
Note that this callback is still wrapped in the transaction around +save+. For example, if you
Is called _after_ Base.save on existing objects that have a record.
def after_update() end
def after_validation() end
def after_validation() end
def after_validation_on_create() end
Is called _after_ Validations.validate (which is part of the Base.save call) on new objects
def after_validation_on_create() end
def after_validation_on_update() end
Is called _after_ Validations.validate (which is part of the Base.save call) on
def after_validation_on_update() end
def before_create() end
def before_create() end
def before_destroy() end
Note: If you need to _destroy_ or _nullify_ associated records first,
Is called _before_ Base.destroy.
def before_destroy() end
def before_save() end
def before_save() end
def before_update() end
def before_update() end
def before_validation() end
def before_validation() end
def before_validation_on_create() end
Is called _before_ Validations.validate (which is part of the Base.save call) on new objects
def before_validation_on_create() end
def before_validation_on_update() end
Is called _before_ Validations.validate (which is part of the Base.save call) on
def before_validation_on_update() end
def callback(method)
def callback(method) result = run_callbacks(method) { |result, object| false == result } if result != false && respond_to_without_attributes?(method) result = send(method) end notify(method) return result end
def create_or_update_with_callbacks #:nodoc:
def create_or_update_with_callbacks #:nodoc: return false if callback(:before_save) == false if result = create_or_update_without_callbacks callback(:after_save) end result end
def create_with_callbacks #:nodoc:
def create_with_callbacks #:nodoc: return false if callback(:before_create) == false result = create_without_callbacks callback(:after_create) result end
def destroy_with_callbacks #:nodoc:
def destroy_with_callbacks #:nodoc: return false if callback(:before_destroy) == false result = destroy_without_callbacks callback(:after_destroy) result end
def notify(method) #:nodoc:
def notify(method) #:nodoc: self.class.changed self.class.notify_observers(method, self) end
def update_with_callbacks(*args) #:nodoc:
def update_with_callbacks(*args) #:nodoc: return false if callback(:before_update) == false result = update_without_callbacks(*args) callback(:after_update) result end
def valid_with_callbacks? #:nodoc:
def valid_with_callbacks? #:nodoc: return false if callback(:before_validation) == false if new_record? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end return false if false == result result = valid_without_callbacks? callback(:after_validation) if new_record? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end return result end