fix: allow auto builds to be replaced by explicit relate commands.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
James Harton 2024-05-31 08:49:59 +12:00 committed by James Harton
parent d5be51556d
commit f8480ec8e7
2 changed files with 14 additions and 2 deletions

View file

@ -146,11 +146,11 @@ defmodule Smokestack.RelatedBuilder do
|> Keyword.get(:build, [])
|> List.wrap()
|> Enum.concat(factory.auto_build)
|> List.wrap()
|> Enum.map(fn
{key, value} -> {key, value}
key when is_atom(key) -> {key, []}
end)
|> remove_explicit_relates(options)
|> Enum.reduce_while({:ok, attrs}, fn {relationship, nested_builds}, {:ok, attrs} ->
case build_related(
attrs,
@ -164,6 +164,18 @@ defmodule Smokestack.RelatedBuilder do
end)
end
defp remove_explicit_relates(builds, options) do
relates =
options[:relate]
|> List.wrap()
|> Map.new()
builds
|> Enum.reject(fn {key, _value} ->
is_map_key(relates, key)
end)
end
defp build_related(attrs, relationship, factory, options) do
ash_relationship = Resource.Info.relationship(factory.resource, relationship)
build_related(attrs, relationship, factory, options, ash_relationship)

View file

@ -198,7 +198,7 @@ defmodule Smokestack.DslTest do
post = AutoBuildRelateFactory.insert!(Post, relate: [author: author])
# The auto-build should not be used - the existing author should be used
assert Ash.count(Author) == 1
assert Ash.count!(Author, domain: Support.Domain) == 1
assert post.author.id == author.id
end