state-machines/state_machines

fire_state_event!

Opened this issue · 3 comments

I can't find the bang version of fire_state_event (throw error instead of returning false). What is the best way to add this feature?

We've been using the following pattern:

# for state machine on attribute 'status' and some event_name
my_model.update_attributes!(status_event: event_name)

This raises an exception, essentially triggering the bang version of the event. Apparently this may be part of the ActiveRecord extension, though: #36 (very dated, but result of a quick search)

Hope this helps.

Still looking for the implementation, but there's some documentation here: https://github.com/pluginaweek/state_machine#explicit-vs-implicit-event-transitions

Found the implementation of implicit / attribute-based state transitions here:

# Vehicle.state_machine.read(vehicle, :event) # => nil # Equivalent to vehicle.state_event

It looks like there are some caveats with implicit transitions documented elsewhere:

# Third, any behavior defined in this method will *not* get executed if

I didn't see any support for passing arguments (ex: an attribute like state_event_arguments), and adding this as an additional attribute seems messy.

Therefore I would also be in favor of defining a .fire!() method, possibly via a special argument to .fire(), and rewriting Event.add_actions to leverage that instead:

object.send(qualified_name, *args) || raise(StateMachines::InvalidTransition.new(object, machine, name))