From 24cc008410e83dc4426f9c96cf5f38f81afed326 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 4 Sep 2024 20:51:54 -0400 Subject: [PATCH] improvement: ArgumentError for non-existent actions in `Ash.can?` --- lib/ash/can.ex | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/ash/can.ex b/lib/ash/can.ex index fac1c2ad..16c5d367 100644 --- a/lib/ash/can.ex +++ b/lib/ash/can.ex @@ -132,32 +132,50 @@ defmodule Ash.Can do defp resource_subject_input(action_or_query_or_changeset, domain, actor, opts) do case action_or_query_or_changeset do {resource, name} when is_atom(name) and is_atom(resource) -> + action = + Ash.Resource.Info.action(resource, name) || + raise ArgumentError, "No such action #{name} on #{inspect(resource)}" + resource_subject_input( - {resource, Ash.Resource.Info.action(resource, name), %{}}, + {resource, action, %{}}, domain, actor, opts ) - {resource, name, input} when is_atom(name) and is_atom(resource) -> + {resource, name, input} when is_atom(name) and is_atom(resource) and not is_nil(name) -> + action = + Ash.Resource.Info.action(resource, name) || + raise ArgumentError, "No such action #{name} on #{inspect(resource)}" + resource_subject_input( - {resource, Ash.Resource.Info.action(resource, name), input}, + {resource, action, input}, domain, actor, opts ) - {%resource{} = record, name} when is_atom(name) and is_atom(resource) -> + {%resource{} = record, name} + when is_atom(name) and is_atom(resource) and not is_nil(name) -> + action = + Ash.Resource.Info.action(resource, name) || + raise ArgumentError, "No such action #{name} on #{inspect(resource)}" + resource_subject_input( - {record, Ash.Resource.Info.action(resource, name), %{}}, + {record, action, %{}}, domain, actor, opts ) - {%resource{} = record, name, input} when is_atom(name) and is_atom(resource) -> + {%resource{} = record, name, input} + when is_atom(name) and is_atom(resource) and not is_nil(name) -> + action = + Ash.Resource.Info.action(resource, name) || + raise ArgumentError, "No such action #{name} on #{inspect(resource)}" + resource_subject_input( - {record, Ash.Resource.Info.action(resource, name), input}, + {record, action, input}, domain, actor, opts