machinery
machinery copied to clipboard
Question: How would you handle extra metadata related to a transition?
We'd like to send in additional metadata related to a transition (like the current user that performed the transition, or a related record ID to close).
How would we pass that in? Currently, I don't see a field in transition_to/3
for this kind of metadata.
Thanks in advance.
🤔 good point, I don't think there is an easy way to to that, unless you add something into the struct that you could pick up later in a callback, but this might be a new feature.
I solved this in my fork shortly after writing this. I'll send you a pull request :)
@atonse I'd be very interested in the pull request as well if you have it around!
For anyone who comes to this thread via Google, there's a very simple version of this here (no tests yet so haven't submitted a PR); https://github.com/TalkingQuickly/machinery/tree/feature/transition_metadata which allows you to pass in metadata, e.g:
Machinery.transition_to(user, MyApp.UserStateMachine, "active", %{changed_by: "Someones Name"})
And then define log_transition
with arity 3 e.g. def log_transition(struct, next_state, metadata) do
and get the metadata map passed in to the log_transition
call. Should be backwards compatible because the transition_to
call just defaults to an empty map if none is provided.