mirror of
https://github.com/ash-project/ash_phoenix.git
synced 2024-09-20 07:12:49 +12:00
WIP
This commit is contained in:
parent
e8bf9d648d
commit
6ba516aeaa
3 changed files with 333 additions and 270 deletions
|
@ -49,12 +49,11 @@ defmodule AshPhoenix.Form.Auto do
|
||||||
if you are using liveview, you could actually look up the record using the input from the read action, and then use `AshPhoenix.Form.update_form/3`
|
if you are using liveview, you could actually look up the record using the input from the read action, and then use `AshPhoenix.Form.update_form/3`
|
||||||
to set that looked up record as the data of the `_update` form.
|
to set that looked up record as the data of the `_update` form.
|
||||||
|
|
||||||
### Many to Many Relationshisp
|
### Many to Many Relationships
|
||||||
|
|
||||||
In the case that a manage_change option points to a join relationship, that form is presented via a special nested form called
|
In the case that a manage_change option points to a join relationship, that form is presented via a special nested form called
|
||||||
`_join`. So the first form in `inputs_for(form, :relationship)` would be for the destination, and then inside of that you could say
|
`_join`. So the first form in `inputs_for(form, :relationship)` would be for the destination, and then inside of that you could say
|
||||||
`inputs_for(nested_form, :_join)`. The parameters are merged together during submission.
|
`inputs_for(nested_form, :_join)`. The parameters are merged together during submission.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@dialyzer {:nowarn_function, rel_to_resource: 2}
|
@dialyzer {:nowarn_function, rel_to_resource: 2}
|
||||||
|
|
|
@ -848,6 +848,9 @@ defmodule AshPhoenix.Form do
|
||||||
|
|
||||||
new_forms =
|
new_forms =
|
||||||
Enum.reduce(form_params, forms, fn {index, params}, forms ->
|
Enum.reduce(form_params, forms, fn {index, params}, forms ->
|
||||||
|
if params["_ignore"] == "true" do
|
||||||
|
Map.put_new(forms, key, [])
|
||||||
|
else
|
||||||
case Enum.find(form.forms[key] || [], &matcher.(&1, params, form, key, index)) do
|
case Enum.find(form.forms[key] || [], &matcher.(&1, params, form, key, index)) do
|
||||||
nil ->
|
nil ->
|
||||||
create_action =
|
create_action =
|
||||||
|
@ -888,6 +891,7 @@ defmodule AshPhoenix.Form do
|
||||||
[validated]
|
[validated]
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if Map.has_key?(new_forms, opts[:as] || key) do
|
if Map.has_key?(new_forms, opts[:as] || key) do
|
||||||
|
@ -897,6 +901,9 @@ defmodule AshPhoenix.Form do
|
||||||
else
|
else
|
||||||
new_forms
|
new_forms
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
if form_params["_ignore"] == "true" do
|
||||||
|
Map.put_new(forms, key, nil)
|
||||||
else
|
else
|
||||||
if form.forms[key] do
|
if form.forms[key] do
|
||||||
new_form =
|
new_form =
|
||||||
|
@ -929,6 +936,7 @@ defmodule AshPhoenix.Form do
|
||||||
Map.put(forms, key, new_form)
|
Map.put(forms, key, new_form)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
case opts[:type] do
|
case opts[:type] do
|
||||||
|
@ -2847,6 +2855,7 @@ defmodule AshPhoenix.Form do
|
||||||
transform_errors
|
transform_errors
|
||||||
) do
|
) do
|
||||||
if (opts[:type] || :single) == :single do
|
if (opts[:type] || :single) == :single do
|
||||||
|
if map(form_params)["_ignore"] != "true" do
|
||||||
if map(form_params)["_form_type"] == "read" do
|
if map(form_params)["_form_type"] == "read" do
|
||||||
read_action =
|
read_action =
|
||||||
opts[:read_action] ||
|
opts[:read_action] ||
|
||||||
|
@ -2890,9 +2899,13 @@ defmodule AshPhoenix.Form do
|
||||||
id: id <> "_#{key}"
|
id: id <> "_#{key}"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
form_params
|
form_params
|
||||||
|> indexed_list()
|
|> indexed_list()
|
||||||
|
|> Enum.reject(fn {form_params, _} ->
|
||||||
|
map(form_params)["_ignore"] == "true"
|
||||||
|
end)
|
||||||
|> Enum.with_index()
|
|> Enum.with_index()
|
||||||
|> Enum.map(fn {{form_params, original_index}, index} ->
|
|> Enum.map(fn {{form_params, original_index}, index} ->
|
||||||
if map(form_params)["_form_type"] == "read" do
|
if map(form_params)["_form_type"] == "read" do
|
||||||
|
@ -2965,6 +2978,7 @@ defmodule AshPhoenix.Form do
|
||||||
end
|
end
|
||||||
|
|
||||||
if (opts[:type] || :single) == :single do
|
if (opts[:type] || :single) == :single do
|
||||||
|
if map(form_params)["_ignore"] != "true" do
|
||||||
if data do
|
if data do
|
||||||
case map(form_params)["_form_type"] || "update" do
|
case map(form_params)["_form_type"] || "update" do
|
||||||
"update" ->
|
"update" ->
|
||||||
|
@ -3051,6 +3065,7 @@ defmodule AshPhoenix.Form do
|
||||||
raise "unexpected form type for form with no data #{other} with params: #{inspect(form_params)}"
|
raise "unexpected form type for form with no data #{other} with params: #{inspect(form_params)}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
data = List.wrap(data)
|
data = List.wrap(data)
|
||||||
|
|
||||||
|
@ -3059,6 +3074,15 @@ defmodule AshPhoenix.Form do
|
||||||
|> Enum.with_index()
|
|> Enum.with_index()
|
||||||
|> Enum.reduce({[], List.wrap(data)}, fn {{form_params, original_index}, index},
|
|> Enum.reduce({[], List.wrap(data)}, fn {{form_params, original_index}, index},
|
||||||
{forms, data} ->
|
{forms, data} ->
|
||||||
|
if form_params["_ignore"] == true do
|
||||||
|
case data do
|
||||||
|
[_item | rest] ->
|
||||||
|
{forms, rest}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
{forms, data}
|
||||||
|
end
|
||||||
|
else
|
||||||
if map(form_params)["_form_type"] == "read" do
|
if map(form_params)["_form_type"] == "read" do
|
||||||
resource =
|
resource =
|
||||||
opts[:read_resource] || opts[:resource] ||
|
opts[:read_resource] || opts[:resource] ||
|
||||||
|
@ -3174,6 +3198,7 @@ defmodule AshPhoenix.Form do
|
||||||
{[form | forms], []}
|
{[form | forms], []}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|> elem(0)
|
|> elem(0)
|
||||||
|> Enum.reverse()
|
|> Enum.reverse()
|
||||||
|
|
|
@ -117,6 +117,45 @@ defmodule AshPhoenix.FormTest do
|
||||||
assert Form.params(form) == %{"comments" => [%{"id" => comment.id}]}
|
assert Form.params(form) == %{"comments" => [%{"id" => comment.id}]}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "ignoring a form filters it from the parameters" do
|
||||||
|
post =
|
||||||
|
Post
|
||||||
|
|> Ash.Changeset.new(%{text: "post"})
|
||||||
|
|> Api.create!()
|
||||||
|
|
||||||
|
comment =
|
||||||
|
Comment
|
||||||
|
|> Ash.Changeset.new(%{text: "comment"})
|
||||||
|
|> Ash.Changeset.replace_relationship(:post, post)
|
||||||
|
|> Api.create!()
|
||||||
|
|
||||||
|
form =
|
||||||
|
post
|
||||||
|
|> Form.for_update(:update_with_replace,
|
||||||
|
api: Api,
|
||||||
|
forms: [
|
||||||
|
comments: [
|
||||||
|
read_resource: Comment,
|
||||||
|
type: :list,
|
||||||
|
read_action: :read,
|
||||||
|
data: [comment]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert [comment_form] = inputs_for(form_for(form, "blah"), :comments)
|
||||||
|
|
||||||
|
assert Phoenix.HTML.Form.input_value(comment_form, :text) == "comment"
|
||||||
|
|
||||||
|
form = Form.validate(form, %{"comments" => [%{"id" => comment.id, "_ignore" => "true"}]})
|
||||||
|
|
||||||
|
assert Form.params(form) == %{"comments" => []}
|
||||||
|
|
||||||
|
form = Form.validate(form, %{"comments" => [%{"id" => comment.id, "_ignore" => "false"}]})
|
||||||
|
|
||||||
|
assert Form.params(form) == %{"comments" => [%{"id" => comment.id}]}
|
||||||
|
end
|
||||||
|
|
||||||
describe "the .changed? field is updated as data changes" do
|
describe "the .changed? field is updated as data changes" do
|
||||||
test "it is false for a create form with no changes" do
|
test "it is false for a create form with no changes" do
|
||||||
form =
|
form =
|
||||||
|
|
Loading…
Reference in a new issue