How to set up a guard at the state level in AASM
Asked Answered
A

1

7

Is there a way to define a guard (eg has_legs?) that is checked before entering a state (eg running) in any event?

In the list of all possible callbacks there are guards for events and transitions, but not for events.

I tried to do a conditional validation for the model. It worked in the event execution, but not for aasm methods (eg may_run?).

Adscititious answered 1/8, 2016 at 2:8 Comment(1)
Can you show your code?Coparcener
P
0

I don't know a build in method. I would use a workaround with a Hash containing the default options.

DEFAULT_EVENT_OPTS = { guard: ... }
DEFAULT_TRANSITION_OPTS = { ... }

event :clean, DEFAULT_EVENT_OPTS.merge({ ... }) do
  transitions DEFAULT_TRANSITION_OPTS.merge({:from => :running, :to => :cleaning, :guard => :cleaning_needed?})
end
Photopia answered 5/8, 2016 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.