Skip to content

Commit f3ff91f

Browse files
committed
Handle when changes in the changeset is nil
1 parent 2a760b7 commit f3ff91f

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

lib/upload/multi.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ defmodule Upload.Multi do
118118

119119
record_changeset =
120120
record
121-
|> Ecto.Changeset.cast(%{field => Map.get(changeset.params, to_string(field))}, [])
121+
|> Ecto.Changeset.cast(%{field => Map.get(changeset.params || %{}, to_string(field))}, [])
122122
|> Upload.Changeset.cast_attachment(field,
123123
key_function: fn _ ->
124124
key_function.(record)

test/support/data_case.ex

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule Upload.DataCase do
99

1010
using do
1111
quote do
12+
use Upload.Testing
13+
1214
import Upload.DataCase
1315
import Mock
1416
end
@@ -25,10 +27,6 @@ defmodule Upload.DataCase do
2527
Application.put_env(:upload, Upload.Storage, adapter: adapter, base_url: "http://example.com")
2628
end
2729

28-
def list_uploaded_keys do
29-
Enum.to_list(Upload.Storage.list!())
30-
end
31-
3230
def errors_on(changeset) do
3331
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
3432
Enum.reduce(opts, message, fn {key, value}, acc ->

test/upload/multi_test.exs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
defmodule Upload.MultiTest do
22
use Upload.DataCase
3-
use Upload.Testing
43

54
alias Upload.Storage
65
alias Upload.Test.Person
@@ -30,6 +29,18 @@ defmodule Upload.MultiTest do
3029

3130
assert errors_on(changeset)[:avatar] == ["is not a supported file type"]
3231
end
32+
33+
test "works when provided a changeset with nil changes" do
34+
changeset = %{Person.changeset(%Person{}) | params: nil}
35+
36+
{:ok, _} =
37+
Ecto.Multi.new()
38+
|> Ecto.Multi.insert(:insert_person, changeset)
39+
|> Upload.Multi.handle_changes(:upload_avatar, :insert_person, changeset, :avatar,
40+
key_function: &key_function/1
41+
)
42+
|> Repo.transaction()
43+
end
3344
end
3445

3546
describe "create_variant" do

0 commit comments

Comments
 (0)