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

Fix Waffle.Ecto.Type load and dump in embeds #44

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
1 change: 1 addition & 0 deletions lib/waffle_ecto/definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ defmodule Waffle.Ecto.Definition do
def cast(value), do: Waffle.Ecto.Type.cast(unquote(definition), value)
def load(value), do: Waffle.Ecto.Type.load(unquote(definition), value)
def dump(value), do: Waffle.Ecto.Type.dump(unquote(definition), value)
def embed_as(format), do: Waffle.Ecto.Type.embed_as(unquote(definition), format)
end

def url({%{file_name: file_name, updated_at: updated_at}, scope}, version, options) do
Expand Down
2 changes: 2 additions & 0 deletions lib/waffle_ecto/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,7 @@ defmodule Waffle.Ecto.Type do
dump(definition, %{file_name: file_name, updated_at: updated_at})
end

def embed_as(_definition, _format), do: :dump

defp log_error(error), do: Logger.error(inspect(error))
end
26 changes: 26 additions & 0 deletions test/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,30 @@ defmodule WaffleTest.Ecto.Type do
{:ok, dumped_type} = DummyDefinition.Type.dump(value)
assert dumped_type == "file.png"
end

test "dumps as embedded type to a string" do
timestamp = NaiveDateTime.from_erl!({{1970, 1, 1}, {0, 0, 0}})

assert {:ok, value} =
Ecto.Type.embedded_dump(
DummyDefinition.Type,
%{file_name: "file.png", updated_at: timestamp},
:json
)

assert value == "file.png?62167219200"
end

test "loads as embedded type from a string" do
timestamp = NaiveDateTime.from_erl!({{1970, 1, 1}, {0, 0, 0}})

assert {:ok, value} =
Ecto.Type.embedded_load(
DummyDefinition.Type,
"file.png?62167219200",
:json
)

assert value == %{file_name: "file.png", updated_at: timestamp}
end
end