Skip to content

Commit cef782e

Browse files
committed
optional set list_data_for_field
1 parent 6c4c080 commit cef782e

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

lib/polymorphic_embed.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ defmodule PolymorphicEmbed do
372372
type_field_name: type_field_name
373373
} = field_opts
374374

375-
list_data_for_field = Map.fetch!(changeset.data, field)
375+
list_data_for_field = Map.fetch!(changeset.data, field) || []
376376

377377
embeds =
378378
Enum.map(list_params, fn params ->
@@ -388,17 +388,13 @@ defmodule PolymorphicEmbed do
388388

389389
module ->
390390
data_for_field =
391-
if list_data_for_field do
392-
Enum.find(list_data_for_field, fn
393-
%{id: id} = datum when not is_nil(id) ->
394-
id == params[:id] and datum.__struct__ == module
395-
396-
_ ->
397-
nil
398-
end)
399-
else
400-
nil
401-
end
391+
Enum.find(list_data_for_field, fn
392+
%{id: id} = datum when not is_nil(id) ->
393+
id == params[:id] and datum.__struct__ == module
394+
395+
_ ->
396+
nil
397+
end)
402398

403399
embed_changeset =
404400
if data_for_field do

0 commit comments

Comments
 (0)