ecto_state_machine icon indicating copy to clipboard operation
ecto_state_machine copied to clipboard

Support event calls on Ecto.Changesets

Open marcelotto opened this issue 7 years ago • 0 comments

We have a use case in which we want to call an EctoStateMachine event on an existing changeset of the model with the state machine. I could achieve this with the few changes in this pull request.

So, with this PR the following is possible:

some_model
|> change(foo: "bar")
|> some_event()

The state machine could then define a changeset as a callback, eg. to ensure various preconditions for the state transition:

defmodule SomeModel do
  use EctoStateMachine,
    states: [:some_state, :some_other_state],
    events: [
      [
        name:     :some_event,
        from:     [:some_state],
        to:       :some_other_state,
        callback: &some_event_changeset/1
      ]
    ]

  def some_event_changeset(changeset)
    validate_required(changeset, [:foo])
  end
end

marcelotto avatar Feb 15 '18 13:02 marcelotto