improvement: error on using relate_actor with :many cardinality relationships

This commit is contained in:
Zach Daniel 2024-01-09 08:46:38 -05:00
parent fc9132e75b
commit 63c15bbfe9
2 changed files with 20 additions and 1 deletions

View file

@ -15,6 +15,8 @@ defmodule Ash.Resource.Change.RelateActor do
end
def change(changeset, opts, %{actor: nil}) do
validate_type!(changeset, opts)
if opts[:allow_nil?] do
changeset
else
@ -29,6 +31,7 @@ defmodule Ash.Resource.Change.RelateActor do
end
def change(changeset, opts, %{actor: actor}) do
validate_type!(changeset, opts)
field = opts[:field]
Changeset.manage_relationship(
@ -39,6 +42,22 @@ defmodule Ash.Resource.Change.RelateActor do
)
end
defp validate_type!(changeset, opts) do
case Ash.Resource.Info.relationship(changeset.resource, opts[:relationship]) do
%{type: type} when type in [:belongs_to, :has_one] ->
:ok
%{type: type} ->
raise ArgumentError, """
Cannot use `relate_actor` change with relationship of type #{inspect(type)}.
It can only be used with a `:belongs_to` or `:has_one` relationship. If you would like to
add the actor to a list, or something else along those lines, use a custom change
along with `Ash.Changeset.manage_relationship`.
"""
end
end
defp actor_or_field(actor, field) when is_nil(field) do
actor
end

View file

@ -31,7 +31,7 @@
"plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"},
"sobelow": {:hex, :sobelow, "0.13.0", "218afe9075904793f5c64b8837cc356e493d88fddde126a463839351870b8d1e", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "cd6e9026b85fc35d7529da14f95e85a078d9dd1907a9097b3ba6ac7ebbe34a0d"},
"sourceror": {:hex, :sourceror, "0.14.1", "c6fb848d55bd34362880da671debc56e77fd722fa13b4dcbeac89a8998fc8b09", [:mix], [], "hexpm", "8b488a219e4c4d7d9ff29d16346fd4a5858085ccdd010e509101e226bbfd8efc"},
"spark": {:hex, :spark, "1.1.52", "e0ddd137899c11fb44ef46cda346a112e60365b93e50264da976f45b1c6e28c5", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.5 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.1", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "2d8b354103eb4ae5fb4ed5f885d491e3ed5684ccb57806c3980fcc15a4b597d6"},
"spark": {:hex, :spark, "1.1.53", "db8a374ef6ada4f38389386bec76b2fa6331d4755308a6e359acad16472e29ea", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.5 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.1", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "5f8a8e2b4abd2544517bb8d29c28576239254b5979d66d9781b154706c4199dd"},
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
"stream_data": {:hex, :stream_data, "0.6.0", "e87a9a79d7ec23d10ff83eb025141ef4915eeb09d4491f79e52f2562b73e5f47", [:mix], [], "hexpm", "b92b5031b650ca480ced047578f1d57ea6dd563f5b57464ad274718c9c29501c"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},