Skip to content

Commit 620624c

Browse files
committed
fix domain boundary in build_after — eliminate reload workaround
relate_instance functions now pass %Instance{id: result.id} to Provider calls rather than converting the domain-specific struct via Map.from_struct. This respects Ash's static relationship contracts (Feature.instance_id belongs to Diffo.Provider.Instance, not Shelf or Card). build_after/2 ends with Ash.load/2 to load relationships back onto the correct domain struct, replacing the per-domain reload that was masking the struct type loss. build_after/4 removed entirely. Also replaces Map.put with struct update syntax and drops dead-looking aliases in favour of fully qualified module names in DSL options.
1 parent 7386b03 commit 620624c

10 files changed

Lines changed: 23 additions & 55 deletions

File tree

lib/diffo/provider/components/base_instance.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ defmodule Diffo.Provider.BaseInstance do
6969
7070
create :build do
7171
change after_action(fn changeset, result, _context ->
72-
ActionHelper.build_after(changeset, result, MyApp.Domain, :get_cluster_by_id)
72+
ActionHelper.build_after(changeset, result)
7373
end)
7474
end
7575
"""

lib/diffo/provider/components/instance/extension/action_helper.ex

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ defmodule Diffo.Provider.Instance.ActionHelper do
2525
@doc """
2626
build after_action helper, relates TMF entities to the new instance
2727
"""
28-
def build_after(changeset, result, module, function) do
29-
with {:ok, result} <- Specification.relate_instance(result, changeset),
30-
{:ok, result} <- Relationship.relate_instance(result, changeset),
31-
{:ok, result} <- Feature.relate_instance(result, changeset),
32-
{:ok, result} <- Characteristic.relate_instance(result, changeset),
33-
{:ok, result} <- Place.relate_instance(result, changeset),
34-
{:ok, result} <- Party.relate_instance(result, changeset),
35-
{:ok, result} <- apply(module, function, [result.id]),
36-
do: {:ok, result}
28+
def build_after(changeset, result) do
29+
specified_by = Ash.Changeset.get_argument(changeset, :specified_by)
30+
result = %{result | specification_id: specified_by}
31+
32+
with {:ok, _} <- Specification.relate_instance(result, changeset),
33+
{:ok, _} <- Relationship.relate_instance(result, changeset),
34+
{:ok, _} <- Feature.relate_instance(result, changeset),
35+
{:ok, _} <- Characteristic.relate_instance(result, changeset),
36+
{:ok, _} <- Place.relate_instance(result, changeset),
37+
{:ok, _} <- Party.relate_instance(result, changeset),
38+
do: Ash.load(result, [:specification, :characteristics, :features, :parties])
3739
end
3840
end

lib/diffo/provider/components/instance/extension/characteristic.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ defmodule Diffo.Provider.Instance.Characteristic do
7373
def relate_instance(result, changeset)
7474
when is_struct(result) and is_struct(changeset, Ash.Changeset) do
7575
characteristics = Ash.Changeset.get_argument(changeset, :characteristics)
76-
instance = struct(Instance, Map.from_struct(result))
77-
Provider.relate_instance_characteristics(instance, %{characteristics: characteristics})
76+
Provider.relate_instance_characteristics(%Instance{id: result.id}, %{characteristics: characteristics})
7877
end
7978

8079
@doc """

lib/diffo/provider/components/instance/extension/feature.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ defmodule Diffo.Provider.Instance.Feature do
101101
def relate_instance(result, changeset)
102102
when is_struct(result) and is_struct(changeset, Ash.Changeset) do
103103
features = Ash.Changeset.get_argument(changeset, :features)
104-
instance = struct(Instance, Map.from_struct(result))
105-
Provider.relate_instance_features(instance, %{features: features})
104+
Provider.relate_instance_features(%Instance{id: result.id}, %{features: features})
106105
end
107106

108107
defimpl String.Chars do

lib/diffo/provider/components/instance/extension/specification.ex

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,7 @@ defmodule Diffo.Provider.Instance.Specification do
5252
def relate_instance(result, changeset)
5353
when is_struct(result) and is_struct(changeset, Ash.Changeset) do
5454
specified_by = Ash.Changeset.get_argument(changeset, :specified_by)
55-
instance = struct(Instance, Map.from_struct(result))
56-
57-
case Provider.specify_instance(instance, %{specified_by: specified_by}) do
58-
{:ok, specification} ->
59-
{:ok,
60-
result
61-
|> Map.put(:specification, specification)
62-
|> Map.put(:specification_id, specified_by)}
63-
64-
{:error, error} ->
65-
{:error, error}
66-
end
55+
Provider.specify_instance(%Instance{id: result.id}, %{specified_by: specified_by})
6756
end
6857

6958
defimpl String.Chars do

test/support/resource/card.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ defmodule Diffo.Test.Card do
5858
end)
5959

6060
change after_action(fn changeset, result, _context ->
61-
ActionHelper.build_after(changeset, result, Servo, :get_card_by_id)
61+
ActionHelper.build_after(changeset, result)
6262
end)
6363

6464
change load [:href]

test/support/resource/invalid/invalid_characteristic.ex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ defmodule Diffo.Test.InvalidCharacteristic do
1212
alias Diffo.Provider.BaseInstance
1313
alias Diffo.Provider.Instance.ActionHelper
1414

15-
alias Diffo.Test.Servo
16-
1715
use Ash.Resource,
1816
fragments: [BaseInstance],
19-
domain: Servo
17+
domain: Diffo.Test.Servo
2018

2119
resource do
2220
description "Ash Resource with an invalid characteristic"
@@ -51,12 +49,7 @@ defmodule Diffo.Test.InvalidCharacteristic do
5149
end)
5250

5351
change after_action(fn changeset, result, _context ->
54-
ActionHelper.build_after(
55-
changeset,
56-
result,
57-
Servo,
58-
:get_invalid_characteristic_by_id
59-
)
52+
ActionHelper.build_after(changeset, result)
6053
end)
6154

6255
change load [:href]

test/support/resource/invalid/invalid_feature_characteristic.ex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ defmodule Diffo.Test.InvalidFeatureCharacteristic do
1212
alias Diffo.Provider.BaseInstance
1313
alias Diffo.Provider.Instance.ActionHelper
1414

15-
alias Diffo.Test.Servo
16-
1715
use Ash.Resource,
1816
fragments: [BaseInstance],
19-
domain: Servo
17+
domain: Diffo.Test.Servo
2018

2119
resource do
2220
description "Ash Resource with an invalid feature characteristic"
@@ -54,12 +52,7 @@ defmodule Diffo.Test.InvalidFeatureCharacteristic do
5452
end)
5553

5654
change after_action(fn changeset, result, _context ->
57-
ActionHelper.build_after(
58-
changeset,
59-
result,
60-
Servo,
61-
:get_invalid_feature_characteristic_by_id
62-
)
55+
ActionHelper.build_after(changeset, result)
6356
end)
6457

6558
change load [:href]

test/support/resource/invalid/invalid_specification.ex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ defmodule Diffo.Test.InvalidSpecification do
1212
alias Diffo.Provider.BaseInstance
1313
alias Diffo.Provider.Instance.ActionHelper
1414

15-
alias Diffo.Test.Servo
16-
1715
use Ash.Resource,
1816
fragments: [BaseInstance],
19-
domain: Servo
17+
domain: Diffo.Test.Servo
2018

2119
resource do
2220
description "Ash Resource with an invalid specification"
@@ -47,12 +45,7 @@ defmodule Diffo.Test.InvalidSpecification do
4745
end)
4846

4947
change after_action(fn changeset, result, _context ->
50-
ActionHelper.build_after(
51-
changeset,
52-
result,
53-
Servo,
54-
:get_invalid_specification_by_id
55-
)
48+
ActionHelper.build_after(changeset, result)
5649
end)
5750

5851
change load [:href]

test/support/resource/shelf.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ defmodule Diffo.Test.Shelf do
7878
end)
7979

8080
change after_action(fn changeset, result, _context ->
81-
ActionHelper.build_after(changeset, result, Servo, :get_shelf_by_id)
81+
ActionHelper.build_after(changeset, result)
8282
end)
8383

8484
change load [:href]

0 commit comments

Comments
 (0)