Module: StateMachine::Integrations::ActiveModel::Observer

Defined in:
lib/state_machine/integrations/active_model/observer.rb

Overview

Adds support for invoking callbacks on ActiveModel observers with more than one argument (e.g. the record and the state transition). By default, ActiveModel only supports passing the record into the callbacks.

For example:

class VehicleObserver < ActiveModel::Observer
  # The default behavior: only pass in the record
  def after_save(vehicle)
  end

  # Custom behavior: allow the transition to be passed in as well
  def after_transition(vehicle, transition)
    Audit.log(vehicle, transition)
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



22
23
24
25
26
27
# File 'lib/state_machine/integrations/active_model/observer.rb', line 22

def self.included(base) #:nodoc:
  base.class_eval do
    alias_method :update_without_multiple_args, :update
    alias_method :update, :update_with_multiple_args
  end
end

Instance Method Details

#update_with_multiple_args(observed_method, object, *args) ⇒ Object

Allows additional arguments other than the object to be passed to the observed methods



31
32
33
34
35
36
37
# File 'lib/state_machine/integrations/active_model/observer.rb', line 31

def update_with_multiple_args(observed_method, object, *args) #:nodoc:
  if args.any?
    send(observed_method, object, *args) if respond_to?(observed_method)
  else
    update_without_multiple_args(observed_method, object)
  end
end