improvement: sort relationship in order of input when managing it

This helps with things like https://github.com/ash-project/ash_phoenix/issues/57
which involve rendering the relationship value after editing it. Retaining
the order allows direct reuse without any gymnastics
This commit is contained in:
Zach Daniel 2022-10-19 00:39:10 -04:00
parent 5a11736931
commit 235e965627
2 changed files with 10 additions and 3 deletions

View file

@ -645,6 +645,13 @@ defmodule Ash.Actions.ManagedRelationships do
opts
) do
{:ok, new_value, notifications} ->
new_value =
if is_list(new_value) do
Enum.reverse(new_value)
else
new_value
end
{:ok, Map.put(record, relationship.name, new_value),
all_notifications ++ notifications}

View file

@ -580,11 +580,11 @@ defmodule Ash.Test.Actions.CreateTest do
|> Api.create!()
|> strip_metadata()
assert Enum.sort(post.related_posts) ==
Enum.sort([
assert post.related_posts ==
[
Api.get!(Post, post2.id),
Api.get!(Post, post3.id)
])
]
|> strip_metadata()
end
end