Skip to content

Lifecycle callbacks #30

@sitch

Description

@sitch

Would be nice to use multiple callbacks in order to support things like Ecto.Multi. So instead of just having a :callback which needs to call :validate_state_transition which expects an Ecto.Changeset, you have :before_transition and an :after_transition.

This allows you to do

  use EctoStateMachine,
    states: [:initial, :confirmed],
    events: [
      [
        name:     :confirm,
        from:     [:inital],
        to:       :confirmed,
        before_transition: fn(model, _params) -> Ecto.Changeset.change(model, expired_at: Ecto.DateTime.utc()),
        after_transition: fn(changeset, params) do
          Multi.new
          |> Multi.update(changeset)
          |> Multi.insert(TBD.changeset(%TBD{}, params)
        end
      ]
    ]

Now you can call User.confirm(params) and the params supplied will get applied to the transition functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions