From 97daeeb8fbce10cf9269648c950fed1a71d01ad9 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 24 Apr 2023 07:04:40 -0600 Subject: [PATCH] fix: don't warn on being managed from attribute changing --- lib/ash/changeset/changeset.ex | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/ash/changeset/changeset.ex b/lib/ash/changeset/changeset.ex index 312c4ffc..df04cfae 100644 --- a/lib/ash/changeset/changeset.ex +++ b/lib/ash/changeset/changeset.ex @@ -1485,12 +1485,29 @@ defmodule Ash.Changeset do def require_values(changeset, _, _, _), do: changeset defp belongs_to_attr_of_rel_being_managed?(attribute, changeset) do + do_belongs_to_attr_of_rel_being_managed?(changeset, attribute) || + belongs_to_attr_of_being_managed_through?(changeset, attribute) + end + + defp do_belongs_to_attr_of_rel_being_managed?(changeset, attribute) 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 + defp belongs_to_attr_of_being_managed_through?( + %{context: %{accessing_from: %{source: source, name: relationship}}}, + attribute + ) do + case Ash.Resource.Info.relationship(source, relationship) do + %{type: :belongs_to} -> false + relationship -> relationship.destination_attribute == attribute.name + end + end + + defp belongs_to_attr_of_being_managed_through?(_, _), do: false + # 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.