From f8480ec8e705a2091bf749b6cb8959be0dac051a Mon Sep 17 00:00:00 2001 From: James Harton Date: Fri, 31 May 2024 08:49:59 +1200 Subject: [PATCH] fix: allow auto builds to be replaced by explicit relate commands. --- lib/smokestack/builders/related_builder.ex | 14 +++++++++++++- test/smokestack/dsl_test.exs | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/smokestack/builders/related_builder.ex b/lib/smokestack/builders/related_builder.ex index b80696d..f1595e5 100644 --- a/lib/smokestack/builders/related_builder.ex +++ b/lib/smokestack/builders/related_builder.ex @@ -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) diff --git a/test/smokestack/dsl_test.exs b/test/smokestack/dsl_test.exs index 1debfe0..7f4d156 100644 --- a/test/smokestack/dsl_test.exs +++ b/test/smokestack/dsl_test.exs @@ -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