Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support casting from string in embed #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/arc_ecto/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ defmodule Arc.Ecto.Type do
def cast(_definition, %{"file_name" => file, "updated_at" => updated_at}) do
{:ok, %{file_name: file, updated_at: updated_at}}
end
def cast(definition, value) when is_bitstring(value) do
load(definition, value)
end
def cast(definition, args) do
case definition.store(args) do
{:ok, file} -> {:ok, %{file_name: file, updated_at: Ecto.DateTime.utc}}
Expand Down
6 changes: 6 additions & 0 deletions test/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ defmodule ArcTest.Ecto.Type do
{:ok, dumped_type} = DummyDefinition.Type.dump(value)
assert dumped_type == "file.png"
end

test "casts embed value" do
{:ok, value} = DummyDefinition.Type.cast("file.png?62167219200")
assert value.file_name == "file.png"
assert value.updated_at == Ecto.DateTime.cast!({{1970, 1, 1}, {0, 0, 0}})
end
end