Skip to content

Commit

Permalink
fix: handle single types against to many forms
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jul 13, 2023
1 parent a47b3fc commit dc41746
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/ash_phoenix/form/auto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ defmodule AshPhoenix.Form.Auto do
Keyword.put(
opts,
:data,
relationship_fetcher(relationship, auto_opts[:relationship_fetcher])
relationship_fetcher(relationship, auto_opts[:relationship_fetcher], opts[:type])
)
else
opts
Expand Down Expand Up @@ -481,19 +481,25 @@ defmodule AshPhoenix.Form.Auto do
end
end

defp relationship_fetcher(relationship, relationship_fetcher) do
defp relationship_fetcher(relationship, relationship_fetcher, type) do
fn parent ->
if relationship_fetcher do
relationship_fetcher.(parent, relationship)
else
case Map.get(parent, relationship.name) do
%Ash.NotLoaded{} ->
if relationship.cardinality == :many do
if type == :single do
nil
else
[]
end

value ->
value
if type == :single && is_list(value) do
Enum.at(value, 0)
else
value
end
end
end
end
Expand Down

0 comments on commit dc41746

Please sign in to comment.