Skip to content

Commit

Permalink
Merge pull request #106 from clinejj/patch-1
Browse files Browse the repository at this point in the history
Add nil check to cast_polymorphic_embeds_many
  • Loading branch information
mathieuprog authored Jul 18, 2024
2 parents c57a335 + cef782e commit 68e54e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit 68e54e0

Please sign in to comment.