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: https://github.com/gjaldon/ecto_enum/releases/tag/0.3.2

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. :slight_smile:

Would appreciate any feedback! Enjoy!

4 Likes

I’m looking forward. I skipped by EctoEnum because of lack of Ecto2 support so I will look at it again here soon.

It would be nice if it had a mode where it could work with native PostgreSQL Enum types as I am already using those in my app and it would be nice to have better checks for it. :slight_smile:

I feel like this example:

iex> Repo.insert!(%User{status: :none})
** (Elixir.EctoEnum.Error) :none is not a valid enum value

Should include the enum type in the error message. Something like: :none is not a valid enum value for StatusEnum

But good work! Would like to see this working for Ecto 2.0 as well!

Thanks for the suggestion! I’ll look into that some more once I work on Ecto2 support. I’ll keep you posted once Ecto 2 support is in. :slight_smile:

1 Like

That definitely makes the error message clearer! Added an issue for it at https://github.com/gjaldon/ecto_enum/issues/13 :smiley:

EctoEnum now supports Postgres’ Enumerated Type :smiley: Thanks for the suggestion @OvermindDL1!

Ecto 2.0 should be out this week too :slight_smile:

1 Like

Whoo!

Very Whoo!

1 Like

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
9 Likes

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.

3 Likes

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.

2 Likes

@gjaldon Thanks for your package. I’ve tried this recently and it works well. If there’s one feedback that I can give it would have to do with the ordering of the sections in the README, it has a Usage, a Reflection and then a Using Postgres’s Enum type. For the first 1 hour or so, I couldn’t get this working for me as I didn’t bother reading through the whole README, especially after seeing the Reflection part. After reading a little further I realized this is actually a reflection function and not a reflection as in conclusion thing.

Only after digging further, I found the relevant info wrt postgres and it works well :slight_smile: Thank you for your work on this.

May I ask you, how can I use this package to add more types to an existing enum in my app given that we’ve already built the initial enum using the ecto enum package. I don’t see any documentation on this and would be great to have some documentation around this as there is a possibility that we may add more types to an existing Enum in our project. I’ve created a relevant issue on github around this with some more info

Thank you.

isn’t it in the readme already?

defmodule MyApp.Repo.Migrations.AddToGenderEnum do
  use Ecto.Migration
  @disable_ddl_transaction true

  def up do
    Ecto.Migration.execute "ALTER TYPE gender ADD VALUE 'other'"
  end

  def down do
  end
end
2 Likes