MishkaGervaz.Table.Web.Events.RecordHandler behaviour (MishkaGervaz v0.0.2)

Copy Markdown View Source

Handles record operations for Events module.

This module provides functions for fetching, deleting, unarchiving, and permanently destroying records.

Every write runs as state.current_user with authorize?: true, so the action's policies and validations both run. Give a record a write of your own only with authorize?: true; pass authorize?: false from seeds and boot code, never from a table event.

Customization

You can create a custom RecordHandler:

defmodule MyApp.CustomRecordHandler do
  use MishkaGervaz.Table.Web.Events.RecordHandler

  # Custom delete that adds audit logging
  def delete_record(state, record) do
    result = super(state, record)
    MyApp.AuditLog.log_delete(record, state.current_user)
    result
  end
end

Then configure it in your resource's DSL:

mishka_gervaz do
  table do
    events do
      record MyApp.CustomRecordHandler
    end
  end
end

See MishkaGervaz.Table.Web.Events, MishkaGervaz.Table.Web.DataLoader, and the sibling handlers SanitizationHandler, SelectionHandler, BulkActionHandler, HookRunner, RelationFilterHandler.

Summary

Callbacks

Deletes a record using the source destroy action.

Destroys a record with a specific Ash action.

Fetches a record by ID.

Permanently destroys an archived record.

Restores an archived record.

Updates a record with a specific Ash action.

Types

archive_status()

@type archive_status() :: :active | :archived

record_struct()

@type record_struct() :: struct()

state()

@type state() :: MishkaGervaz.Table.Web.State.t()

Callbacks

delete_record(state, record)

@callback delete_record(state :: state(), record :: record_struct()) ::
  {:ok, record_struct()} | {:error, term()}

Deletes a record using the source destroy action.

Returns {:ok, deleted_record} or {:error, reason}.

destroy_record(state, record, action)

@callback destroy_record(
  state :: state(),
  record :: record_struct(),
  action :: atom() | {atom(), atom()}
) ::
  {:ok, record_struct()} | {:error, term()}

Destroys a record with a specific Ash action.

The action can be an atom or tuple {master_action, tenant_action}. Returns {:ok, destroyed_record} or {:error, reason}.

get_record(state, id, archive_status)

@callback get_record(
  state :: state(),
  id :: String.t(),
  archive_status :: archive_status()
) ::
  record_struct()

Fetches a record by ID.

Uses the appropriate action based on archive status and user type.

permanent_destroy_record(state, record)

@callback permanent_destroy_record(state :: state(), record :: record_struct()) ::
  {:ok, record_struct()} | {:error, term()}

Permanently destroys an archived record.

Returns {:ok, destroyed_record} or {:error, reason}.

unarchive_record(state, record)

@callback unarchive_record(state :: state(), record :: record_struct()) ::
  {:ok, record_struct()} | {:error, term()}

Restores an archived record.

Returns {:ok, restored_record} or {:error, reason}.

update_record(state, record, action)

@callback update_record(
  state :: state(),
  record :: record_struct(),
  action :: atom() | {atom(), atom()}
) ::
  {:ok, record_struct()} | {:error, term()}

Updates a record with a specific Ash action.

The action can be an atom or tuple {master_action, tenant_action}. Returns {:ok, updated_record} or {:error, reason}.