ExTeal.Action behaviour (ExTeal v0.21.0) View Source
ExTeal Actions allow you to perform one off tasks on your ExTeal Index resources with custom conditions. For example you might want to batch update a group of articles to a published state.
Each ExTeal action should contain a commit/3
function.
Fields have not been implemented yet
The commit function is responsible for modifying the state of the application to achieve the desired actions.
Let's look at the PublishAction
action:
defmodule PortfolioWeb.ExTeal.PublishAction do
use ExTeal.Action
alias ExTeal.ActionResponse
def title, do: "Publish" // defaults to Publish Action
def key, do: "publish" // default to publish-action
def commit(conn, fields, query) do
resources = Repo.all(query) //having the raw query gives you the option to batch large requests.
with {:ok, _results} <- Portfolio.Content.publish(resources) do //ideally the updates here are part of a Repo.transaction.
ActionResponse.success("Successfully published")
_ ->
ActionResponse.error("Error publishing")
end
end
end
The commit should return an ActionResponse struct. The ActionResponse types that are available are 'success', 'error', 'redirect', 'download' and 'push'.
Link to this section Summary
Link to this section Types
Specs
action_responses() :: :ok | {:error, String.t()} | ExTeal.ActionResponse.t()
Link to this section Callbacks
Specs
commit(Plug.Conn.t(), [ExTeal.Field.t()], Ecto.Query.t()) :: action_responses()
Specs
key() :: String.t()
Override the default key for the action
Specs
options(Plug.Conn.t()) :: map()
Specs
title() :: String.t()
Override the default title for the action