Sha256: b98e21d556985406ff6ea7c23fb118d9bc038b2bda69569d86476f4ccc081ffb

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module StateMachine
  module Integrations #:nodoc:
    module ActiveModel
      # 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
      module Observer
        def update_with_transition(args)
          observed_method, object, transition = args
          send(observed_method, object, transition) if respond_to?(observed_method)
        end
      end
    end
  end
end

ActiveModel::Observer.class_eval do
  include StateMachine::Integrations::ActiveModel::Observer
end if defined?(ActiveModel::Observer)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
state_machine-1.1.2 lib/state_machine/integrations/active_model/observer.rb
state_machine-1.1.1 lib/state_machine/integrations/active_model/observer.rb
state_machine-1.1.0 lib/state_machine/integrations/active_model/observer.rb
state_machine-1.0.3 lib/state_machine/integrations/active_model/observer.rb