View Source ExTeal.Action behaviour (ExTeal v0.28.1)

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'.

Summary

Callbacks

Override the default key for the action

Override the default title for the action

Types

@type action_responses() :: :ok | {:error, String.t()} | ExTeal.ActionResponse.t()

Callbacks

@callback commit(Plug.Conn.t(), [ExTeal.Field.t()], Ecto.Query.t()) :: action_responses()
@callback key() :: String.t()

Override the default key for the action

@callback options(Plug.Conn.t()) :: map()
@callback title() :: String.t()

Override the default title for the action

Functions