Skip to content

Commit

Permalink
Merge pull request #97 from dwyl/custom-atom-ecto-type-#93
Browse files Browse the repository at this point in the history
Custom atom ecto type
  • Loading branch information
nelsonic authored Jul 13, 2022
2 parents 2a33aa7 + 017119d commit e70f48d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
16 changes: 16 additions & 0 deletions lib/app/ecto_atom.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule App.EctoAtom do
@moduledoc """
Custom Ecto type for atom
"""
use Ecto.Type

def type, do: :string

def cast(value) when is_atom(value), do: {:ok, value}
def cast(_), do: :error

def load(value), do: {:ok, String.to_atom(value)}

def dump(value) when is_atom(value), do: {:ok, Atom.to_string(value)}
def dump(_), do: :error
end
3 changes: 1 addition & 2 deletions lib/app/status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule App.Status do
alias __MODULE__

schema "status" do
field :text, :string
field :text, App.EctoAtom
field :status_code, :integer

timestamps()
Expand Down Expand Up @@ -34,5 +34,4 @@ defmodule App.Status do
{:ok, status}
end
end

end
6 changes: 2 additions & 4 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ alias App.{Person, Status}

# Statuses: github.com/dwyl/statuses
Statuses.get_statuses()
|> Enum.each(fn(s) ->
|> Enum.each(fn s ->
Status.upsert(%{
text: Atom.to_string(s.text)
|> String.split("_", trim: true)
|> Enum.join(" "),
text: s.text,
status_code: s.code
})
end)
Expand Down
10 changes: 4 additions & 6 deletions test/app/status_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ defmodule App.StatusTest do
alias App.Status

describe "status" do
@valid_attrs %{text: "some text"}
@valid_attrs %{text: :some_text}
# @update_attrs %{text: "some updated text"}
@invalid_attrs %{text: nil}


def status_fixture(attrs \\ %{}) do
{:ok, status} =
attrs
Expand All @@ -19,7 +18,7 @@ defmodule App.StatusTest do

test "create/1 with valid data creates a status" do
{:ok, status} = Status.create(@valid_attrs)
assert status.text == "some text"
assert status.text == :some_text
end

test "create/1 with invalid data returns error changeset" do
Expand All @@ -28,16 +27,15 @@ defmodule App.StatusTest do

test "upsert/1 with valid data returns status" do
{:ok, status} = Status.upsert(@valid_attrs)
assert status.text == "some text"
assert status.text == :some_text

# confirm that upsert/1 does not re-create the same status:
{:ok, status_again} = Status.upsert(@valid_attrs)
assert status_again.id == status.id
end


test "upsert/1 with new data returns the new status" do
new_status = %{text: "Hello Simon!"}
new_status = %{text: :Hello_Simon!}
{:ok, status} = Status.upsert(new_status)
assert status.text == new_status.text
end
Expand Down

0 comments on commit e70f48d

Please sign in to comment.