2021-07-16 06:09:15 +12:00
|
|
|
defmodule AshPhoenix.FormTest do
|
|
|
|
use ExUnit.Case
|
|
|
|
import Phoenix.HTML.Form, only: [form_for: 2, inputs_for: 2]
|
|
|
|
|
|
|
|
alias AshPhoenix.Form
|
2021-11-11 09:11:22 +13:00
|
|
|
alias AshPhoenix.Test.{Api, Comment, OtherApi, Post, PostWithDefault}
|
2021-07-16 06:09:15 +12:00
|
|
|
alias Phoenix.HTML.FormData
|
|
|
|
|
|
|
|
describe "form_for fields" do
|
|
|
|
test "it should show simple field values" do
|
|
|
|
form =
|
|
|
|
Post
|
|
|
|
|> Form.for_create(:create)
|
|
|
|
|> Form.validate(%{"text" => "text"})
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert FormData.input_value(form.source, form, :text) == "text"
|
|
|
|
end
|
2021-07-19 04:28:47 +12:00
|
|
|
|
|
|
|
test "it sets the default id of a form" do
|
|
|
|
assert Form.for_create(Post, :create).id == "form"
|
|
|
|
assert Form.for_create(Post, :create, as: "post").id == "post"
|
|
|
|
end
|
2021-07-16 06:09:15 +12:00
|
|
|
end
|
|
|
|
|
2021-11-13 16:10:33 +13:00
|
|
|
test "validation errors are attached to fields" do
|
2021-11-11 09:11:22 +13:00
|
|
|
form = Form.for_create(PostWithDefault, :create, api: Api)
|
2021-11-13 15:22:55 +13:00
|
|
|
form = AshPhoenix.Form.validate(form, %{"text" => ""}, errors: form.submitted_once?)
|
|
|
|
{:error, form} = Form.submit(form, params: %{"text" => ""})
|
2021-11-11 09:11:22 +13:00
|
|
|
assert %{errors: [text: {"is required", []}]} = form_for(form, "foo")
|
|
|
|
assert form.valid? == false
|
|
|
|
end
|
|
|
|
|
2021-11-07 05:15:50 +13:00
|
|
|
describe "the .changed? field is updated as data changes" do
|
2021-11-07 07:12:53 +13:00
|
|
|
test "it is false for a create form with no changes" do
|
2021-11-07 05:15:50 +13:00
|
|
|
form =
|
|
|
|
Post
|
|
|
|
|> Form.for_create(:create)
|
|
|
|
|> Form.validate(%{})
|
|
|
|
|
2021-11-07 07:12:53 +13:00
|
|
|
refute form.changed?
|
2021-11-07 05:15:50 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it is false by default for update forms" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{text: "post"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
form =
|
|
|
|
post
|
|
|
|
|> Form.for_update(:update)
|
|
|
|
|> Form.validate(%{})
|
|
|
|
|
|
|
|
refute form.changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it is true when a change is made" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{text: "post"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
form =
|
|
|
|
post
|
|
|
|
|> Form.for_update(:update)
|
|
|
|
|> Form.validate(%{text: "post1"})
|
|
|
|
|
|
|
|
assert form.changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it goes back to false if the change is unmade" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{text: "post"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
form =
|
|
|
|
post
|
|
|
|
|> Form.for_update(:update)
|
|
|
|
|> Form.validate(%{text: "post1"})
|
|
|
|
|
|
|
|
assert form.changed?
|
|
|
|
|
|
|
|
form =
|
|
|
|
form
|
|
|
|
|> Form.validate(%{text: "post"})
|
|
|
|
|
|
|
|
refute form.changed?
|
|
|
|
end
|
2021-11-07 07:12:53 +13:00
|
|
|
|
|
|
|
test "adding a form causes changed? to be true on the root form, but not the nested form" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
api: Api,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
2021-11-08 22:47:12 +13:00
|
|
|
|> Form.add_form(:post)
|
2021-11-07 07:12:53 +13:00
|
|
|
|
|
|
|
assert form.changed?
|
|
|
|
refute form.forms[:post].changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "removing a form that was there prior marks the form as changed" 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!()
|
|
|
|
|
|
|
|
# Check the persisted post.comments count after create
|
|
|
|
post = Post |> Api.get!(post.id) |> Api.load!(:comments)
|
|
|
|
assert Enum.count(post.comments) == 1
|
|
|
|
|
|
|
|
form =
|
|
|
|
post
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
api: Api,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
resource: Comment,
|
|
|
|
type: :list,
|
|
|
|
data: [comment],
|
|
|
|
create_action: :create,
|
|
|
|
update_action: :update
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
refute form.changed?
|
|
|
|
|
|
|
|
form = Form.remove_form(form, [:comments, 0])
|
|
|
|
|
|
|
|
assert form.changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "removing a form that was added does not mark the form as changed" 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!()
|
|
|
|
|
|
|
|
# Check the persisted post.comments count after create
|
|
|
|
post = Post |> Api.get!(post.id) |> Api.load!(:comments)
|
|
|
|
assert Enum.count(post.comments) == 1
|
|
|
|
|
|
|
|
form =
|
|
|
|
post
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
api: Api,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
resource: Comment,
|
|
|
|
type: :list,
|
|
|
|
data: [comment],
|
|
|
|
create_action: :create,
|
|
|
|
update_action: :update
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
refute form.changed?
|
|
|
|
|
|
|
|
form = Form.add_form(form, [:comments])
|
|
|
|
|
|
|
|
assert form.changed?
|
|
|
|
|
|
|
|
form = Form.remove_form(form, [:comments, 1])
|
|
|
|
|
|
|
|
refute form.changed?
|
|
|
|
end
|
2021-11-07 05:15:50 +13:00
|
|
|
end
|
|
|
|
|
2021-07-16 06:09:15 +12:00
|
|
|
describe "errors" do
|
|
|
|
test "errors are not set on the form without validating" do
|
|
|
|
form =
|
|
|
|
Post
|
|
|
|
|> Form.for_create(:create)
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert form.errors == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "errors are set on the form according to changeset errors on validate" do
|
|
|
|
form =
|
|
|
|
Post
|
|
|
|
|> Form.for_create(:create)
|
|
|
|
|> Form.validate(%{})
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert form.errors == [{:text, {"is required", []}}]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "nested errors are set on the appropriate form after submit" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
2021-07-19 11:20:25 +12:00
|
|
|
api: Api,
|
2021-07-16 06:09:15 +12:00
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{})
|
|
|
|
|> Form.validate(%{"text" => "text", "post" => %{}})
|
2021-07-19 11:20:25 +12:00
|
|
|
|> Form.submit(force?: true)
|
2021-07-16 06:09:15 +12:00
|
|
|
|> elem(1)
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert form.errors == []
|
|
|
|
|
2021-07-17 10:23:07 +12:00
|
|
|
assert hd(inputs_for(form, :post)).errors == [{:text, {"is required", []}}]
|
2021-07-16 06:09:15 +12:00
|
|
|
end
|
|
|
|
|
2021-08-02 05:18:37 +12:00
|
|
|
test "relationship source data is retained, so that it can be properly removed" 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!()
|
|
|
|
|
|
|
|
comment = Comment |> Api.get!(comment.id)
|
|
|
|
|
|
|
|
comment
|
|
|
|
|> Api.load!(:post)
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
api: Api,
|
|
|
|
forms: [
|
|
|
|
auto?: true
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.remove_form([:post])
|
|
|
|
|> Form.submit!(params: %{"text" => "text", "post" => %{"text" => "new_post"}})
|
|
|
|
|
|
|
|
assert [%{text: "new_post"}] = Api.read!(Post)
|
|
|
|
end
|
|
|
|
|
2021-07-17 11:58:14 +12:00
|
|
|
test "nested errors are set on the appropriate form after submit, even if no submit actually happens" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
2021-07-19 11:20:25 +12:00
|
|
|
api: Api,
|
2021-07-17 11:58:14 +12:00
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{})
|
2021-07-19 11:20:25 +12:00
|
|
|
|> Form.submit(params: %{"text" => "text", "post" => %{}})
|
2021-07-17 11:58:14 +12:00
|
|
|
|> elem(1)
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert form.errors == []
|
|
|
|
|
|
|
|
assert hd(inputs_for(form, :post)).errors == [{:text, {"is required", []}}]
|
|
|
|
end
|
|
|
|
|
2021-07-22 13:52:04 +12:00
|
|
|
test "nested errors can be fetched with `Form.errors/2`" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
api: Api,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{})
|
|
|
|
|> Form.validate(%{"text" => "text", "post" => %{}})
|
|
|
|
|> Form.submit(force?: true)
|
|
|
|
|> elem(1)
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert Form.errors(form.source, for_path: [:post]) == [{:text, "is required"}]
|
|
|
|
|
|
|
|
assert Form.errors(form.source, for_path: [:post], format: :raw) == [
|
|
|
|
{:text, {"is required", []}}
|
|
|
|
]
|
|
|
|
|
|
|
|
assert Form.errors(form.source, for_path: [:post], format: :plaintext) == [
|
|
|
|
"text: is required"
|
|
|
|
]
|
|
|
|
|
|
|
|
assert Form.errors(form.source, for_path: :all) == %{[:post] => [{:text, "is required"}]}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "errors can be fetched with `Form.errors/2`" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
api: Api,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{})
|
|
|
|
|> Form.validate(%{"post" => %{"text" => "text"}})
|
|
|
|
|> Form.submit(force?: true)
|
|
|
|
|> elem(1)
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert Form.errors(form.source) == [{:text, "is required"}]
|
|
|
|
|
|
|
|
assert Form.errors(form.source, format: :raw) == [
|
|
|
|
{:text, {"is required", []}}
|
|
|
|
]
|
|
|
|
|
|
|
|
assert Form.errors(form.source, format: :plaintext) == [
|
|
|
|
"text: is required"
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-08-11 17:27:07 +12:00
|
|
|
test "nested forms don't submit empty values when not present in forms" do
|
2021-07-16 06:09:15 +12:00
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{})
|
|
|
|
|> Form.validate(%{"text" => "text"})
|
|
|
|
|
|
|
|
assert Form.params(form) == %{"text" => "text", "post" => nil}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "nested forms submit empty list values when not present in input params" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{})
|
|
|
|
|> Form.validate(%{"text" => "text"})
|
|
|
|
|
2021-08-04 14:59:04 +12:00
|
|
|
assert Form.value(form, :text) == "text"
|
|
|
|
|
2021-07-16 06:09:15 +12:00
|
|
|
assert Form.params(form) == %{"text" => "text", "post" => []}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "nested errors are set on the appropriate form after submit for many to many relationships" do
|
|
|
|
form =
|
|
|
|
Post
|
|
|
|
|> Form.for_create(:create,
|
2021-07-19 11:20:25 +12:00
|
|
|
api: Api,
|
2021-07-16 06:09:15 +12:00
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
for: :linked_posts,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{})
|
|
|
|
|> Form.validate(%{"text" => "text", "post" => [%{}]})
|
2021-07-19 11:20:25 +12:00
|
|
|
|> Form.submit(force?: true)
|
2021-07-16 06:09:15 +12:00
|
|
|
|> elem(1)
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert form.errors == []
|
|
|
|
|
|
|
|
assert [nested_form] = inputs_for(form, :post)
|
|
|
|
assert nested_form.errors == [{:text, {"is required", []}}]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "data" do
|
|
|
|
test "it uses the provided data to create forms even without input" do
|
|
|
|
post1_id = Ash.UUID.generate()
|
|
|
|
post2_id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(
|
|
|
|
:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
data: [%Post{id: post1_id}, %Post{id: post2_id}],
|
|
|
|
update_action: :update,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.validate(%{
|
|
|
|
"text" => "text",
|
2021-08-28 05:21:48 +12:00
|
|
|
"post" => %{
|
|
|
|
"0" => %{"id" => post1_id},
|
|
|
|
"1" => %{"id" => post2_id}
|
|
|
|
}
|
2021-07-16 06:09:15 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
assert Form.params(form) == %{
|
|
|
|
"post" => [
|
2021-08-28 05:21:48 +12:00
|
|
|
%{"id" => post1_id, "comments" => []},
|
|
|
|
%{"id" => post2_id, "comments" => []}
|
2021-07-16 06:09:15 +12:00
|
|
|
],
|
|
|
|
"text" => "text"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a function can be used to derive the data from the data of the parent form" do
|
|
|
|
post1_id = Ash.UUID.generate()
|
|
|
|
post2_id = Ash.UUID.generate()
|
|
|
|
comment_id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(
|
|
|
|
:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
data: [
|
|
|
|
%Post{id: post1_id, comments: [%Comment{id: comment_id}]},
|
|
|
|
%Post{id: post2_id, comments: []}
|
|
|
|
],
|
|
|
|
update_action: :update,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
data: &(&1.comments || []),
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
create_action: :create,
|
|
|
|
update_action: :update
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.validate(%{
|
|
|
|
"text" => "text",
|
|
|
|
"post" => %{
|
|
|
|
"0" => %{"id" => post1_id, "comments" => %{"0" => %{"id" => comment_id}}},
|
|
|
|
"1" => %{"id" => post2_id}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert Form.params(form) == %{
|
|
|
|
"post" => [
|
|
|
|
%{"comments" => [%{"id" => comment_id}], "id" => post1_id},
|
2021-08-28 05:21:48 +12:00
|
|
|
%{"id" => post2_id, "comments" => []}
|
2021-07-16 06:09:15 +12:00
|
|
|
],
|
|
|
|
"text" => "text"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "submit" do
|
|
|
|
test "it runs the action with the params" do
|
|
|
|
assert {:ok, %{text: "text"}} =
|
|
|
|
Post
|
2021-07-19 11:20:25 +12:00
|
|
|
|> Form.for_create(:create, api: Api)
|
2021-07-16 06:09:15 +12:00
|
|
|
|> Form.validate(%{text: "text"})
|
2021-07-19 11:20:25 +12:00
|
|
|
|> Form.submit()
|
2021-07-16 06:09:15 +12:00
|
|
|
end
|
2021-07-19 16:58:46 +12:00
|
|
|
|
|
|
|
test "it raises an appropriate error when the incorrect api is configured" do
|
|
|
|
assert_raise RuntimeError,
|
|
|
|
~r/Resource AshPhoenix.Test.Post not found in api AshPhoenix.Test.OtherApi/,
|
|
|
|
fn ->
|
|
|
|
Post
|
|
|
|
|> Form.for_create(:create, api: OtherApi)
|
|
|
|
|> Form.validate(%{text: "text"})
|
|
|
|
|> Form.submit()
|
|
|
|
end
|
|
|
|
end
|
2021-07-16 06:09:15 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "params" do
|
|
|
|
test "it includes nested forms, and honors their `for` configuration" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
other_post: [
|
|
|
|
type: :single,
|
|
|
|
for: :for_posts,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form("form[post]", params: %{"text" => "post_text"})
|
|
|
|
|> Form.add_form("form[other_post]", params: %{"text" => "post_text"})
|
|
|
|
|> Form.add_form("form[post][0][comments]", params: %{"text" => "post_text"})
|
|
|
|
|> Form.validate(%{
|
|
|
|
"text" => "text",
|
|
|
|
"post" => [%{"comments" => [%{"text" => "post_text"}], "text" => "post_text"}],
|
|
|
|
"other_post" => %{"text" => "post_text"}
|
|
|
|
})
|
|
|
|
|
2021-08-12 10:48:52 +12:00
|
|
|
assert %{
|
2021-07-16 06:09:15 +12:00
|
|
|
"text" => "text",
|
|
|
|
"post" => [%{"comments" => [%{"text" => "post_text"}], "text" => "post_text"}],
|
|
|
|
"for_posts" => %{"text" => "post_text"}
|
2021-08-12 10:48:52 +12:00
|
|
|
} = Form.params(form)
|
2021-07-16 06:09:15 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "`inputs_for` with no configuration" do
|
|
|
|
test "it should raise an error" do
|
|
|
|
form =
|
|
|
|
Post
|
|
|
|
|> Form.for_create(:create)
|
|
|
|
|> Form.validate(%{text: "text"})
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert_raise AshPhoenix.Form.NoFormConfigured, fn ->
|
|
|
|
inputs_for(form, :post) == []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "inputs_for` relationships" do
|
2021-07-19 14:07:54 +12:00
|
|
|
test "it should name the fields correctly on `for_update`" do
|
|
|
|
post_id = Ash.UUID.generate()
|
|
|
|
comment_id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
comment = %Comment{
|
|
|
|
text: "text",
|
|
|
|
post: %Post{
|
|
|
|
id: post_id,
|
|
|
|
text: "Some text",
|
|
|
|
comments: [%Comment{id: comment_id}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
as: "comment",
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: comment.post,
|
|
|
|
type: :single,
|
|
|
|
resource: Post,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
data: & &1.comments,
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
comments_form =
|
|
|
|
form
|
|
|
|
|> form_for("action")
|
|
|
|
|> inputs_for(:post)
|
|
|
|
|> hd()
|
|
|
|
|> inputs_for(:comments)
|
|
|
|
|> hd()
|
|
|
|
|
|
|
|
assert comments_form.name == "comment[post][comments][0]"
|
|
|
|
end
|
|
|
|
|
2021-07-16 06:09:15 +12:00
|
|
|
test "the `type: :single` option should create a form without integer paths" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text"})
|
|
|
|
|> Form.validate(%{"text" => "text", "post" => %{"text" => "post_text"}})
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert %Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
2021-07-17 10:23:07 +12:00
|
|
|
related_form = hd(inputs_for(form, :post))
|
2021-07-16 06:09:15 +12:00
|
|
|
|
|
|
|
assert related_form.name == "form[post]"
|
|
|
|
|
|
|
|
assert FormData.input_value(related_form.source, related_form, :text) == "post_text"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it should show nothing in `inputs_for` by default" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.validate(%{"text" => "text"})
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert inputs_for(form, :post) == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when a value has been appended to the relationship, a form is created" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text"})
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
related_form
|
|
|
|
] = inputs_for(form, :post)
|
|
|
|
|
|
|
|
assert FormData.input_value(related_form.source, related_form, :text) == "post_text"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a query path can be used when manipulating forms" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form("form[post]", params: %{text: "post_text"})
|
|
|
|
|> Form.add_form("form[post][0][comments]", params: %{text: "post_text"})
|
|
|
|
|> form_for("action")
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
post_form
|
|
|
|
] = inputs_for(form, :post)
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Comment}}
|
|
|
|
] = inputs_for(post_form, :comments)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "list values get an index in their name and id" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text0"})
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text1"})
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text2"})
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
form_0,
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
form_1,
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
form_2
|
|
|
|
] = inputs_for(form_for(form, "action"), :post)
|
|
|
|
|
|
|
|
assert form_0.name == "form[post][0]"
|
|
|
|
assert form_0.id == "form_post_0"
|
|
|
|
|
|
|
|
assert form_1.name == "form[post][1]"
|
|
|
|
assert form_1.id == "form_post_1"
|
|
|
|
|
|
|
|
assert form_2.name == "form[post][2]"
|
|
|
|
assert form_2.id == "form_post_2"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when a value has been removed from the relationship, the form is removed" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text0"})
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text1"})
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text2"})
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}},
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}},
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}}
|
|
|
|
] = inputs_for(form_for(form, "action"), :post)
|
|
|
|
|
|
|
|
form =
|
|
|
|
form
|
|
|
|
|> Form.remove_form([:post, 0])
|
|
|
|
|> Form.remove_form([:post, 1])
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
related_form
|
|
|
|
] = inputs_for(form_for(form, "action"), :post)
|
|
|
|
|
|
|
|
assert FormData.input_value(related_form.source, related_form, :text) == "post_text1"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when all values have been removed from a relationship, the empty list remains" do
|
|
|
|
form =
|
|
|
|
Comment
|
|
|
|
|> Form.for_create(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text0"})
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text1"})
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text2"})
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}},
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}},
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}}
|
|
|
|
] = inputs_for(form_for(form, "action"), :post)
|
|
|
|
|
|
|
|
form =
|
|
|
|
form
|
|
|
|
|> Form.remove_form([:post, 0])
|
|
|
|
|> Form.remove_form([:post, 0])
|
|
|
|
|> Form.remove_form([:post, 0])
|
|
|
|
|> Form.validate(%{})
|
|
|
|
|
|
|
|
assert [] = inputs_for(form_for(form, "action"), :post)
|
|
|
|
assert Form.params(form) == %{"post" => []}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when all values have been removed from an existing relationship, the empty list remains" do
|
|
|
|
post1_id = Ash.UUID.generate()
|
|
|
|
post2_id = Ash.UUID.generate()
|
|
|
|
comment = %Comment{text: "text", post: [%Post{id: post1_id}, %Post{id: post2_id}]}
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:create,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: comment.post,
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create,
|
|
|
|
update_action: :update
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form(:post, params: %{text: "post_text3"})
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}},
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}},
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}}
|
|
|
|
] = inputs_for(form_for(form, "action"), :post)
|
|
|
|
|
|
|
|
form =
|
|
|
|
form
|
|
|
|
|> Form.remove_form([:post, 0])
|
|
|
|
|> Form.remove_form([:post, 0])
|
|
|
|
|> Form.remove_form([:post, 0])
|
|
|
|
|> Form.validate(%{})
|
|
|
|
|
|
|
|
assert Form.params(form) == %{"post" => []}
|
|
|
|
end
|
2021-07-17 10:23:07 +12:00
|
|
|
|
2021-07-20 17:54:36 +12:00
|
|
|
test "when all values have been removed from an existing `:single` relationship, the empty list remains" do
|
|
|
|
post_id = Ash.UUID.generate()
|
|
|
|
comment_2 = %Comment{text: "text"}
|
|
|
|
|
|
|
|
comment = %Comment{text: "text", post: %Post{id: post_id, comments: [comment_2]}}
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: & &1.post,
|
|
|
|
type: :single,
|
|
|
|
resource: Post,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
data: & &1.comments,
|
|
|
|
create_action: :create,
|
|
|
|
update_action: :update
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert [%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}}] =
|
|
|
|
form
|
|
|
|
|> form_for("action")
|
|
|
|
|> inputs_for(:post)
|
|
|
|
|
|
|
|
form =
|
|
|
|
form
|
|
|
|
|> Form.remove_form([:post, :comments, 0])
|
2021-08-12 10:48:52 +12:00
|
|
|
# This is added by the hidden fields helper, so we add it here to simulate that.
|
|
|
|
|> Form.validate(%{"post" => %{"_touched" => "comments"}})
|
2021-07-20 17:54:36 +12:00
|
|
|
|
2021-08-12 10:48:52 +12:00
|
|
|
assert %{"post" => %{"comments" => []}} = Form.params(form)
|
2021-07-20 17:54:36 +12:00
|
|
|
end
|
|
|
|
|
2021-07-20 14:55:05 +12:00
|
|
|
test "remaining forms are reindexed after a form has been removed" do
|
|
|
|
post1_id = Ash.UUID.generate()
|
|
|
|
post2_id = Ash.UUID.generate()
|
|
|
|
post3_id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
comment = %Comment{
|
|
|
|
text: "text",
|
|
|
|
post: [%Post{id: post1_id}, %Post{id: post2_id}, %Post{id: post3_id}]
|
|
|
|
}
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:create,
|
|
|
|
forms: [
|
|
|
|
posts: [
|
|
|
|
data: comment.post,
|
|
|
|
type: :list,
|
|
|
|
resource: Post,
|
|
|
|
create_action: :create,
|
|
|
|
update_action: :update
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.remove_form([:posts, 1])
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
form_0,
|
|
|
|
%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}} =
|
|
|
|
form_1
|
|
|
|
] = inputs_for(form_for(form, "action"), :posts)
|
|
|
|
|
|
|
|
assert form_0.name == "form[posts][0]"
|
|
|
|
assert form_0.id == "form_posts_0"
|
|
|
|
|
|
|
|
assert form_1.name == "form[posts][1]"
|
|
|
|
assert form_1.id == "form_posts_1"
|
|
|
|
end
|
|
|
|
|
2021-07-17 10:23:07 +12:00
|
|
|
test "when `:single`, `inputs_for` generates a list of one single item" do
|
|
|
|
post_id = Ash.UUID.generate()
|
|
|
|
comment = %Comment{text: "text", post: %Post{id: post_id, text: "Some text"}}
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: comment.post,
|
|
|
|
type: :single,
|
|
|
|
resource: Post,
|
|
|
|
update_action: :update
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert [%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Post}}] =
|
|
|
|
inputs_for(form_for(form, "action"), :post)
|
|
|
|
end
|
2021-07-18 07:57:27 +12:00
|
|
|
|
2021-07-19 14:07:54 +12:00
|
|
|
test "it creates nested forms for single resources" do
|
2021-07-18 07:57:27 +12:00
|
|
|
post_id = Ash.UUID.generate()
|
2021-07-18 08:06:15 +12:00
|
|
|
comment_id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
comment = %Comment{
|
|
|
|
text: "text",
|
|
|
|
post: %Post{
|
|
|
|
id: post_id,
|
|
|
|
text: "Some text",
|
|
|
|
comments: [%Comment{id: comment_id}]
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 07:57:27 +12:00
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: comment.post,
|
|
|
|
type: :single,
|
|
|
|
resource: Post,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
data: & &1.comments,
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-07-18 08:06:15 +12:00
|
|
|
assert [%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Comment}}] =
|
2021-07-18 07:57:27 +12:00
|
|
|
form
|
|
|
|
|> form_for("action")
|
|
|
|
|> inputs_for(:post)
|
|
|
|
|> hd()
|
|
|
|
|> inputs_for(:comments)
|
|
|
|
end
|
2021-07-20 15:42:11 +12:00
|
|
|
|
|
|
|
test "it `add_form`s for nested single resources" do
|
|
|
|
post_id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
comment = %Comment{
|
|
|
|
text: "text",
|
|
|
|
post: %Post{
|
|
|
|
id: post_id,
|
|
|
|
text: "Some text",
|
|
|
|
comments: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: comment.post,
|
|
|
|
type: :single,
|
|
|
|
resource: Post,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form([:post, :comments])
|
|
|
|
|
|
|
|
assert [%Phoenix.HTML.Form{source: %AshPhoenix.Form{resource: AshPhoenix.Test.Comment}}] =
|
|
|
|
form
|
|
|
|
|> form_for("action")
|
|
|
|
|> inputs_for(:post)
|
|
|
|
|> hd()
|
|
|
|
|> inputs_for(:comments)
|
|
|
|
end
|
2021-07-20 17:00:44 +12:00
|
|
|
|
|
|
|
test "it `remove_form`s for nested single resources" do
|
|
|
|
post_id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
comment = %Comment{
|
|
|
|
text: "text",
|
|
|
|
post: %Post{
|
|
|
|
id: post_id,
|
|
|
|
text: "Some text",
|
|
|
|
comments: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: comment.post,
|
|
|
|
type: :single,
|
|
|
|
resource: Post,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Form.add_form([:post, :comments])
|
|
|
|
|> Form.remove_form([:post, :comments, 0])
|
|
|
|
|
|
|
|
assert [] =
|
|
|
|
form
|
|
|
|
|> form_for("action")
|
|
|
|
|> inputs_for(:post)
|
|
|
|
|> hd()
|
|
|
|
|> inputs_for(:comments)
|
|
|
|
end
|
2021-07-16 06:09:15 +12:00
|
|
|
end
|
2021-08-05 16:45:09 +12:00
|
|
|
|
|
|
|
describe "issue #259" do
|
|
|
|
test "updating should not duplicate nested resources" 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!()
|
|
|
|
|
|
|
|
# Check the persisted post.comments count after create
|
|
|
|
post = Post |> Api.get!(post.id) |> Api.load!(:comments)
|
|
|
|
assert Enum.count(post.comments) == 1
|
|
|
|
|
|
|
|
# Grab the persisted comment
|
|
|
|
comment = Comment |> Api.get!(comment.id) |> Api.load!(post: [:comments])
|
|
|
|
|
|
|
|
form =
|
|
|
|
comment
|
|
|
|
|> Form.for_update(:update,
|
|
|
|
as: "comment",
|
|
|
|
api: Api,
|
|
|
|
forms: [
|
|
|
|
post: [
|
|
|
|
data: & &1.post,
|
|
|
|
type: :single,
|
|
|
|
resource: Post,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create,
|
|
|
|
forms: [
|
|
|
|
comments: [
|
|
|
|
data: & &1.comments,
|
|
|
|
type: :list,
|
|
|
|
resource: Comment,
|
|
|
|
update_action: :update,
|
|
|
|
create_action: :create
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-08-05 18:07:36 +12:00
|
|
|
updated_comment =
|
2021-08-05 16:45:09 +12:00
|
|
|
form
|
2021-08-05 18:07:36 +12:00
|
|
|
|> AshPhoenix.Form.submit!(
|
2021-08-05 16:45:09 +12:00
|
|
|
params: %{
|
|
|
|
"post" => %{
|
|
|
|
"id" => post.id,
|
|
|
|
"text" => "text",
|
|
|
|
"comments" => %{
|
|
|
|
"0" => %{
|
|
|
|
"id" => comment.id,
|
|
|
|
"text" => comment.text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Enum.count(updated_comment.post.comments) == 1
|
|
|
|
|
|
|
|
# now, check the persisted post
|
|
|
|
persisted_post = Post |> Api.get!(post.id) |> Api.load!(:comments)
|
|
|
|
assert Enum.count(persisted_post.comments) == 1
|
|
|
|
end
|
|
|
|
end
|
2021-07-16 06:09:15 +12:00
|
|
|
end
|