Skip to content

Commit

Permalink
optional set list_data_for_field
Browse files Browse the repository at this point in the history
  • Loading branch information
clinejj committed Jul 18, 2024
1 parent 6c4c080 commit cef782e
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions 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 All @@ -388,17 +388,13 @@ defmodule PolymorphicEmbed do

module ->
data_for_field =
if list_data_for_field do
Enum.find(list_data_for_field, fn
%{id: id} = datum when not is_nil(id) ->
id == params[:id] and datum.__struct__ == module

_ ->
nil
end)
else
nil
end
Enum.find(list_data_for_field, fn
%{id: id} = datum when not is_nil(id) ->
id == params[:id] and datum.__struct__ == module

_ ->
nil
end)

embed_changeset =
if data_for_field do
Expand Down

0 comments on commit cef782e

Please sign in to comment.