Skip to content

Instantly share code, notes, and snippets.

@krainboltgreene
Created January 2, 2020 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krainboltgreene/77ae63012ee4c20cb8f18c7024a21cc0 to your computer and use it in GitHub Desktop.
Save krainboltgreene/77ae63012ee4c20cb8f18c7024a21cc0 to your computer and use it in GitHub Desktop.
defmodule Poutineer.Models.Account do
# ...
import Estate, only: [machine: 1]
# ...
machine([
onboarding_state: [complete: [converted: "completed"]],
role_state: [
grant_moderation_powers: [user: "moderator"],
grant_administrator_powers: [user: "administrator", moderator: "administrator"]
]
])
# ...
end
defmodule Estate do
defmacro machine(machines) do
Enum.flat_map(machines, fn ({column_name, events}) ->
Enum.flat_map(events, fn ({event_name, transitions}) ->
Enum.map(transitions, fn ({from, to}) ->
quote do
@desc "A before hook"
def unquote(:"before_#{event_name}")(%Ecto.Changeset{} = record) do
record
end
@desc "An after hook"
def unquote(:"after_#{event_name}")(%{} = record) do
record
end
@desc "An action to change the state, if the transition matches, but doesn't save"
def unquote(event_name)(%{unquote(column_name) => unquote(Atom.to_string(from))} = record) do
record
|> Ecto.Changeset.change()
|> unquote(:"before_#{event_name}")()
|> Ecto.Changeset.cast(%{unquote(column_name) => unquote(to)}, [unquote(column_name)])
|> Ecto.Changeset.validate_required(unquote(column_name))
|> unquote(:"after_#{event_name}")()
end
@desc "An action to change the state, if the transition matches, but does save"
def unquote(:"#{event_name}!")(%{:id => id, unquote(column_name) => unquote(Atom.to_string(from))} = record) when not is_nil(id) do
record
|> Ecto.Changeset.change()
|> unquote(:"before_#{event_name}")()
|> Ecto.Changeset.cast(%{unquote(column_name) => unquote(to)}, [unquote(column_name)])
|> Ecto.Changeset.validate_required(unquote(column_name))
|> Poutineer.Repo.update()
|> unquote(:"after_#{event_name}")()
end
# If you can't match, then raise transition failure
end
end)
end)
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment