improvement: simplify atomic state transition with new ash feature

This commit is contained in:
Zach Daniel 2024-07-13 16:13:54 -04:00
parent 75ae32a6dd
commit d9821e158b
3 changed files with 24 additions and 27 deletions

View file

@ -11,6 +11,7 @@ defmodule AshStateMachine.BuiltinChanges.TransitionState do
AshStateMachine.Info.state_machine_transitions(changeset.resource, changeset.action.name)
attribute = AshStateMachine.Info.state_machine_state_attribute!(changeset.resource)
old_state = expr(^ref(attribute))
target = opts[:target]
all_states = AshStateMachine.Info.state_machine_all_states(changeset.resource)
@ -25,22 +26,16 @@ defmodule AshStateMachine.BuiltinChanges.TransitionState do
^old_state in ^List.wrap(transition.from) and ^target in ^List.wrap(transition.to)
)
if is_nil(expr) do
state_expr
else
expr(^state_expr or ^expr)
end
end)
new_state_value =
has_matching_transition =
{:atomic, [], expr(not (^states_expr)),
expr(
cond do
^target not in ^all_states ->
error(
AshStateMachine.Errors.NoMatchingTransition,
%{old_state: ^old_state, target: ^target, action: ^changeset.action.name}
)
^states_expr ->
^opts[:target]
true ->
error(
AshStateMachine.Errors.NoMatchingTransition,
%{
@ -49,10 +44,12 @@ defmodule AshStateMachine.BuiltinChanges.TransitionState do
action: ^changeset.action.name
}
)
end
)
)}
{:atomic, %{attribute => new_state_value}}
{:atomic, changeset, %{attribute => opts[:target]},
[
has_matching_transition
]}
end
end
end

View file

@ -126,7 +126,7 @@ defmodule AshStateMachine.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ash, ash_version("~> 3.0")},
{:ash, ash_version("~> 3.0 and >= 3.1.4")},
{:simple_sat, "~> 0.1", only: [:dev, :test], runtime: false},
{:ex_doc, github: "elixir-lang/ex_doc", only: [:dev, :test], runtime: false},
{:ex_check, "~> 0.12", only: [:dev, :test]},

View file

@ -1,5 +1,5 @@
%{
"ash": {:hex, :ash, "3.1.3", "c5c65e18107247df4857951fa546e720c6e2ef0afde07ad0c2f523725a751eb2", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, "~> 0.2.12", [hex: :igniter, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, ">= 0.8.1 and < 1.0.0-0", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.7 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "530c04f32b2562352e48c92fab50bc837819c6cd3453c4fa9c9842b2e9d8483b"},
"ash": {:hex, :ash, "3.1.4", "5e77bd66d3008b9d03ce1d172cebe6a7a916ea61a715b1a84421819c70cda85a", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, "~> 0.2.12", [hex: :igniter, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, ">= 0.8.1 and < 1.0.0-0", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.7 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "8c5e1b777fdd2ee488c60ea973aa9e1d0da255d2bd362a547482c047f9f0c362"},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"castore": {:hex, :castore, "1.0.8", "dedcf20ea746694647f883590b82d9e96014057aff1d44d03ec90f36a5c0dc6e", [:mix], [], "hexpm", "0b2b66d2ee742cb1d9cb8c8be3b43c3a70ee8651f37b75a8b982e036752983f1"},
"comparable": {:hex, :comparable, "1.0.0", "bb669e91cedd14ae9937053e5bcbc3c52bb2f22422611f43b6e38367d94a495f", [:mix], [{:typable, "~> 0.1", [hex: :typable, repo: "hexpm", optional: false]}], "hexpm", "277c11eeb1cd726e7cd41c6c199e7e52fa16ee6830b45ad4cdc62e51f62eb60c"},