Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fire_state_event! #66

Open
gagalago opened this issue May 2, 2018 · 3 comments
Open

fire_state_event! #66

gagalago opened this issue May 2, 2018 · 3 comments

Comments

@gagalago
Copy link
Contributor

gagalago commented May 2, 2018

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?

@oehlschl
Copy link

oehlschl commented Jun 26, 2018

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.

@oehlschl
Copy link

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

@oehlschl
Copy link

oehlschl commented Jun 26, 2018

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants