Fix remove_component (#86)

This commit is contained in:
Darren Black 2023-03-28 18:21:41 +11:00 committed by GitHub
parent 1a1ff1a568
commit d22a48a514
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View file

@ -903,11 +903,11 @@ defmodule AshPhoenix.FilterForm do
|> set_validity()
end
@doc "Removes the group *or* component with the given id"
def remove_component(form, group_or_component_id) do
@doc "Removes the group *or* predicate with the given id"
def remove_component(form, group_or_predicate_id) do
form
|> remove_group(group_or_component_id)
|> remove_component(group_or_component_id)
|> remove_group(group_or_predicate_id)
|> remove_predicate(group_or_predicate_id)
end
defp remove_if_empty(form, false), do: [form]

View file

@ -61,6 +61,31 @@ defmodule AshPhoenix.FilterFormTest do
} = form
end
test "groups and predicates can be removed with remove_component" do
form = FilterForm.new(Post)
{form, group_id} = FilterForm.add_group(form, operator: :or, return_id?: true)
{form, predicate_id} =
FilterForm.add_predicate(form, :title, :eq, "new post", to: group_id, return_id?: true)
form = FilterForm.remove_component(form, predicate_id)
assert %FilterForm{
components: [
%FilterForm{
components: []
}
]
} = form
form = FilterForm.remove_component(form, group_id)
assert %FilterForm{
components: []
} = form
end
test "with `remove_empty_groups?: true` empty groups are removed on component removal" do
form = FilterForm.new(Post, remove_empty_groups?: true)