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

Add nil check to cast_polymorphic_embeds_many #106

Merged
merged 3 commits into from
Jul 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/polymorphic_embed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ defmodule PolymorphicEmbed do
type_field_name: type_field_name
} = field_opts

list_data_for_field = Map.fetch!(changeset.data, field)
list_data_for_field = Map.fetch!(changeset.data, field) || []

embeds =
Enum.map(list_params, fn params ->
Expand Down
22 changes: 22 additions & 0 deletions test/polymorphic_embed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,7 @@ defmodule PolymorphicEmbedTest do
address: "address"
}
],
contexts2: nil,
contexts3: [
%{
__type__: "device",
Expand Down Expand Up @@ -1784,6 +1785,27 @@ defmodule PolymorphicEmbedTest do
]
}

assert {:ok, _} =
reminder
|> reminder_module.changeset(attrs)
|> Repo.update()

# Make sure it works for embeds with nil entries
attrs = %{
contexts2: [
%{
__type__: "device",
ref: "12345",
type: "cellphone"
},
%{
__type__: "device",
ref: "56789",
type: "laptop"
}
]
}

assert {:ok, _} =
reminder
|> reminder_module.changeset(attrs)
Expand Down
Loading