machinery icon indicating copy to clipboard operation
machinery copied to clipboard

Question: How would you handle extra metadata related to a transition?

Open atonse opened this issue 5 years ago • 4 comments

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.

atonse avatar Jun 21 '19 19:06 atonse

🤔 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.

joaomdmoura avatar Oct 25 '19 14:10 joaomdmoura

I solved this in my fork shortly after writing this. I'll send you a pull request :)

atonse avatar Oct 25 '19 14:10 atonse

@atonse I'd be very interested in the pull request as well if you have it around!

TalkingQuickly avatar Jun 17 '20 15:06 TalkingQuickly

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.

TalkingQuickly avatar Jun 17 '20 16:06 TalkingQuickly