Module: ActiveModel::Callbacks

### Home Classes Methods

### In Files

- lib/active_model/callbacks.rb

### Methods

- ::extended
- #define_model_callbacks

### Included Modules

- ActiveSupport::Callbacks

### Class/Module Index

Quicksearch

- ActiveModel
- ActiveModel::AttributeMethods
- ActiveModel::AttributeMethods::ClassMethods
- ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher
- ActiveModel::BlockValidator
- ActiveModel::Callbacks
- ActiveModel::Conversion
- ActiveModel::Dirty
- ActiveModel::EachValidator
- ActiveModel::Errors
- ActiveModel::Lint
- ActiveModel::Lint::Tests
- ActiveModel::MassAssignmentSecurity
- ActiveModel::MassAssignmentSecurity::BlackList
- ActiveModel::MassAssignmentSecurity::ClassMethods
- ActiveModel::MassAssignmentSecurity::Error
- ActiveModel::MassAssignmentSecurity::LoggerSanitizer
- ActiveModel::MassAssignmentSecurity::PermissionSet
- ActiveModel::MassAssignmentSecurity::Sanitizer
- ActiveModel::MassAssignmentSecurity::StrictSanitizer
- ActiveModel::MassAssignmentSecurity::WhiteList
- ActiveModel::MissingAttributeError
- ActiveModel::Name
- ActiveModel::Naming
- ActiveModel::Observer
- ActiveModel::ObserverArray
- ActiveModel::Observing
- ActiveModel::Observing::ClassMethods
- ActiveModel::SecurePassword
- ActiveModel::SecurePassword::ClassMethods
- ActiveModel::SecurePassword::InstanceMethodsOnActivation
- ActiveModel::Serialization
- ActiveModel::Serializers
- ActiveModel::Serializers::JSON
- ActiveModel::Serializers::Xml
- ActiveModel::StrictValidationFailed
- ActiveModel::Translation
- ActiveModel::Validations
- ActiveModel::Validations::AcceptanceValidator
- ActiveModel::Validations::Callbacks
- ActiveModel::Validations::Callbacks::ClassMethods
- ActiveModel::Validations::ClassMethods
- ActiveModel::Validations::ConfirmationValidator
- ActiveModel::Validations::ExclusionValidator
- ActiveModel::Validations::FormatValidator
- ActiveModel::Validations::HelperMethods
- ActiveModel::Validations::InclusionValidator
- ActiveModel::Validations::LengthValidator
- ActiveModel::Validations::NumericalityValidator
- ActiveModel::Validations::PresenceValidator
- ActiveModel::Validations::WithValidator
- ActiveModel::Validator

No matching classes.

# ActiveModel::Callbacks

## Active Model Callbacks

Provides an interface for any class to have Active Record like callbacks.

Like the Active Record methods, the callback chain is aborted as soon as one of the methods in the chain returns false.

First, extend ActiveModel::Callbacks from the class you are creating:

    class MyModel
      extend ActiveModel::Callbacks
    end

Then define a list of methods that you want callbacks attached to:

    define_model_callbacks :create, :update

This will provide all three standard callbacks (before, around and after) for both the :create and :update methods. To implement, you need to wrap the methods you want callbacks on in a block so that the callbacks get a chance to fire:

    def create
      run_callbacks :create do
        # Your create action methods here
      end
    end

Then in your class, you can use the `before_create`, `after_create` and `around_create` methods, just as you would in an Active Record module.

    before_create :action_before_create

    def action_before_create
      # Your code here
    end

You can choose not to have all three callbacks by passing a hash to the define_model_callbacks method.

    define_model_callbacks :create, :only => [:after, :before]

Would only create the after_create and before_create callback methods in your class.

### Public Class Methods

extended(base) click to toggle source

    # File lib/active_model/callbacks.rb, line 49
    def self.extended(base)
      base.class_eval do
        include ActiveSupport::Callbacks
      end
    end

### Public Instance Methods

define_model_callbacks(*callbacks) click to toggle source

define_model_callbacks accepts the same options define_callbacks does, in case you want to overwrite a default. Besides that, it also accepts an :only option, where you can choose if you want all types (before, around or after) or just some.

    define_model_callbacks :initializer, :only => :after

Note, the `:only => <type>` hash will apply to all callbacks defined on that method call. To get around this you can call the define_model_callbacks method as many times as you need.

    define_model_callbacks :create, :only => :after
    define_model_callbacks :update, :only => :before
    define_model_callbacks :destroy, :only => :around

Would create `after_create`, `before_update` and `around_destroy` methods only.

You can pass in a class to before_<type>, after_<type> and around_<type>, in which case the callback will call that class's <action>_<type> method passing the object that the callback is being called on.

    class MyModel
      extend ActiveModel::Callbacks
      define_model_callbacks :create

      before_create AnotherClass
    end

    class AnotherClass
      def self.before_create( obj )
        # obj is the MyModel instance that the callback is being called on
      end
    end

    # File lib/active_model/callbacks.rb, line 88
    def define_model_callbacks(*callbacks)
      options = callbacks.extract_options!
      options = {
         :terminator => "result == false",
         :scope => [:kind, :name],
         :only => [:before, :around, :after]
      }.merge(options)

      types = Array.wrap(options.delete(:only))

      callbacks.each do |callback|
        define_callbacks(callback, options)

        types.each do |type|
          send("_define_#{type}_model_callback", self, callback)
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.