From a4fef11c6e30468b8cfb3e02d91a4ee4cf724f93 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Tue, 19 Oct 2021 05:25:33 -0400 Subject: [PATCH] fix: don't require primary actions if disabled --- .../transformers/set_primary_actions.ex | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/ash/resource/transformers/set_primary_actions.ex b/lib/ash/resource/transformers/set_primary_actions.ex index ed9f47ea..591b882b 100644 --- a/lib/ash/resource/transformers/set_primary_actions.ex +++ b/lib/ash/resource/transformers/set_primary_actions.ex @@ -14,6 +14,8 @@ defmodule Ash.Resource.Transformers.SetPrimaryActions do @extension Ash.Resource.Dsl def transform(resource, dsl_state) do + primary_actions? = Ash.Resource.Info.primary_actions?(resource) + dsl_state |> Transformer.get_entities([:actions]) |> Enum.group_by(& &1.type) @@ -48,29 +50,33 @@ defmodule Ash.Resource.Transformers.SetPrimaryActions do )}} {type, actions}, {:ok, dsl_state} -> - case min(Enum.count(actions, & &1.primary?), 2) do - 0 -> - {:halt, - {:error, - DslError.exception( - module: __MODULE__, - message: - "Multiple actions of type #{type} defined, one must be designated as `primary?`", - path: [:actions, type] - )}} + if primary_actions? && !(primary_actions? == :only_read && type != :read) do + case min(Enum.count(actions, & &1.primary?), 2) do + 0 -> + {:halt, + {:error, + DslError.exception( + module: __MODULE__, + message: + "Multiple actions of type #{type} defined, one must be designated as `primary?`", + path: [:actions, type] + )}} - 1 -> - {:cont, {:ok, dsl_state}} + 1 -> + {:cont, {:ok, dsl_state}} - 2 -> - {:halt, - {:error, - DslError.exception( - module: __MODULE__, - message: - "Multiple actions of type #{type} configured as `primary?: true`, but only one action per type can be the primary", - path: [:actions, type] - )}} + 2 -> + {:halt, + {:error, + DslError.exception( + module: __MODULE__, + message: + "Multiple actions of type #{type} configured as `primary?: true`, but only one action per type can be the primary", + path: [:actions, type] + )}} + end + else + {:cont, {:ok, dsl_state}} end end) end