Skip to content

Commit 85ae614

Browse files
committed
local refactoring
1 parent 812cc7b commit 85ae614

26 files changed

Lines changed: 239 additions & 314 deletions

lib/access/resources/cable.ex

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ defmodule DiffoExample.Access.Cable do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Relationship
14-
alias Diffo.Provider.Extension.Characteristic
15-
alias Diffo.Provider.Assigner
1613
alias Diffo.Provider.Assignment
17-
alias Diffo.Provider.Extension.Pool
1814

1915
alias DiffoExample.Access
2016

@@ -74,15 +70,7 @@ defmodule DiffoExample.Access.Cable do
7470
argument :characteristic_value_updates, {:array, :term}
7571

7672
change set_attribute(:resource_state, :operating)
77-
78-
change after_action(fn changeset, result, _context ->
79-
with {:ok, result} <- Ash.load(result, [:characteristics]),
80-
{:ok, result} <-
81-
Characteristic.update_all(result, changeset, characteristics()),
82-
{:ok, result} <- Pool.update_pools(result, changeset, pools()),
83-
{:ok, result} <- Access.get_cable_by_id(result.id),
84-
do: {:ok, result}
85-
end)
73+
change DiffoExample.Changes.Define
8674
end
8775

8876
update :relate do
@@ -100,11 +88,7 @@ defmodule DiffoExample.Access.Cable do
10088
description "relates the cable with an instance by assigning a pair"
10189
argument :assignment, :struct, constraints: [instance_of: Assignment]
10290

103-
change after_action(fn changeset, result, _context ->
104-
with {:ok, result} <- Assigner.assign(result, changeset, :pairs),
105-
{:ok, result} <- Access.get_cable_by_id(result.id),
106-
do: {:ok, result}
107-
end)
91+
change {DiffoExample.Changes.Assign, pool: :pairs}
10892
end
10993
end
11094
end

lib/access/resources/card.ex

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ defmodule DiffoExample.Access.Card do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Relationship
14-
alias Diffo.Provider.Extension.Characteristic
15-
alias Diffo.Provider.Assigner
1613
alias Diffo.Provider.Assignment
17-
alias Diffo.Provider.Extension.Pool
1814

1915
alias DiffoExample.Access
2016

@@ -74,37 +70,21 @@ defmodule DiffoExample.Access.Card do
7470
argument :characteristic_value_updates, {:array, :term}
7571

7672
change set_attribute(:resource_state, :operating)
77-
78-
change after_action(fn changeset, result, _context ->
79-
with {:ok, result} <- Ash.load(result, [:characteristics]),
80-
{:ok, result} <-
81-
Characteristic.update_all(result, changeset, characteristics()),
82-
{:ok, result} <- Pool.update_pools(result, changeset, pools()),
83-
{:ok, result} <- Access.get_card_by_id(result.id),
84-
do: {:ok, result}
85-
end)
73+
change DiffoExample.Changes.Define
8674
end
8775

8876
update :relate do
8977
description "relates the card with other instances"
9078
argument :relationships, {:array, :struct}
9179

92-
change after_action(fn changeset, result, _context ->
93-
with {:ok, result} <- Relationship.relate_instance(result, changeset),
94-
{:ok, result} <- Access.get_card_by_id(result.id),
95-
do: {:ok, result}
96-
end)
80+
change DiffoExample.Changes.Relate
9781
end
9882

9983
update :assign_port do
10084
description "relates the card with an instance by assigning a port"
10185
argument :assignment, :struct, constraints: [instance_of: Assignment]
10286

103-
change after_action(fn changeset, result, _context ->
104-
with {:ok, result} <- Assigner.assign(result, changeset, :ports),
105-
{:ok, result} <- Access.get_card_by_id(result.id),
106-
do: {:ok, result}
107-
end)
87+
change {DiffoExample.Changes.Assign, pool: :ports}
10888
end
10989
end
11090
end

lib/access/resources/path.ex

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ defmodule DiffoExample.Access.Path do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Relationship
14-
alias Diffo.Provider.Extension.Characteristic
1513

1614
alias DiffoExample.Access
1715

@@ -66,24 +64,14 @@ defmodule DiffoExample.Access.Path do
6664
description "defines the path"
6765
argument :characteristic_value_updates, {:array, :term}
6866

69-
change after_action(fn changeset, result, _context ->
70-
with {:ok, result} <- Ash.load(result, [:characteristics]),
71-
{:ok, result} <-
72-
Characteristic.update_all(result, changeset, characteristics()),
73-
{:ok, result} <- Access.get_path_by_id(result.id),
74-
do: {:ok, result}
75-
end)
67+
change DiffoExample.Changes.Define
7668
end
7769

7870
update :relate do
7971
description "relates the path with other instances"
8072
argument :relationships, {:array, :struct}
8173

82-
change after_action(fn changeset, result, _context ->
83-
with {:ok, result} <- Relationship.relate_instance(result, changeset),
84-
{:ok, result} <- Access.get_path_by_id(result.id),
85-
do: {:ok, result}
86-
end)
74+
change DiffoExample.Changes.Relate
8775
end
8876
end
8977
end

lib/access/resources/shelf.ex

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ defmodule DiffoExample.Access.Shelf do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Relationship
14-
alias Diffo.Provider.Extension.Characteristic
15-
alias Diffo.Provider.Assigner
1613
alias Diffo.Provider.Assignment
17-
alias Diffo.Provider.Extension.Pool
1814

1915
alias DiffoExample.Access
2016

@@ -74,37 +70,21 @@ defmodule DiffoExample.Access.Shelf do
7470
argument :characteristic_value_updates, {:array, :term}
7571

7672
change set_attribute(:resource_state, :operating)
77-
78-
change after_action(fn changeset, result, _context ->
79-
with {:ok, result} <- Ash.load(result, [:characteristics]),
80-
{:ok, result} <-
81-
Characteristic.update_all(result, changeset, characteristics()),
82-
{:ok, result} <- Pool.update_pools(result, changeset, pools()),
83-
{:ok, result} <- Access.get_shelf_by_id(result.id),
84-
do: {:ok, result}
85-
end)
73+
change DiffoExample.Changes.Define
8674
end
8775

8876
update :relate do
8977
description "relates the shelf with cards"
9078
argument :relationships, {:array, :struct}
9179

92-
change after_action(fn changeset, result, _context ->
93-
with {:ok, result} <- Relationship.relate_instance(result, changeset),
94-
{:ok, result} <- Access.get_shelf_by_id(result.id),
95-
do: {:ok, result}
96-
end)
80+
change DiffoExample.Changes.Relate
9781
end
9882

9983
update :assign_slot do
10084
description "relates the shelf with an instance by assigning a slot"
10185
argument :assignment, :struct, constraints: [instance_of: Assignment]
10286

103-
change after_action(fn changeset, result, _context ->
104-
with {:ok, result} <- Assigner.assign(result, changeset, :slots),
105-
{:ok, result} <- Access.get_shelf_by_id(result.id),
106-
do: {:ok, result}
107-
end)
87+
change {DiffoExample.Changes.Assign, pool: :slots}
10888
end
10989
end
11090
end

lib/access/services/dsl_access.ex

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ defmodule DiffoExample.Access.DslAccess do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Extension.Characteristic
1413
alias Diffo.Provider.Instance.Place
1514

1615
alias DiffoExample.Access
@@ -100,14 +99,7 @@ defmodule DiffoExample.Access.DslAccess do
10099
argument :characteristic_value_updates, {:array, :term}
101100

102101
change transition_state(:reserved)
103-
104-
change after_action(fn changeset, result, _context ->
105-
with {:ok, result} <- Ash.load(result, [:characteristics]),
106-
{:ok, result} <-
107-
Characteristic.update_all(result, changeset, characteristics()),
108-
{:ok, result} <- Access.get_dsl_by_id(result.id),
109-
do: {:ok, result}
110-
end)
102+
change DiffoExample.Changes.Define
111103
end
112104
end
113105
end

lib/access/util.ex

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Changes.Assign do
6+
@moduledoc """
7+
After-action for `:assign_*`-style update actions on Diffo Instance resources
8+
that declare pools.
9+
10+
Applies the `:assignment` argument via `Assigner.assign/3` against the named
11+
pool, then reloads the instance through its primary read so preparations
12+
re-run.
13+
14+
Pass the pool name as an option:
15+
16+
update :assign_pair do
17+
argument :assignment, :struct, constraints: [instance_of: Assignment]
18+
change {DiffoExample.Changes.Assign, pool: :pairs}
19+
end
20+
"""
21+
use Ash.Resource.Change
22+
require Ash.Query
23+
24+
alias Diffo.Provider.Assigner
25+
26+
@impl true
27+
def change(changeset, opts, _context) do
28+
pool = Keyword.fetch!(opts, :pool)
29+
30+
Ash.Changeset.after_action(changeset, fn changeset, result ->
31+
mod = result.__struct__
32+
33+
with {:ok, result} <- Assigner.assign(result, changeset, pool),
34+
{:ok, result} <-
35+
mod
36+
|> Ash.Query.for_read(:read)
37+
|> Ash.Query.filter(id == ^result.id)
38+
|> Ash.read_one() do
39+
{:ok, result}
40+
end
41+
end)
42+
end
43+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Changes.Define do
6+
@moduledoc """
7+
After-action for `:define`-style update actions on Diffo Instance resources.
8+
9+
Applies the `:characteristic_value_updates` argument against the resource's
10+
compile-time `characteristics/0` and `pools/0` declarations, then reloads
11+
the instance through its primary read so preparations re-run.
12+
13+
Use it on any instance update action that carries
14+
`argument :characteristic_value_updates, {:array, :term}`:
15+
16+
update :define do
17+
argument :characteristic_value_updates, {:array, :term}
18+
19+
change set_attribute(:resource_state, :operating)
20+
change DiffoExample.Changes.Define
21+
end
22+
23+
Lifecycle transitions (resource_state for resources, transition_state for
24+
services) remain on the action — they are intent-specific.
25+
"""
26+
use Ash.Resource.Change
27+
require Ash.Query
28+
29+
alias Diffo.Provider.Extension.Characteristic
30+
alias Diffo.Provider.Extension.Pool
31+
32+
@impl true
33+
def change(changeset, _opts, _context) do
34+
Ash.Changeset.after_action(changeset, fn changeset, result ->
35+
mod = result.__struct__
36+
37+
with {:ok, result} <- Ash.load(result, [:characteristics]),
38+
{:ok, result} <- Characteristic.update_all(result, changeset, mod.characteristics()),
39+
{:ok, result} <- Pool.update_pools(result, changeset, mod.pools()),
40+
{:ok, result} <-
41+
mod
42+
|> Ash.Query.for_read(:read)
43+
|> Ash.Query.filter(id == ^result.id)
44+
|> Ash.read_one() do
45+
{:ok, result}
46+
end
47+
end)
48+
end
49+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Changes.Relate do
6+
@moduledoc """
7+
After-action for `:relate`-style update actions on Diffo Instance resources.
8+
9+
Applies the `:relationships` argument via `Relationship.relate_instance`,
10+
then reloads the instance through its primary read so preparations re-run.
11+
12+
Use it on any instance update action that carries
13+
`argument :relationships, {:array, :struct}`:
14+
15+
update :relate do
16+
argument :relationships, {:array, :struct}
17+
change DiffoExample.Changes.Relate
18+
end
19+
"""
20+
use Ash.Resource.Change
21+
require Ash.Query
22+
23+
alias Diffo.Provider.Instance.Relationship
24+
25+
@impl true
26+
def change(changeset, _opts, _context) do
27+
Ash.Changeset.after_action(changeset, fn changeset, result ->
28+
mod = result.__struct__
29+
30+
with {:ok, result} <- Relationship.relate_instance(result, changeset),
31+
{:ok, result} <-
32+
mod
33+
|> Ash.Query.for_read(:read)
34+
|> Ash.Query.filter(id == ^result.id)
35+
|> Ash.read_one() do
36+
{:ok, result}
37+
end
38+
end)
39+
end
40+
end

0 commit comments

Comments
 (0)