fix: handle unions on initial form creation

closes #154
This commit is contained in:
Zach Daniel 2024-04-19 14:21:13 +01:00
parent 676015a6b7
commit 40b3f63dd3
3 changed files with 57 additions and 40 deletions

View file

@ -151,7 +151,8 @@ defmodule AshPhoenix.Form.Auto do
end end
updater = fn opts, data, params -> updater = fn opts, data, params ->
{type, constraints, _tag} = determine_type(constraints, data, params) {type, constraints, _tag} =
determine_type(constraints, data, params)
{embed, constraints, fake_embedded?} = {embed, constraints, fake_embedded?} =
if Ash.Type.embedded_type?(type) do if Ash.Type.embedded_type?(type) do
@ -298,8 +299,8 @@ defmodule AshPhoenix.Form.Auto do
if is_struct(data) do if is_struct(data) do
case config[:tag_value] || key do case config[:tag_value] || key do
value when is_atom(value) -> value when is_atom(value) ->
data[config[:tag]] == to_string(value) || Map.get(data, config[:tag]) == to_string(value) ||
data[config[:tag]] == value Map.get(data, config[:tag]) == value
value -> value ->
data[config[:tag]] == value data[config[:tag]] == value

View file

@ -483,7 +483,7 @@ defmodule AshPhoenix.Form do
source, source,
action, action,
params, params,
changeset_opts allow_all_keys_to_be_skipped(changeset_opts, params)
) )
%__MODULE__{ %__MODULE__{
@ -580,7 +580,7 @@ defmodule AshPhoenix.Form do
source, source,
action, action,
params, params,
changeset_opts allow_all_keys_to_be_skipped(changeset_opts, params)
) )
%__MODULE__{ %__MODULE__{
@ -716,7 +716,7 @@ defmodule AshPhoenix.Form do
source, source,
action, action,
params, params,
changeset_opts allow_all_keys_to_be_skipped(changeset_opts, params)
) )
%__MODULE__{ %__MODULE__{
@ -3193,16 +3193,9 @@ defmodule AshPhoenix.Form do
end) end)
touched_forms = touched_forms =
if is_map(params) do Enum.reduce(Map.keys(params) -- ["_touched"], touched_forms, &MapSet.put(&2, &1))
Enum.reduce(Map.keys(params) -- ["_touched"], touched_forms, &MapSet.put(&2, &1))
else
touched_forms
end
form_touched = form_touched = params["_touched"]
if is_map(params) do
params["_touched"]
end
if is_binary(form_touched) do if is_binary(form_touched) do
form_touched form_touched
@ -3933,35 +3926,33 @@ defmodule AshPhoenix.Form do
warn_on_unhandled_errors? warn_on_unhandled_errors?
) do ) do
if Keyword.has_key?(opts, :data) do if Keyword.has_key?(opts, :data) do
data =
if opts[:data] do
if is_function(opts[:data]) do
if Enum.at(prev_data_trail, 0) do
case call_data(opts[:data], prev_data_trail) do
%Ash.NotLoaded{} ->
raise AshPhoenix.Form.NoDataLoaded,
path: Enum.reverse(trail, [key])
other ->
other
end
else
nil
end
else
opts[:data]
end
end
cond do cond do
opts[:update_action] -> opts[:update_action] ->
update_action = opts[:update_action] update_action = opts[:update_action]
data =
if opts[:data] do
if is_function(opts[:data]) do
if Enum.at(prev_data_trail, 0) do
case call_data(opts[:data], prev_data_trail) do
%Ash.NotLoaded{} ->
raise AshPhoenix.Form.NoDataLoaded,
path: Enum.reverse(trail, [key])
other ->
other
end
else
nil
end
else
opts[:data]
end
end
if data do if data do
form_values = form_values =
if (opts[:type] || :single) == :single do if (opts[:type] || :single) == :single do
opts = update_opts(opts, data, %{})
for_action(data, update_action, for_action(data, update_action,
domain: domain, domain: domain,
actor: actor, actor: actor,
@ -3981,7 +3972,7 @@ defmodule AshPhoenix.Form do
data data
|> Enum.with_index() |> Enum.with_index()
|> Enum.map(fn {data, index} -> |> Enum.map(fn {data, index} ->
opts = update_opts(opts, data, %{}) opts = Keyword.put(opts, :data, data)
for_action(data, update_action, for_action(data, update_action,
domain: domain, domain: domain,
@ -4092,7 +4083,32 @@ defmodule AshPhoenix.Form do
end end
true -> true ->
{forms, params} if data && data != [] do
opts = update_opts(opts, data, %{})
if opts[:update_action] || opts[:read_action] do
handle_form_without_params(
forms,
params,
opts,
key,
domain,
actor,
tenant,
trail,
prev_data_trail,
error?,
name,
id,
transform_errors,
warn_on_unhandled_errors?
)
else
{forms, params}
end
else
{forms, params}
end
end end
else else
{forms, params} {forms, params}

View file

@ -1,5 +1,5 @@
%{ %{
"ash": {:hex, :ash, "3.0.0-rc.24", "1d6393b6ae73440d1bdecc09f7d8fc29e4b12f3664551acd61e44555660ae4f2", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, ">= 0.8.1 and < 1.0.0-0", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.1.18 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.6", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d268c198b83a5ea6a6b54d4132127cbcbab858757175d44f3b2fcf78dc6ffdfd"}, "ash": {:hex, :ash, "3.0.0-rc.26", "e0d65bb786247481ec663af7c1cc21346f7184afe5880d6a63cd6dff80129969", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, ">= 0.8.1 and < 1.0.0-0", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.1.18 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.6", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "42553a0dbe6a494ab5eb5df1818fd64154832edc463fdc01ff34943223d89a14"},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"}, "bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"castore": {:hex, :castore, "1.0.6", "ffc42f110ebfdafab0ea159cd43d31365fa0af0ce4a02ecebf1707ae619ee727", [:mix], [], "hexpm", "374c6e7ca752296be3d6780a6d5b922854ffcc74123da90f2f328996b962d33a"}, "castore": {:hex, :castore, "1.0.6", "ffc42f110ebfdafab0ea159cd43d31365fa0af0ce4a02ecebf1707ae619ee727", [:mix], [], "hexpm", "374c6e7ca752296be3d6780a6d5b922854ffcc74123da90f2f328996b962d33a"},
"comparable": {:hex, :comparable, "1.0.0", "bb669e91cedd14ae9937053e5bcbc3c52bb2f22422611f43b6e38367d94a495f", [:mix], [{:typable, "~> 0.1", [hex: :typable, repo: "hexpm", optional: false]}], "hexpm", "277c11eeb1cd726e7cd41c6c199e7e52fa16ee6830b45ad4cdc62e51f62eb60c"}, "comparable": {:hex, :comparable, "1.0.0", "bb669e91cedd14ae9937053e5bcbc3c52bb2f22422611f43b6e38367d94a495f", [:mix], [{:typable, "~> 0.1", [hex: :typable, repo: "hexpm", optional: false]}], "hexpm", "277c11eeb1cd726e7cd41c6c199e7e52fa16ee6830b45ad4cdc62e51f62eb60c"},