To ensure state changes are saved (e.g., to a database), implement a persist/2 or persist/3 function in your state machine module.
persist/2 receives (struct, next_state).persist/3 receives (struct, next_state, extra_metadata).
Requirement: The function must return the updated struct.
Example
defmodule YourProject.UserStateMachine do
use Machinery,
states: ["created", "completed"],
transitions: %{"created" => "completed"}
def persist(struct, next_state) do
# Example: Updating a database via an external module
{:ok, user} = Accounts.update_user(struct, %{state: next_state})
user
end
end
def persist(struct, next_state) do
# Updating a user on the database with the new state.
{:ok, user} = Accounts.update_user(struct, %{state: next_state})
# `persist` should always return the updated struct
user
end