Alternera.Resource.UI.Actions (alternera_resource_ui v0.9.0)

Copy Markdown View Source

The declared actions of a resource (action :id, "Label" in its ui): who may run one, what it asks for, and running it. The host's buttons, its dialog (Alternera.Resource.UI.ActionRunner) and the contract kit's harness all run actions through here, so an action behaves the same wherever it is drawn.

An action with run: runs that Ash action of the resource with the context's tenant and actor: the policies decide, the history records the change. Its input is the Ash action's public arguments and the attributes it accepts. An action with handler: calls Alternera.Resource.UI.ActionHandler.handle_action/4 with the arguments it declares. A selection runs the action once per record, so each record gets its own policy check and version.

Summary

Types

An input the dialog asks for.

What running an action came to.

Functions

Whether the viewer may run action: a read-only action always, any other only with can_write?. The resource's policies still decide when it runs.

The dialog's params (string keys) cast to the inputs' types: {:ok, input} with atom keys, or {:error, %{name => message}}. A blank value is nil; a required input that is nil is refused; a missing one takes its default.

The inputs the dialog asks for, in order.

Whether to offer action for record (nil for the resource or a selection): allowed, and its visible function, if any, says yes. A visible function that raises is logged and hides the action.

The actions of actions that act on on (:resource, :record or :selection).

What the dialog shows first for target: on one record, each input that is one of its attributes holds the record's value (string keys, as the dialog's params), so an update starts from what is there. Elsewhere nothing; the inputs show their defaults.

Runs action on target (:resource, {:record, record} or {:selection, records}) with the cast input. Every error is logged.

Types

input()

@type input() :: %{
  name: atom(),
  type: module(),
  constraints: keyword(),
  label: String.t(),
  description: String.t() | nil,
  default: term(),
  required?: boolean()
}

An input the dialog asks for.

result()

@type result() :: %{
  status: :ok | :error,
  message: String.t(),
  failed: [%{key: String.t(), message: String.t()}],
  open: tuple() | nil,
  download: {String.t(), iodata()} | nil
}

What running an action came to.

Functions

allowed?(action, arg2)

Whether the viewer may run action: a read-only action always, any other only with can_write?. The resource's policies still decide when it runs.

cast(inputs, params, opts \\ [])

@spec cast([input()], map(), keyword()) ::
  {:ok, map()} | {:error, %{required(atom()) => String.t()}}

The dialog's params (string keys) cast to the inputs' types: {:ok, input} with atom keys, or {:error, %{name => message}}. A blank value is nil; a required input that is nil is refused; a missing one takes its default.

With clear: true (one record, its values prefilled) an optional input without a default that the reader emptied is passed as nil, so the action clears it; otherwise a blank input is left out (unchanged).

inputs(resource, action)

The inputs the dialog asks for, in order.

offered?(action, record, ctx)

Whether to offer action for record (nil for the resource or a selection): allowed, and its visible function, if any, says yes. A visible function that raises is logged and hides the action.

on(actions, on)

The actions of actions that act on on (:resource, :record or :selection).

prefill(inputs, arg2)

@spec prefill([input()], :resource | {:record, struct()} | {:selection, [struct()]}) ::
  map()

What the dialog shows first for target: on one record, each input that is one of its attributes holds the record's value (string keys, as the dialog's params), so an update starts from what is there. Elsewhere nothing; the inputs show their defaults.

run(resource, action, target, input, ctx)

@spec run(
  module(),
  Alternera.Resource.Extension.Action.t(),
  :resource | {:record, struct()} | {:selection, [struct()]},
  map(),
  Alternera.Resource.UI.Context.t()
) :: result()

Runs action on target (:resource, {:record, record} or {:selection, records}) with the cast input. Every error is logged.