Eventful.Transitable (eventful v3.0.1)
This module is the one you'll use to define your main schema which require the state machine.
For example imagine the following:
defmodule MyApp.Post do
use Ecto.Schema
import Ecto.Changeset
schema "posts" do
field :current_state, :string, default: "created"
end
end
You can add a state machine like this assuming you've setup the Event
module already:
defmodule MyApp.Post do
use Ecto.Schema
import Ecto.Changeset
use Eventful.Transitable
alias __MODULE__.Event
alias __MODULE__.Transitions
Transitions
|> governs(:current_state, on: Event)
schema "posts" do
field :current_state, :string, default: "created"
end
end
You can also setup multiple state machines with multiple fields:
defmodule MyApp.Post do
use Ecto.Schema
import Ecto.Changeset
use Eventful.Transitable
alias __MODULE__.Event
alias __MODULE__.Transitions
alias __MODULE__.Visibilities
# You can optionally use locks
Transitions
|> governs(:current_state, on: Event, lock: :current_state_version)
Visibilities
|> governs(:visibility, on: Event)
schema "posts" do
field :current_state, :string, default: "created"
field :current_state_version, :integer, default: 0
field :visibility, :string, default: "private"
end
end
Link to this section Summary
Functions
The governs function allows you to define governance on each of your fields.
Link to this section Functions
The governs function allows you to define governance on each of your fields.
Transitions
|> governs(:current_state, on: Event)