Daniel
Daniel

Reputation: 7172

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

Upvotes: 0

Views: 872

Answers (1)

rohit89
rohit89

Reputation: 5773

You could use guards.

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

Upvotes: 1

Related Questions