mirror of
https://github.com/ash-project/ash_phoenix.git
synced 2024-09-20 07:12:49 +12:00
chore: refactor the use_data? change
This commit is contained in:
parent
e98c617405
commit
a7b2296d43
2 changed files with 29 additions and 41 deletions
|
@ -417,23 +417,29 @@ defmodule AshPhoenix do
|
|||
changeset
|
||||
|
||||
{rel, id, nil} ->
|
||||
if Map.has_key?(changeset.relationships, rel) || !opts[:use_data?] ||
|
||||
!loaded?(changeset.data, rel) do
|
||||
new_relationships =
|
||||
changeset.relationships
|
||||
|> Map.put_new(rel, [])
|
||||
|> Map.update!(rel, fn manages ->
|
||||
manages ++ [{add_to_path(nil, path, add), [meta: [id: id]]}]
|
||||
to_add =
|
||||
if opts[:use_data?] && loaded?(changeset.data, rel) do
|
||||
changeset.data
|
||||
|> Map.get(rel)
|
||||
|> List.wrap()
|
||||
|> Enum.reduce(nil, fn _, acc ->
|
||||
add_to_path(acc, path, %{})
|
||||
end)
|
||||
|> add_to_path(path, add)
|
||||
else
|
||||
add_to_path(nil, path, add)
|
||||
end
|
||||
|
||||
manages ++ [{to_add, [meta: [id: id]]}]
|
||||
end)
|
||||
|
||||
%{changeset | relationships: new_relationships}
|
||||
else
|
||||
add_all_related_and_try_again(changeset, rel, original_path, outer_form_name, opts)
|
||||
end
|
||||
|
||||
{rel, _id, index} ->
|
||||
if Map.has_key?(changeset.relationships, rel) || !opts[:use_data?] ||
|
||||
!loaded?(changeset.data, rel) do
|
||||
new_relationships =
|
||||
changeset.relationships
|
||||
|> Map.put_new(rel, [])
|
||||
|
@ -444,9 +450,6 @@ defmodule AshPhoenix do
|
|||
end)
|
||||
|
||||
%{changeset | relationships: new_relationships}
|
||||
else
|
||||
add_all_related_and_try_again(changeset, rel, original_path, outer_form_name, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -457,21 +460,6 @@ defmodule AshPhoenix do
|
|||
end
|
||||
end
|
||||
|
||||
defp add_all_related_and_try_again(changeset, rel, path, outer_form_name, opts) do
|
||||
changeset.data
|
||||
|> Map.get(rel)
|
||||
|> List.wrap()
|
||||
|> Enum.reduce(changeset, fn _, changeset ->
|
||||
add_related(
|
||||
changeset,
|
||||
path,
|
||||
outer_form_name,
|
||||
Keyword.merge(opts, add: %{}, use_data?: false)
|
||||
)
|
||||
end)
|
||||
|> add_related(path, outer_form_name, opts)
|
||||
end
|
||||
|
||||
@remove_related_opts [
|
||||
add: [
|
||||
type: :any,
|
||||
|
|
|
@ -3,7 +3,7 @@ defmodule AshPhoenix.ChangesetTest do
|
|||
import Phoenix.HTML.Form, only: [form_for: 2, inputs_for: 2, inputs_for: 3]
|
||||
|
||||
alias Phoenix.HTML.FormData
|
||||
alias AshPhoenix.Test.{Api, Comment, Post, PostLink}
|
||||
alias AshPhoenix.Test.{Api, Comment, Post}
|
||||
|
||||
describe "form_for fields" do
|
||||
test "it should show simple field values" do
|
||||
|
|
Loading…
Reference in a new issue