mirror of
https://github.com/ash-project/ash.git
synced 2024-09-19 21:13:10 +12:00
fix: change_attribute failing on a union attribute (#778)
This commit is contained in:
parent
a3a176287d
commit
1ce05562aa
2 changed files with 27 additions and 1 deletions
|
@ -530,7 +530,7 @@ defmodule Ash.Type.Union do
|
|||
|> Map.new()
|
||||
|> case do
|
||||
%{tag: field, tag_value: tag} ->
|
||||
if new_value[field] == tag || new_value[to_string(field)] == tag,
|
||||
if get_tag(new_value, field) == tag,
|
||||
do: do_prepare_change(type_name, old_value, new_value, constraints),
|
||||
else: {:ok, new_value}
|
||||
|
||||
|
|
|
@ -58,6 +58,22 @@ defmodule Ash.Test.Type.UnionTest do
|
|||
]
|
||||
]
|
||||
end
|
||||
|
||||
attribute :thing, :union,
|
||||
constraints: [
|
||||
types: [
|
||||
foo: [
|
||||
type: Foo,
|
||||
tag: :type,
|
||||
tag_value: "foo"
|
||||
],
|
||||
bar: [
|
||||
type: Bar,
|
||||
tag: :type,
|
||||
tag_value: "bar"
|
||||
]
|
||||
]
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -234,6 +250,16 @@ defmodule Ash.Test.Type.UnionTest do
|
|||
end
|
||||
end
|
||||
|
||||
test "it handles changing union attribute on a resource" do
|
||||
Example
|
||||
|> Ash.Changeset.for_create(:create, %{thing: %Foo{type: "foo", foo: "foo"}})
|
||||
|> Ash.Test.AnyApi.create!()
|
||||
|> Ash.Changeset.new()
|
||||
|> Ash.Changeset.change_attribute(:thing, %Bar{type: "bar", bar: "bar"})
|
||||
|> Ash.Changeset.for_update(:update)
|
||||
|> Ash.Test.AnyApi.update!()
|
||||
end
|
||||
|
||||
test "it handles paths on a resource" do
|
||||
Example
|
||||
|> Ash.Changeset.for_create(:create, %{things: [%{type: :foo, foo: "bar"}]})
|
||||
|
|
Loading…
Reference in a new issue