mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
fix: don't initially require attributes for belongs to relationships being managed
This commit is contained in:
parent
ba1fa4c6d5
commit
3d8f988e13
1 changed files with 21 additions and 0 deletions
|
@ -1403,6 +1403,13 @@ defmodule Ash.Changeset do
|
|||
name when is_atom(name) ->
|
||||
Ash.Resource.Info.attribute(changeset.resource, name)
|
||||
end)
|
||||
|> then(fn attributes ->
|
||||
if private_and_belongs_to? do
|
||||
attributes
|
||||
else
|
||||
Enum.reject(attributes, &belongs_to_attr_of_rel_being_managed?(&1, changeset))
|
||||
end
|
||||
end)
|
||||
|> Enum.reduce(changeset, fn required_attribute, changeset ->
|
||||
if changing_attribute?(changeset, required_attribute.name) do
|
||||
if is_nil(get_attribute(changeset, required_attribute.name)) do
|
||||
|
@ -1448,6 +1455,13 @@ defmodule Ash.Changeset do
|
|||
name when is_atom(name) ->
|
||||
Ash.Resource.Info.attribute(changeset.resource, name)
|
||||
end)
|
||||
|> then(fn attributes ->
|
||||
if private_and_belongs_to? do
|
||||
attributes
|
||||
else
|
||||
Enum.reject(attributes, &belongs_to_attr_of_rel_being_managed?(&1, changeset))
|
||||
end
|
||||
end)
|
||||
|> Enum.reduce(changeset, fn required_attribute, changeset ->
|
||||
if changing_attribute?(changeset, required_attribute.name) do
|
||||
if is_nil(get_attribute(changeset, required_attribute.name)) do
|
||||
|
@ -1470,6 +1484,13 @@ defmodule Ash.Changeset do
|
|||
|
||||
def require_values(changeset, _, _, _), do: changeset
|
||||
|
||||
defp belongs_to_attr_of_rel_being_managed?(attribute, changeset) do
|
||||
Enum.any?(changeset.relationships, fn {key, _} ->
|
||||
relationship = Ash.Resource.Info.relationship(changeset.resource, key)
|
||||
relationship.type == :belongs_to && relationship.source_attribute == attribute.name
|
||||
end)
|
||||
end
|
||||
|
||||
# Attributes that are private and/or are the source field of a belongs_to relationship
|
||||
# are typically not set by input, so they aren't required until the actual action
|
||||
# is run.
|
||||
|
|
Loading…
Reference in a new issue