asiniy / Ecto_state_machine
State machine pattern for Ecto
Stars: ✭ 91
Programming Languages
elixir
2628 projects
Labels
Projects that are alternatives of or similar to Ecto state machine
Paper trail
Track and record all the changes in your database with Ecto. Revert back to anytime in history.
Stars: ✭ 380 (+317.58%)
Mutual labels: ecto
Accent
The first developer-oriented translation tool. True asynchronous flow between translators and your team.
Stars: ✭ 721 (+692.31%)
Mutual labels: ecto
Elixir Boilerplate
⚗ The stable base upon which we build our Elixir projects at Mirego.
Stars: ✭ 627 (+589.01%)
Mutual labels: ecto
Esx
A client for the Elasticsearch with Ecto, written in Elixir
Stars: ✭ 25 (-72.53%)
Mutual labels: ecto
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (+303.3%)
Mutual labels: ecto
Nebulex
In-memory and distributed caching toolkit for Elixir.
Stars: ✭ 662 (+627.47%)
Mutual labels: ecto
Ecto materialized path
Tree structure & hierarchy for ecto models
Stars: ✭ 45 (-50.55%)
Mutual labels: ecto
Kaffy
Powerfully simple admin package for phoenix applications
Stars: ✭ 617 (+578.02%)
Mutual labels: ecto
Disco
Simple, opinionated yet flexible library to build CQRS/ES driven systems 🕺
Stars: ✭ 31 (-65.93%)
Mutual labels: ecto
Query
Query adds tools to aid the use of Ecto in web settings.
Stars: ✭ 23 (-74.73%)
Mutual labels: ecto
Filterable
Filtering from incoming params in Elixir/Ecto/Phoenix with easy to use DSL.
Stars: ✭ 83 (-8.79%)
Mutual labels: ecto
Ecto morph
morph your Ecto capabilities into the s t r a t o s p h e r e !
Stars: ✭ 72 (-20.88%)
Mutual labels: ecto
Ecto state machine
This package allows to use finite state machine pattern in Ecto. Specify:
- states
- events
- column (optional)
and go:
defmodule User do
use Web, :model
use EctoStateMachine,
states: [:unconfirmed, :confirmed, :blocked, :admin],
events: [
[
name: :confirm,
from: [:unconfirmed],
to: :confirmed,
callback: fn(model) -> Ecto.Changeset.change(model, confirmed_at: Ecto.DateTime.utc) end # yeah you can bring your own code to these functions.
], [
name: :block,
from: [:confirmed, :admin],
to: :blocked
], [
name: :make_admin,
from: [:confirmed],
to: :admin
]
]
schema "users" do
field :state, :string, default: "unconfirmed"
end
end
now you can do:
user = Repo.get_by(User, id: 1)
# Create changeset transition user state to "confirmed". We can make him admin!
confirmed_user = User.confirm(user) # =>
# We can validate ability to change user's state
User.can_confirm?(confirmed_user) # => false
User.can_make_admin?(confirmed_user) # => true
# Create changeset transition user state to "admin"
admin = User.make_admin(confirmed_user)
# Store changeset to the database
Repo.update(admin)
# List all possible states
# If column isn't `:state`, function name will be prefixed. IE,
# for column `:rules` function name will be `rules_states`
User.states # => [:unconfirmed, :confirmed, :blocked, :admin]
# List all possible events
# If column isn't `:state`, function name will be prefixed. IE,
# for column `:rules` function name will be `rules_events`
User.events # => [:confirm, :block, :make_admin]
You can check out whole test/dummy
directory to inspect how to organize sample app.
Installation
If available in Hex, the package can be installed as:
-
Add ecto_state_machine to your list of dependencies in
mix.exs
:def deps do [{:ecto_state_machine, "~> 0.1.0"}] end
Custom column name
ecto_state_machine
uses state
database column by default. You can specify
column
option to change it. Like this:
defmodule Dummy.User do
use Dummy.Web, :model
use EctoStateMachine,
column: :rules,
# bla-bla-bla
end
Now your state will be stored into rules
column.
Contributions
- Install dependencies
mix deps.get
- Setup your
config/test.exs
&config/dev.exs
- Run migrations
mix ecto.migrate
&MIX_ENV=test mix ecto.migrate
- Develop new feature
- Write new tests
- Test it:
mix test
- Open new PR!
Roadmap to 1.0
- [x] Cover by tests
- [x] Custom db column name
- [x] Validation method for changeset indicates its value in the correct range
- [x] Initial value
- [x] CI
- [x] Add status? methods
- [ ] Introduce it at elixir-radar and my blog
- [ ] Custom error messages for changeset (with translations by gettext ability)
- [x] Rely on last versions of ecto & elixir
- [ ] Write dedicated module instead of requiring everything into the model
- [ ] Write bang! methods which are raising exception instead of returning invalid changeset
- [ ] Rewrite spaghetti description in README
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].