mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
fix: run action changes on destroy
test: test manual actions fix: pattern match manage_relationship notifications fix
This commit is contained in:
parent
7d37f8ae3d
commit
097447bf38
6 changed files with 107 additions and 7 deletions
|
@ -222,8 +222,9 @@ defmodule Ash.Actions.Create do
|
||||||
|> add_tenant(changeset)
|
|> add_tenant(changeset)
|
||||||
|> manage_relationships(api, changeset, engine_opts)
|
|> manage_relationships(api, changeset, engine_opts)
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, result} ->
|
{:ok, result, %{notifications: new_notifications}} ->
|
||||||
{:ok, result, instructions}
|
{:ok, result,
|
||||||
|
Map.update!(instructions, :notifications, &(&1 ++ new_notifications))}
|
||||||
|
|
||||||
{:error, error} ->
|
{:error, error} ->
|
||||||
{:error, error}
|
{:error, error}
|
||||||
|
@ -249,7 +250,7 @@ defmodule Ash.Actions.Create do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp manage_relationships({:ok, nil}, _, _, _) do
|
defp manage_relationships({:ok, nil}, _, _, _) do
|
||||||
{:ok, nil}
|
{:ok, nil, %{notifications: []}}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp manage_relationships({:ok, created}, api, changeset, engine_opts) do
|
defp manage_relationships({:ok, created}, api, changeset, engine_opts) do
|
||||||
|
|
|
@ -157,7 +157,7 @@ defmodule Ash.Actions.Update do
|
||||||
|
|
||||||
if changeset.valid? do
|
if changeset.valid? do
|
||||||
if action.manual? do
|
if action.manual? do
|
||||||
{:ok, nil}
|
{:ok, changeset.data}
|
||||||
else
|
else
|
||||||
resource
|
resource
|
||||||
|> Ash.DataLayer.update(changeset)
|
|> Ash.DataLayer.update(changeset)
|
||||||
|
@ -178,8 +178,9 @@ defmodule Ash.Actions.Update do
|
||||||
|> add_tenant(changeset)
|
|> add_tenant(changeset)
|
||||||
|> manage_relationships(api, changeset, engine_opts)
|
|> manage_relationships(api, changeset, engine_opts)
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, data} ->
|
{:ok, data, %{notifications: new_notifications}} ->
|
||||||
{:ok, data, instructions}
|
{:ok, data,
|
||||||
|
Map.update!(instructions, :notifications, &(&1 ++ new_notifications))}
|
||||||
|
|
||||||
{:error, error} ->
|
{:error, error} ->
|
||||||
{:error, error}
|
{:error, error}
|
||||||
|
|
|
@ -430,6 +430,7 @@ defmodule Ash.Changeset do
|
||||||
|> Map.put(:__validated_for_action__, action.name)
|
|> Map.put(:__validated_for_action__, action.name)
|
||||||
|> Map.put(:action, action)
|
|> Map.put(:action, action)
|
||||||
|> cast_params(action, params, opts)
|
|> cast_params(action, params, opts)
|
||||||
|
|> run_action_changes(action, opts[:actor])
|
||||||
|> add_validations()
|
|> add_validations()
|
||||||
|> mark_validated(action.name)
|
|> mark_validated(action.name)
|
||||||
|> require_arguments(action)
|
|> require_arguments(action)
|
||||||
|
|
|
@ -90,6 +90,20 @@ defmodule Ash.Test.Actions.CreateTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defmodule ManualCreateAuthor do
|
||||||
|
@moduledoc false
|
||||||
|
use Ash.Resource.Change
|
||||||
|
|
||||||
|
def change(changeset, _, _) do
|
||||||
|
Ash.Changeset.after_action(changeset, fn _, nil ->
|
||||||
|
{:ok,
|
||||||
|
Ash.Test.Actions.CreateTest.Author
|
||||||
|
|> Ash.Changeset.for_create(:create, %{name: "manual"})
|
||||||
|
|> Ash.Test.Actions.CreateTest.Api.create!()}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defmodule Author do
|
defmodule Author do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
||||||
|
@ -110,6 +124,12 @@ defmodule Ash.Test.Actions.CreateTest do
|
||||||
change {DuplicateName, []}
|
change {DuplicateName, []}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create :manual_create do
|
||||||
|
accept []
|
||||||
|
manual? true
|
||||||
|
change ManualCreateAuthor
|
||||||
|
end
|
||||||
|
|
||||||
update :update
|
update :update
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -328,6 +348,16 @@ defmodule Ash.Test.Actions.CreateTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "manual creates" do
|
||||||
|
test "the manual action succeeds" do
|
||||||
|
Author
|
||||||
|
|> Ash.Changeset.for_create(:manual_create)
|
||||||
|
|> Api.create!()
|
||||||
|
|
||||||
|
assert [%{name: "manual"}] = Api.read!(Author)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "require_attributes" do
|
describe "require_attributes" do
|
||||||
test "it requires attributes that have a default" do
|
test "it requires attributes that have a default" do
|
||||||
assert_raise Ash.Error.Invalid, ~r/attribute tag is required/, fn ->
|
assert_raise Ash.Error.Invalid, ~r/attribute tag is required/, fn ->
|
||||||
|
|
|
@ -29,6 +29,19 @@ defmodule Ash.Test.Actions.DestroyTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defmodule ManualDestroyAuthor do
|
||||||
|
@moduledoc false
|
||||||
|
use Ash.Resource.Change
|
||||||
|
|
||||||
|
def change(changeset, _, _) do
|
||||||
|
Ash.Changeset.after_action(changeset, fn _changeset, data ->
|
||||||
|
Ash.Test.Actions.DestroyTest.Api.destroy!(data)
|
||||||
|
|
||||||
|
{:ok, data}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defmodule Author do
|
defmodule Author do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
use Ash.Resource, data_layer: Ash.DataLayer.Ets, authorizers: [Ash.Test.Authorizer]
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets, authorizers: [Ash.Test.Authorizer]
|
||||||
|
@ -41,7 +54,16 @@ defmodule Ash.Test.Actions.DestroyTest do
|
||||||
read :read
|
read :read
|
||||||
create :create
|
create :create
|
||||||
update :update
|
update :update
|
||||||
destroy :destroy
|
|
||||||
|
destroy :destroy do
|
||||||
|
primary? true
|
||||||
|
end
|
||||||
|
|
||||||
|
destroy :manual do
|
||||||
|
accept []
|
||||||
|
manual? true
|
||||||
|
change ManualDestroyAuthor
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes do
|
attributes do
|
||||||
|
@ -129,4 +151,17 @@ defmodule Ash.Test.Actions.DestroyTest do
|
||||||
assert Api.get!(Author, author.id)
|
assert Api.get!(Author, author.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "manual destroy" do
|
||||||
|
test "allows destroying a record" do
|
||||||
|
author =
|
||||||
|
Author
|
||||||
|
|> new(%{name: "foo"})
|
||||||
|
|> Api.create!()
|
||||||
|
|
||||||
|
assert Api.destroy!(author, action: :manual) == :ok
|
||||||
|
|
||||||
|
refute Api.get!(Author, author.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -67,6 +67,21 @@ defmodule Ash.Test.Actions.UpdateTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defmodule ManualUpdateAuthor do
|
||||||
|
@moduledoc false
|
||||||
|
use Ash.Resource.Change
|
||||||
|
|
||||||
|
def change(changeset, _, _) do
|
||||||
|
Ash.Changeset.after_action(changeset, fn _changeset, data ->
|
||||||
|
{:ok,
|
||||||
|
data
|
||||||
|
|> Ash.Changeset.new()
|
||||||
|
|> Ash.Changeset.change_attribute(:name, "manual")
|
||||||
|
|> Ash.Test.Actions.UpdateTest.Api.update!()}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defmodule Author do
|
defmodule Author do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
||||||
|
@ -87,6 +102,12 @@ defmodule Ash.Test.Actions.UpdateTest do
|
||||||
update :duplicate_name do
|
update :duplicate_name do
|
||||||
change {DuplicateName, []}
|
change {DuplicateName, []}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
update :manual_update do
|
||||||
|
accept []
|
||||||
|
manual? true
|
||||||
|
change ManualUpdateAuthor
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes do
|
attributes do
|
||||||
|
@ -188,6 +209,17 @@ defmodule Ash.Test.Actions.UpdateTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "manual updates" do
|
||||||
|
test "the update occurs properly" do
|
||||||
|
author =
|
||||||
|
Author
|
||||||
|
|> new(%{name: "auto"})
|
||||||
|
|> Api.create!()
|
||||||
|
|
||||||
|
assert %Author{name: "manual"} = author |> new() |> Api.update!(action: :manual_update)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "allow_nil?" do
|
describe "allow_nil?" do
|
||||||
test "it does not allow updating a value to `nil` when `allow_nil?: false`" do
|
test "it does not allow updating a value to `nil` when `allow_nil?: false`" do
|
||||||
profile =
|
profile =
|
||||||
|
|
Loading…
Reference in a new issue