Mix.install([
{:ecto, "~> 3.10"},
# change to 0.4.1 to see how it worked previously
{:fsmx, "0.5.0"}
])
defmodule TestSchema do
use Ecto.Schema
import Ecto.Changeset
schema "state_machine" do
field(:state, :string, default: "one")
field(:data, :map)
end
use Fsmx.Struct,
transitions: %{
"one" => ["two", "three"]
}
def transition_changeset(changeset, "one", "two", params) do
changeset
|> cast(params, [:data])
|> validate_required([:data])
end
# There is no transition_changeset/4 defined for transition "one" => "three" on purpose.
end
struct = %TestSchema{state: "one"}
Transitioning to state "two" works as expected
%{valid?: true} = Fsmx.transition_changeset(struct, "two", %{"data" => %{foo: :bar}})
Transitioning to "three" doesn't work anymore, as there is no transition_changeset/4 callback defined. Although this worked previously.
%{valid?: true} = Fsmx.transition_changeset(struct, "three", %{})