Sha256: b3b3a74454aaff109ed28e3ff8b15f584feed87cc47121dccfcd0562d58a1c6e

Contents?: true

Size: 1.38 KB

Versions: 31

Compression:

Stored size: 1.38 KB

Contents

module StateMachine
  module Integrations #:nodoc:
    module ActiveRecord
      # Adds support for invoking callbacks on ActiveRecord observers with more
      # than one argument (e.g. the record *and* the state transition).  By
      # default, ActiveRecord only supports passing the record into the
      # callbacks.
      # 
      # For example:
      # 
      #   class VehicleObserver < ActiveRecord::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 self.included(base) #:nodoc:
          base.class_eval do
            alias_method :update_without_multiple_args, :update
            alias_method :update, :update_with_multiple_args
          end
        end
        
        # Allows additional arguments other than the object to be passed to the
        # observed methods
        def update_with_multiple_args(observed_method, object, *args) #:nodoc:
          send(observed_method, object, *args) if respond_to?(observed_method)
        end
      end
    end
  end
end

ActiveRecord::Observer.class_eval do
  include StateMachine::Integrations::ActiveRecord::Observer
end

Version data entries

31 entries across 31 versions & 5 rubygems

Version Path
joelind-state_machine-0.8.1 lib/state_machine/integrations/active_record/observer.rb
pluginaweek-state_machine-0.7.6 lib/state_machine/integrations/active_record/observer.rb
pluginaweek-state_machine-0.8.0 lib/state_machine/integrations/active_record/observer.rb
branston-0.6.1 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.6.0 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.5.2 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.5.1 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.5.0 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
mattscilipoti-state_machine-0.8.0.1 lib/state_machine/integrations/active_record/observer.rb
branston-0.4.6 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.4.5 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.4.4 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.4.3 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.4.2 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.4.1 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
branston-0.4.0 lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb
state_machine-0.8.0 lib/state_machine/integrations/active_record/observer.rb
state_machine-0.7.6 lib/state_machine/integrations/active_record/observer.rb
state_machine-0.6.0 lib/state_machine/integrations/active_record/observer.rb
state_machine-0.5.2 lib/state_machine/integrations/active_record/observer.rb