Class StateMachine::TransitionCollection
In: lib/state_machine/transition_collection.rb
Parent: Array

Represents a collection of transitions in a state machine

Methods

new   perform  

Included Modules

Assertions

Attributes

skip_actions  [R]  Whether to skip running the action for each transition‘s machine
skip_after  [R]  Whether to skip running the after callbacks
use_transaction  [R]  Whether transitions should wrapped around a transaction block

Public Class methods

Creates a new collection of transitions that can be run in parallel. Each transition must be for a different attribute.

Configuration options:

  • :actions - Whether to run the action configured for each transition
  • :after - Whether to run after callbacks
  • :transaction - Whether to wrap transitions within a transaction

[Source]

    # File lib/state_machine/transition_collection.rb, line 22
22:     def initialize(transitions = [], options = {})
23:       super(transitions)
24:       
25:       # Determine the validity of the transitions as a whole
26:       @valid = all?
27:       reject! {|transition| !transition}
28:       
29:       attributes = map {|transition| transition.attribute}.uniq
30:       raise ArgumentError, 'Cannot perform multiple transitions in parallel for the same state machine attribute' if attributes.length != length
31:       
32:       assert_valid_keys(options, :actions, :after, :transaction)
33:       options = {:actions => true, :after => true, :transaction => true}.merge(options)
34:       @skip_actions = !options[:actions]
35:       @skip_after = !options[:after]
36:       @use_transaction = options[:transaction]
37:     end

Public Instance methods

Runs each of the collection‘s transitions in parallel.

All transitions will run through the following steps:

  1. Before callbacks
  2. Persist state
  3. Invoke action
  4. After callbacks (if configured)
  5. Rollback (if action is unsuccessful)

If a block is passed to this method, that block will be called instead of invoking each transition‘s action.

[Source]

    # File lib/state_machine/transition_collection.rb, line 50
50:     def perform(&block)
51:       reset
52:       
53:       if valid?
54:         if use_event_attributes? && !block_given?
55:           each do |transition|
56:             transition.transient = true
57:             transition.machine.write(object, :event_transition, transition)
58:           end
59:           
60:           run_actions
61:         else
62:           within_transaction do
63:             catch(:halt) { run_callbacks(&block) }
64:             rollback unless success?
65:           end
66:         end
67:       end
68:       
69:       if actions.length == 1 && results.include?(actions.first)
70:         results[actions.first]
71:       else
72:         success?
73:       end
74:     end

[Validate]