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

WIP: Add ability to update image with existing data #26

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
5 changes: 5 additions & 0 deletions lib/waffle_ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ defmodule Waffle.Ecto.Schema do
{field, upload = %{__struct__: Plug.Upload}}, fields ->
[{field, {upload, scope}} | fields]

# Allow update with data from schema
{field, data = %{file_name: filename}}, fields
when is_binary(filename) ->
[{field, {data, scope}} | fields]

# Allow casting binary data structs
{field, upload = %{filename: filename, binary: binary}}, fields
when is_binary(filename) and is_binary(binary) ->
Expand Down
10 changes: 10 additions & 0 deletions test/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ defmodule WaffleTest.Ecto.Schema do
%{file_name: "file.png", updated_at: _} = cs.changes.avatar
end

test_with_mock "supports updating with already existing data", DummyDefinition,
store: fn {_, %TestUser{}} ->
{:ok, "file.png"}
end do
attrs = %{"avatar" => build_upload("/path/to/my/file.png")}
cs = TestUser.changeset(%TestUser{}, attrs)
assert {:ok, user} = Ecto.Changeset.apply_action(cs, :insert)
TestUser.changeset(user, %{"avatar" => user.avatar})
end

test_with_mock "cascades storage error into an error", DummyDefinition,
store: fn {%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"},
%TestUser{}} ->
Expand Down