From dc41746061bc5a90b79466d444d690e5a74a0a66 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Thu, 13 Jul 2023 18:21:23 -0400 Subject: [PATCH] fix: handle single types against to many forms --- lib/ash_phoenix/form/auto.ex | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/ash_phoenix/form/auto.ex b/lib/ash_phoenix/form/auto.ex index f86814d..a372e32 100644 --- a/lib/ash_phoenix/form/auto.ex +++ b/lib/ash_phoenix/form/auto.ex @@ -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 @@ -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