From 80cedce2a21f4d6421bfc027f43839ed81765efb Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 2 Aug 2021 11:02:38 -0400 Subject: [PATCH] fix: remove belongs to related after action --- lib/ash/actions/managed_relationships.ex | 144 ++++++++++++----------- 1 file changed, 77 insertions(+), 67 deletions(-) diff --git a/lib/ash/actions/managed_relationships.ex b/lib/ash/actions/managed_relationships.ex index 6a89a548..66a89cc2 100644 --- a/lib/ash/actions/managed_relationships.ex +++ b/lib/ash/actions/managed_relationships.ex @@ -235,77 +235,87 @@ defmodule Ash.Actions.ManagedRelationships do case data do {:ok, data} -> - current_value = Map.get(data, relationship.name) - - case delete_unused( - data, - List.wrap(current_value), - relationship, - [], - [], - changeset, - actor, - opts - ) do - {:ok, _, new_instructions} -> - instructions = new_instructions ++ instructions - - if input in [nil || []] do + if input in [nil || []] do + {:cont, {changeset, instructions}} + else + case opts[:on_no_match] do + ignore when ignore in [:ignore, :match] -> {:cont, {changeset, instructions}} - else - case opts[:on_no_match] do - ignore when ignore in [:ignore, :match] -> - {:cont, {changeset, instructions}} - :error -> - if opts[:on_lookup] != :ignore do - changeset = - changeset - |> Ash.Changeset.add_error( - NotFound.exception( - primary_key: input, - resource: relationship.destination - ), - [opts[:meta][:id] || relationship.name] - ) - |> Ash.Changeset.put_context(:private, %{ - error: %{relationship.name => true} - }) - - {:halt, {changeset, instructions}} - else - changeset = - changeset - |> Ash.Changeset.add_error( - InvalidRelationship.exception( - relationship: relationship.name, - message: "Changes would create a new related record" - ), - [opts[:meta][:id] || relationship.name] - ) - |> Ash.Changeset.put_context(:private, %{ - error: %{relationship.name => true} - }) - - {:halt, {changeset, instructions}} - end - - {:create, action_name} -> - do_create_belongs_to_record( - relationship, - action_name, - input, - changeset, - actor, - opts, - instructions, - index + :error -> + if opts[:on_lookup] != :ignore do + changeset = + changeset + |> Ash.Changeset.add_error( + NotFound.exception( + primary_key: input, + resource: relationship.destination + ), + [opts[:meta][:id] || relationship.name] ) - end - end + |> Ash.Changeset.put_context(:private, %{ + error: %{relationship.name => true} + }) - {:error, error} -> - {:halt, {Ash.Changeset.add_error(changeset, error), instructions}} + {:halt, {changeset, instructions}} + else + changeset = + changeset + |> Ash.Changeset.add_error( + InvalidRelationship.exception( + relationship: relationship.name, + message: "Changes would create a new related record" + ), + [opts[:meta][:id] || relationship.name] + ) + |> Ash.Changeset.put_context(:private, %{ + error: %{relationship.name => true} + }) + + {:halt, {changeset, instructions}} + end + + {:create, action_name} -> + do_create_belongs_to_record( + relationship, + action_name, + input, + changeset, + actor, + opts, + instructions, + index + ) + end + end + |> case do + {:cont, {changeset, instructions}} -> + changeset = + Ash.Changeset.after_action(changeset, fn _, result -> + current_value = Map.get(data, relationship.name) + + case delete_unused( + data, + List.wrap(current_value), + relationship, + [], + [], + changeset, + actor, + opts + ) do + {:ok, _, new_instructions} -> + {:ok, result, new_instructions} + + {:error, error} -> + {:halt, {Ash.Changeset.add_error(changeset, error), instructions}} + end + end) + + {:cont, {changeset, instructions}} + + {:halt, other} -> + {:halt, other} end {:error, error} ->