mirror of
https://github.com/ash-project/ash_phoenix.git
synced 2024-09-20 07:12:49 +12:00
improvement: tests and better behavior for union forms
This commit is contained in:
parent
f4f8897e9f
commit
e295fb72f7
4 changed files with 100 additions and 8 deletions
|
@ -177,8 +177,12 @@ defmodule AshPhoenix.Form.Auto do
|
||||||
|
|
||||||
transform_params =
|
transform_params =
|
||||||
if fake_embedded? do
|
if fake_embedded? do
|
||||||
fn form, _params, _type ->
|
fn form, params, type ->
|
||||||
|
if type == :nested do
|
||||||
AshPhoenix.Form.value(form, :value)
|
AshPhoenix.Form.value(form, :value)
|
||||||
|
else
|
||||||
|
params
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -244,11 +248,11 @@ defmodule AshPhoenix.Form.Auto do
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
|
|
||||||
#{inspect(params)}
|
#{inspect(params, pretty: true)}
|
||||||
|
|
||||||
Available types:
|
Available types:
|
||||||
|
|
||||||
#{inspect(constraints[:types])}
|
#{inspect(constraints[:types], pretty: true)}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
{_key, config} ->
|
{_key, config} ->
|
||||||
|
@ -266,13 +270,16 @@ defmodule AshPhoenix.Form.Auto do
|
||||||
raise """
|
raise """
|
||||||
Got no "_union_type" parameter, and no union type had a tag & tag_value pair matching the params.
|
Got no "_union_type" parameter, and no union type had a tag & tag_value pair matching the params.
|
||||||
|
|
||||||
|
If you are adding a form, select a type using `params: %{"_union_type" => "type_name"}`, or if one
|
||||||
|
or more of your types is using a tag you can set that tag with `params: %{"tag" => "tag_value"}`.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
|
|
||||||
#{inspect(params)}
|
#{inspect(params, pretty: true)}
|
||||||
|
|
||||||
Available types:
|
Available types:
|
||||||
|
|
||||||
#{inspect(constraints[:types])}
|
#{inspect(constraints[:types], pretty: true)}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
{_key, config} ->
|
{_key, config} ->
|
||||||
|
@ -823,6 +830,8 @@ defmodule AshPhoenix.Form.Auto do
|
||||||
|> Keyword.new()
|
|> Keyword.new()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp union?({:array, type}), do: union?(type)
|
||||||
|
|
||||||
defp union?(type) do
|
defp union?(type) do
|
||||||
if Ash.Type.NewType.new_type?(type) do
|
if Ash.Type.NewType.new_type?(type) do
|
||||||
union?(Ash.Type.NewType.subtype_of(type))
|
union?(Ash.Type.NewType.subtype_of(type))
|
||||||
|
|
|
@ -1182,7 +1182,7 @@ defmodule AshPhoenix.Form do
|
||||||
Map.update(forms, key, [new_form], &(&1 ++ [new_form]))
|
Map.update(forms, key, [new_form], &(&1 ++ [new_form]))
|
||||||
|
|
||||||
matching_form ->
|
matching_form ->
|
||||||
opts = update_opts(opts, matching_form.data, form_params)
|
opts = update_opts(opts, matching_form.data, params)
|
||||||
|
|
||||||
validated =
|
validated =
|
||||||
validate(matching_form, params,
|
validate(matching_form, params,
|
||||||
|
@ -2889,6 +2889,13 @@ defmodule AshPhoenix.Form do
|
||||||
fields -> Keyword.put(hidden, :_touched, fields)
|
fields -> Keyword.put(hidden, :_touched, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
hidden =
|
||||||
|
if form.params["_union_type"] do
|
||||||
|
Keyword.put(hidden, :_union_type, form.params["_union_type"])
|
||||||
|
else
|
||||||
|
hidden
|
||||||
|
end
|
||||||
|
|
||||||
if form.params["_index"] && form.params["_index"] != "" do
|
if form.params["_index"] && form.params["_index"] != "" do
|
||||||
Keyword.put(hidden, :_index, form.params["_index"])
|
Keyword.put(hidden, :_index, form.params["_index"])
|
||||||
else
|
else
|
||||||
|
|
|
@ -2,7 +2,8 @@ defmodule AshPhoenix.AutoFormTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
|
|
||||||
alias AshPhoenix.Form.Auto
|
alias AshPhoenix.Form.Auto
|
||||||
alias AshPhoenix.Test.Post
|
alias AshPhoenix.Test.{Api, Post}
|
||||||
|
import Phoenix.HTML.Form, only: [form_for: 2]
|
||||||
import AshPhoenix.Form, only: [update_opts: 2]
|
import AshPhoenix.Form, only: [update_opts: 2]
|
||||||
|
|
||||||
test "it works for simple relationships" do
|
test "it works for simple relationships" do
|
||||||
|
@ -53,6 +54,80 @@ defmodule AshPhoenix.AutoFormTest do
|
||||||
assert validated.source.arguments[:comment_ids] == [1]
|
assert validated.source.arguments[:comment_ids] == [1]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "single unions" do
|
||||||
|
test "a form can be added for a union" do
|
||||||
|
Post
|
||||||
|
|> AshPhoenix.Form.for_create(:create,
|
||||||
|
api: Api,
|
||||||
|
forms: [
|
||||||
|
auto?: true
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|> AshPhoenix.Form.add_form(:union, params: %{"type" => "foo"})
|
||||||
|
|> form_for("action")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "a form can be removed from a union" do
|
||||||
|
form =
|
||||||
|
Post
|
||||||
|
|> AshPhoenix.Form.for_create(:create,
|
||||||
|
api: Api,
|
||||||
|
forms: [
|
||||||
|
auto?: true
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|> AshPhoenix.Form.add_form(:union, params: %{"type" => "foo"})
|
||||||
|
|> form_for("action")
|
||||||
|
|
||||||
|
AshPhoenix.Form.remove_form(form, [:union])
|
||||||
|
end
|
||||||
|
|
||||||
|
test "a form can be added for a non-embedded type" do
|
||||||
|
Post
|
||||||
|
|> AshPhoenix.Form.for_create(:create,
|
||||||
|
api: Api,
|
||||||
|
forms: [
|
||||||
|
auto?: true
|
||||||
|
],
|
||||||
|
params: %{
|
||||||
|
"text" => "foobar"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|> AshPhoenix.Form.add_form(:union, params: %{"_union_type" => "bar", "value" => 10})
|
||||||
|
|> AshPhoenix.Form.submit!()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "list unions" do
|
||||||
|
test "a form can be added for a union" do
|
||||||
|
Post
|
||||||
|
|> AshPhoenix.Form.for_create(:create,
|
||||||
|
api: Api,
|
||||||
|
forms: [
|
||||||
|
auto?: true
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|> AshPhoenix.Form.add_form(:union_array, params: %{"type" => "foo"})
|
||||||
|
|> form_for("action")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "a form can be removed from a union" do
|
||||||
|
form =
|
||||||
|
Post
|
||||||
|
|> AshPhoenix.Form.for_create(:create,
|
||||||
|
api: Api,
|
||||||
|
forms: [
|
||||||
|
auto?: true
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|> AshPhoenix.Form.add_form(:union_array, params: %{"type" => "foo"})
|
||||||
|
|> form_for("action")
|
||||||
|
|
||||||
|
AshPhoenix.Form.remove_form(form, [:union_array, 0])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
defp auto_forms(resource, action) do
|
defp auto_forms(resource, action) do
|
||||||
[forms: Auto.auto(resource, action)]
|
[forms: Auto.auto(resource, action)]
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,7 @@ defmodule AshPhoenix.Test.Post do
|
||||||
uuid_primary_key(:id)
|
uuid_primary_key(:id)
|
||||||
attribute(:text, :string, allow_nil?: false)
|
attribute(:text, :string, allow_nil?: false)
|
||||||
attribute(:union, AshPhoenix.Test.UnionValue)
|
attribute(:union, AshPhoenix.Test.UnionValue)
|
||||||
|
attribute(:union_array, {:array, AshPhoenix.Test.UnionValue})
|
||||||
attribute(:title, :string)
|
attribute(:title, :string)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue