improvement: automatically fall back to a default domain when working with embeds

This commit is contained in:
Zach Daniel 2024-07-23 19:14:16 -04:00
parent 0301009073
commit 516ff8ef8e
6 changed files with 22 additions and 21 deletions

View file

@ -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."

View file

@ -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

View file

@ -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."

View file

@ -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

View file

@ -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."

View file

@ -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