gjaldon
EctoEnum for ActiveRecord-like enums in Ecto updated!
As the title states, EctoEnum has just been updated after some time of hardly any activity in the repo. Here’s the latest release: Release 0.3.2 · gjaldon/ecto_enum · GitHub
The latest release still isn’t compatible with Ecto 1.1 to 2.0. I’ll be working on that in the upcoming days. There’s been significant change in how custom Ecto types work in 1.1 so it will take a bit more work. ![]()
Would appreciate any feedback! Enjoy!
Most Liked
idi527
Just in case anyone is looking for a way to use postgres enum types with ecto, here’s how (without any additional dependencies).
defmodule Invoice.Status do
@moduledoc """
The status of payment; whether the invoice has been paid or not.
Enumeration members:
- `:payment_automatically_applied`
- `:payment_complete`
- `:payment_declined`
- `:payment_due`
- `:payment_past_due`
"""
@behaviour Ecto.Type
statuses = [
:payment_automatically_applied,
:payment_complete,
:payment_declined,
:payment_due,
:payment_past_due
] |> Enum.map(fn status ->
{status, to_string(status)}
end)
@doc "Returns the underlying schema type for the custom type"
@spec type :: :invoice_status
def type, do: :invoice_status
@doc "Casts the given input to the custom type"
@spec cast(atom | binary) :: {:ok, atom} | :error
def cast(status)
for {atom, string} <- statuses do
def cast(unquote(atom)), do: {:ok, unquote(atom)}
def cast(unquote(string)), do: {:ok, unquote(atom)}
end
def cast(_other), do: :error
@doc "Loads the given term into a custom type"
@spec load(binary) :: {:ok, atom}
def load(status)
for {atom, string} <- statuses do
def load(unquote(string)), do: {:ok, unquote(atom)}
end
@doc "Dumps the given term into an Ecto native type"
@spec dump(atom | binary) :: {:ok, binary} | :error
def dump(status)
for {atom, string} <- statuses do
def dump(unquote(atom)), do: {:ok, unquote(string)}
def dump(unquote(string)), do: {:ok, unquote(string)}
end
def dump(_other), do: :error
end
gjaldon
No doubt you could write your own code to create a custom Ecto type. The point of this small and focused library is to do the metaprogramming for you so don’t need to write that code every time you need to create an Enum in Ecto. With EctoEnum, you just need to write a few lines of code to have a StatusEnum, DayOfWeekEnum, etc… Also, this library actually has tests and is used by many other devs. That’s the point of having libraries and sharing packages, so we can focus on other work.
gjaldon
Oh and EctoEnum works with Postgres’s Enum type or a plain ol’ integer. It will have support for MySQL’s Enum type in the future too.
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









