state_machines icon indicating copy to clipboard operation
state_machines copied to clipboard

fire_state_event!

Open gagalago opened this issue 6 years ago • 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?

gagalago avatar May 02 '18 13:05 gagalago

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: https://github.com/state-machines/state_machines/issues/36 (very dated, but result of a quick search)~~

Hope this helps.

oehlschl avatar Jun 26 '18 04:06 oehlschl

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

oehlschl avatar Jun 26 '18 05:06 oehlschl

Found the implementation of implicit / attribute-based state transitions here: https://github.com/state-machines/state_machines/blob/c94d6908ff2a005604da60bb51f0f41187e006bb/lib/state_machines/machine.rb#L1051-L1051

It looks like there are some caveats with implicit transitions documented elsewhere: https://github.com/state-machines/state_machines/blob/c94d6908ff2a005604da60bb51f0f41187e006bb/lib/state_machines/machine.rb#L1204-L1204

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: https://github.com/state-machines/state_machines/blob/a8e18f614d81c024437a87f12f091e6b0c62a5f8/lib/state_machines/event.rb#L224-L224

oehlschl avatar Jun 26 '18 05:06 oehlschl