Skip to content

Instantly share code, notes, and snippets.

@linusdm
Created August 11, 2023 15:28
Show Gist options
  • Save linusdm/a79c241c2f9b7a1c3ac158b1aa530ca2 to your computer and use it in GitHub Desktop.
Save linusdm/a79c241c2f9b7a1c3ac158b1aa530ca2 to your computer and use it in GitHub Desktop.
Fsmx repro transition_changeset

fsmx repro

Mix.install([
  {:ecto, "~> 3.10"},
  # change to 0.4.1 to see how it worked previously
  {:fsmx, "0.5.0"}
])

Section

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", %{})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment