module StateMachines::Integrations::ActiveModel
Public Class Methods
Classes that include ActiveModel::Observing will automatically use the ActiveModel
integration.
# File lib/state_machines/integrations/active_model/observers.rb, line 11 def self.matching_ancestors [::ActiveModel, ::ActiveModel::Observing, ::ActiveModel::Validations] end
Public Instance Methods
# File lib/state_machines/integrations/active_model/observers.rb, line 60 def add_callback(type, options, &block) options[:terminator] = callback_terminator @callbacks[type == :around ? :before : type].insert(-2, callback = StateMachines::Callback.new(type, options, &block)) add_states(callback.known_states) callback end
Adds a set of default callbacks that utilize the Observer
extensions
# File lib/state_machines/integrations/active_model/observers.rb, line 16 def add_default_callbacks callbacks[:before] << StateMachines::Callback.new(:before) { |object, transition| notify(:before, object, transition) } callbacks[:after] << StateMachines::Callback.new(:after) { |object, transition| notify(:after, object, transition) } callbacks[:failure] << StateMachines::Callback.new(:failure) { |object, transition| notify(:after_failure_to, object, transition) } end
Initializes class-level extensions and defaults for this machine
# File lib/state_machines/integrations/active_model/observers.rb, line 68 def after_initialize super() add_default_callbacks end
Notifies observers on the given object that a callback occurred involving the given transition. This will attempt to call the following methods on observers:
-
#{type}_#{qualified_event}from#{from}to#{to}
-
#{type}_#{qualified_event}from#{from}
-
#{type}_#{qualified_event}to#{to}
-
#{type}_#{qualified_event}
-
#{type}transition#{machine_name}from#{from}to#{to}
-
#{type}transition#{machine_name}from#{from}
-
#{type}transition#{machine_name}to#{to}
-
#{type}transition#{machine_name}
-
#{type}_transition
This will always return true regardless of the results of the callbacks.
# File lib/state_machines/integrations/active_model/observers.rb, line 37 def notify(type, object, transition) name = self.name event = transition.qualified_event from = transition.from_name || 'nil' to = transition.to_name || 'nil' # Machine-specific updates ["#{type}_#{event}", "#{type}_transition_#{name}"].each do |event_segment| ["_from_#{from}", nil].each do |from_segment| ["_to_#{to}", nil].each do |to_segment| object.class.changed if object.class.respond_to?(:changed) object.class.notify_observers('update_with_transition', ObserverUpdate.new([event_segment, from_segment, to_segment].join, object, transition)) end end end # Generic updates object.class.changed if object.class.respond_to?(:changed) object.class.notify_observers('update_with_transition', ObserverUpdate.new("#{type}_transition", object, transition)) true end