1

I have AASM with ActiveRecord model.

Have many statuses and events with their transitions. I want to create event that will allow transition from any state except one.

event :set_vacant_pass do
  transitions to: :vacant_pass, from: ??
end
alexey_the_cat
  • 1,812
  • 19
  • 33

2 Answers2

1

After some digging, i just decided to have simple solution:

event :set_vacant_pass do
  transitions to: :vacant_pass, from: Vacation.man_statuses.except('vacant_pass').keys
end

This will allow event transition from any state except vacant_pass.

(man_status is enum column and used for AASM)

alexey_the_cat
  • 1,812
  • 19
  • 33
0

Spitballing so syntax may not be perfect, but you can specify this explicitly using a list. Supposing your valid states to transition to "vacant_pass" from are "new", "created" and "closed":

event :set_vacant_pass do
  transitions from: [:new, :created, :closed], to: :vacant_pass
end

Or you could use a guard

event :set_vacant_pass do
  transitions_from: Vacations.man_statuses.keys, to: :vacant_pass, guard: :transition_valid?
end

def transition_valid?
  self.state == :vacant_pass
end

Added extra, you can use

ClassName.aasm.states.map(&:name)

to get an array of symbols containing all possible states for your class.

Stuart
  • 1,054
  • 9
  • 20