Module: ActiveSupport::Callbacks

Defined in:
lib/active_support/callbacks.rb

Overview

Callbacks are hooks into the lifecycle of an object that allow you to trigger logic before or after an alteration of the object state.

Mixing in this module allows you to define callbacks in your class.

Example:

class Storage
  include ActiveSupport::Callbacks

  define_callbacks :before_save, :after_save
end

class ConfigStorage < Storage
  before_save :saving_message
  def saving_message
    puts "saving..."
  end

  after_save do |object|
    puts "saved"
  end

  def save
    run_callbacks(:before_save)
    puts "- save"
    run_callbacks(:after_save)
  end
end

config = ConfigStorage.new
config.save

Output:

saving...
- save
saved

Callbacks from parent classes are inherited.

Example:

class Storage
  include ActiveSupport::Callbacks

  define_callbacks :before_save, :after_save

  before_save :prepare
  def prepare
    puts "preparing save"
  end
end

class ConfigStorage < Storage
  before_save :saving_message
  def saving_message
    puts "saving..."
  end

  after_save do |object|
    puts "saved"
  end

  def save
    run_callbacks(:before_save)
    puts "- save"
    run_callbacks(:after_save)
  end
end

config = ConfigStorage.new
config.save

Output:

preparing save
saving...
- save
saved

Defined Under Namespace

Modules: ClassMethods Classes: Callback, CallbackChain

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



205
206
207
# File 'lib/active_support/callbacks.rb', line 205

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#run_callbacks(kind, options = {}, &block) ⇒ Object

Runs all the callbacks defined for the given options.

If a block is given it will be called after each callback receiving as arguments:

* the result from the callback
* the object which has the callback

If the result from the block evaluates to false, the callback chain is stopped.

Example:

class Storage
  include ActiveSupport::Callbacks

  define_callbacks :before_save, :after_save
end

class ConfigStorage < Storage
  before_save :pass
  before_save :pass
  before_save :stop
  before_save :pass

  def pass
    puts "pass"
  end

  def stop
    puts "stop"
    return false
  end

  def save
    result = run_callbacks(:before_save) { |result, object| result == false }
    puts "- save" if result
  end
end

config = ConfigStorage.new
config.save

Output:

pass
pass
stop


276
277
278
# File 'lib/active_support/callbacks.rb', line 276

def run_callbacks(kind, options = {}, &block)
  self.class.send("#{kind}_callback_chain").run(self, options, &block)
end