improvement: add callbacks for bulk actions on manual actions

This commit is contained in:
Zach Daniel 2023-12-20 21:23:13 -05:00
parent ed887413d7
commit 0932cf32a7
6 changed files with 77 additions and 6 deletions

View file

@ -903,6 +903,18 @@ defmodule Ash.Actions.Create.Bulk do
must_return_records_for_changes?,
tenant: opts[:tenant]
})
|> Enum.flat_map(fn
{:ok, result} ->
[result]
{:error, error} ->
store_error(ref, error, opts)
[]
{:notifications, notifications} ->
store_notification(ref, notifications, opts)
[]
end)
else
[changeset] = batch

View file

@ -864,6 +864,18 @@ defmodule Ash.Actions.Destroy.Bulk do
must_return_records_for_changes?,
tenant: opts[:tenant]
})
|> Enum.flat_map(fn
{:ok, result} ->
[result]
{:error, error} ->
store_error(ref, error, opts)
[]
{:notifications, notifications} ->
store_notification(ref, notifications, opts)
[]
end)
else
[changeset] = batch
@ -924,9 +936,6 @@ defmodule Ash.Actions.Destroy.Bulk do
{:ok, result} ->
result
:ok ->
[]
{:error, error} ->
store_error(ref, error, opts)
[]

View file

@ -919,6 +919,18 @@ defmodule Ash.Actions.Update.Bulk do
tenant: opts[:tenant]
}
])
|> Enum.flat_map(fn
{:ok, result} ->
[result]
{:error, error} ->
store_error(ref, error, opts)
[]
{:notifications, notifications} ->
store_notification(ref, notifications, opts)
[]
end)
else
[changeset] = batch
@ -977,9 +989,6 @@ defmodule Ash.Actions.Update.Bulk do
{:ok, result} ->
result
:ok ->
[]
{:error, error} ->
store_error(ref, error, opts)
[]

View file

@ -21,6 +21,19 @@ defmodule Ash.Resource.ManualCreate do
| {:ok, Ash.Resource.record(), %{notifications: [Ash.Notifier.Notification.t()]}}
| {:error, term}
@callback bulk_create(
changesets :: Enumerable.t(Ash.Changeset.t()),
opts :: Keyword.t(),
context :: context()
) ::
list(
{:ok, Ash.Resource.record()}
| {:error, Ash.Error.t()}
| {:notifications, list(Ash.Notifier.Notification.t())}
)
@optional_callbacks [bulk_create: 3]
defmacro __using__(_) do
quote do
@behaviour Ash.Resource.ManualCreate

View file

@ -1,6 +1,8 @@
defmodule Ash.Resource.ManualDestroy do
@moduledoc """
A module to implement manual destroy actions.
Note that in the returns of these functions you must return the destroyed record or records.
"""
@type context :: %{
@ -20,6 +22,19 @@ defmodule Ash.Resource.ManualDestroy do
| {:ok, Ash.Resource.record(), list(Ash.Notifier.Notification.t())}
| {:error, term}
@callback bulk_destroy(
changesets :: Enumerable.t(Ash.Changeset.t()),
opts :: Keyword.t(),
context :: context()
) ::
list(
{:ok, Ash.Resource.record()}
| {:error, Ash.Error.t()}
| {:notifications, list(Ash.Notifier.Notification.t())}
)
@optional_callbacks [bulk_destroy: 3]
defmacro __using__(_) do
quote do
@behaviour Ash.Resource.ManualDestroy

View file

@ -20,6 +20,19 @@ defmodule Ash.Resource.ManualUpdate do
| {:ok, Ash.Resource.record(), %{notifications: [Ash.Notifier.Notification.t()]}}
| {:error, term}
@callback bulk_update(
changesets :: Enumerable.t(Ash.Changeset.t()),
opts :: Keyword.t(),
context :: context()
) ::
list(
{:ok, Ash.Resource.record()}
| {:error, Ash.Error.t()}
| {:notifications, list(Ash.Notifier.Notification.t())}
)
@optional_callbacks [bulk_update: 3]
defmacro __using__(_) do
quote do
@behaviour Ash.Resource.ManualUpdate