Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle callbacks #30

Open
sitch opened this issue Mar 29, 2017 · 0 comments
Open

Lifecycle callbacks #30

sitch opened this issue Mar 29, 2017 · 0 comments

Comments

@sitch
Copy link

sitch commented Mar 29, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant