diff --git a/lib/ash/action_input.ex b/lib/ash/action_input.ex index eb611995..42c63d8b 100644 --- a/lib/ash/action_input.ex +++ b/lib/ash/action_input.ex @@ -58,6 +58,7 @@ defmodule Ash.ActionInput do domain = input.domain || opts[:domain] || Ash.Resource.Info.domain(input.resource) || + Ash.Actions.Helpers.maybe_embedded_domain(input.resource) || raise ArgumentError, message: "Could not determine domain for action. Provide the `domain` option or configure a domain in the resource directly." diff --git a/lib/ash/actions/helpers.ex b/lib/ash/actions/helpers.ex index 0a53db59..6fa8df63 100644 --- a/lib/ash/actions/helpers.ex +++ b/lib/ash/actions/helpers.ex @@ -74,6 +74,12 @@ defmodule Ash.Actions.Helpers do opts end + def maybe_embedded_domain(resource) do + if Ash.Resource.Info.embedded?(resource) do + Ash.EmbeddableType.ShadowDomain + end + end + def set_context_and_get_opts(domain, query_or_changeset, opts) do opts = transform_tenant(opts) opts = set_skip_unknown_opts(opts, query_or_changeset) @@ -81,7 +87,7 @@ defmodule Ash.Actions.Helpers do domain = Ash.Resource.Info.domain(query_or_changeset.resource) || opts[:domain] || domain || - query_or_changeset.domain + query_or_changeset.domain || maybe_embedded_domain(query_or_changeset.resource) opts = case query_or_changeset.context do diff --git a/lib/ash/changeset/changeset.ex b/lib/ash/changeset/changeset.ex index 1c8142db..8661177f 100644 --- a/lib/ash/changeset/changeset.ex +++ b/lib/ash/changeset/changeset.ex @@ -1318,6 +1318,7 @@ defmodule Ash.Changeset do domain = changeset.domain || opts[:domain] || Ash.Resource.Info.domain(changeset.resource) || + Ash.Actions.Helpers.maybe_embedded_domain(changeset.resource) || raise ArgumentError, message: "Could not determine domain for changeset. Provide the `domain` option or configure a domain in the resource directly." @@ -1606,6 +1607,7 @@ defmodule Ash.Changeset do defp do_for_action(changeset, action_or_name, params, opts) do domain = changeset.domain || opts[:domain] || Ash.Resource.Info.domain(changeset.resource) || + Ash.Actions.Helpers.maybe_embedded_domain(changeset.resource) || raise ArgumentError, message: "Could not determine domain for changeset. Provide the `domain` option or configure a domain in the resource directly." diff --git a/lib/ash/helpers.ex b/lib/ash/helpers.ex index f9d9d5dc..da1c2934 100644 --- a/lib/ash/helpers.ex +++ b/lib/ash/helpers.ex @@ -501,7 +501,8 @@ defmodule Ash.Helpers do defp domain_from_resource(resource) do if Ash.Resource.Info.resource?(resource) || (is_map(resource) and not is_struct(resource)) do - Ash.Resource.Info.domain(resource) + Ash.Resource.Info.domain(resource) || + Ash.Actions.Helpers.maybe_embedded_domain(resource) end end diff --git a/lib/ash/query/query.ex b/lib/ash/query/query.ex index a791e126..d5c1a888 100644 --- a/lib/ash/query/query.ex +++ b/lib/ash/query/query.ex @@ -500,6 +500,7 @@ defmodule Ash.Query do domain = query.domain || opts[:domain] || Ash.Resource.Info.domain(query.resource) || + Ash.Actions.Helpers.maybe_embedded_domain(query.resource) || raise ArgumentError, "Could not determine domain for query. Provide the `domain` option or configure a domain in the resource directly." @@ -2879,6 +2880,7 @@ defmodule Ash.Query do def apply_to(query, records, opts \\ []) do domain = query.domain || Ash.Resource.Info.domain(query.resource) || opts[:domain] || + Ash.Actions.Helpers.maybe_embedded_domain(query.resource) || raise ArgumentError, "Could not determine domain for #{inspect(query)}, please provide the `:domain` option." diff --git a/lib/ash/resource.ex b/lib/ash/resource.ex index ceb15ffe..cf68856b 100644 --- a/lib/ash/resource.ex +++ b/lib/ash/resource.ex @@ -118,29 +118,18 @@ defmodule Ash.Resource do ] do @persist {:simple_notifiers, List.wrap(opts[:simple_notifiers])} - cond do - embedded? && has_domain? -> - raise """ - Configuration Error in #{inspect(__MODULE__)}: + unless embedded? || has_domain? do + IO.warn(""" + Configuration Error: - `domain` option must not be specified for embedded resource. - """ + `domain` option missing for #{inspect(__MODULE__)} - embedded? || has_domain? -> - :ok + If you wish to make a resource compatible with multiple domains, set the domain to `nil` explicitly. - true -> - IO.warn(""" - Configuration Error: + Example configuration: - `domain` option missing for #{inspect(__MODULE__)} - - If you wish to make a resource compatible with multiple domains, set the domain to `nil` explicitly. - - Example configuration: - - use Ash.Resource, #{String.trim_trailing(String.trim_leading(inspect([{:domain, YourDomain} | opts], pretty: true), "["), "]")} - """) + use Ash.Resource, #{String.trim_trailing(String.trim_leading(inspect([{:domain, YourDomain} | opts], pretty: true), "["), "]")} + """) end if domain do