Search code examples
rubyaasm

How to skip a state in a state machine using AASM


I'm using the ruby AASM gem.

Does anyone know what the right way to skip a state is?

class Job
  # ...

  event :stage1_completed do
    if stage2_completed?
      transitions from: :stage1, :to => :stage3
    else
      transitions from: :stage1, :to => :stage2
    end
  end

  # ...
end

What is the best way to set this up in AASM?

I am using this code in a set of resque jobs, thus stage1 is a resque job, which then updates the state and starts the next resque job be done. Same for stage2, then stage3


Solution

  • You could use guards.

    event :stage1_completed do
        transitions from: :stage1, :to => :stage3, :guard => :stage2_completed? 
    end