Skip to content

Commit

Permalink
docs: update action docs w/ basic calling examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Feb 21, 2025
1 parent b204b1a commit 248fc34
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
15 changes: 15 additions & 0 deletions documentation/topics/actions/destroy-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,26 @@ end

For a full list of all of the available options for configuring destroy actions, see [the Ash.Resource.Dsl documentation](dsl-ash-resource.html#actions-destroy).

## Calling Destroy Actions

The basic formula for calling a destroy action looks like this:

```elixir
record
|> Ash.Query.for_destroy(:action_name, %{argument: :value}, ...opts)
|> Ash.destroy!()
```

See below for variations on action calling, and see the [code interface guide](/documentation/topics/code-interfaces.md) guide for how to
define idiomatic and convenient functions that call your actions.


## Returning the destroyed record

You can use the `return_destroyed?` option to return the destroyed record.

```elixir
# when a resource is passed, or a query w/ no action, the primary destroy action is used.
ticket = Ash.get!(Ticket, 1)
Ash.destroy!(ticket)
# => :ok
Expand Down
13 changes: 13 additions & 0 deletions documentation/topics/actions/generic-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ A generic action declares its arguments, return type, and implementation, as ill
For a full list of all of the available options for configuring generic actions, see [the Ash.Resource.Dsl documentation](dsl-ash-resource.html#actions-action).
## Calling Generic Actions
The basic formula for calling a generic action looks like this:
```elixir
Resource
|> Ash.ActionInput.for_action(:action_name, %{argument: :value}, ...opts)
|> Ash.run_action!()
```
See the [code interface guide](/documentation/topics/code-interfaces.md) guide for how to
define idiomatic and convenient functions that call your actions.

## Why use generic actions?

The example above could be written as a normal function in elixir, i.e
Expand Down
17 changes: 16 additions & 1 deletion documentation/topics/actions/read-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,33 @@ end

For a full list of all of the available options for configuring read actions, see [the Ash.Resource.Dsl documentation](dsl-ash-resource.html#actions-read).

## Calling Read Actions

The basic formula for calling a read action looks like this:

```elixir
Resource
|> Ash.Query.for_read(:action_name, %{argument: :value}, ...opts)
|> Ash.read!()
```

See below for variations on action calling, and see the [code interface guide](/documentation/topics/code-interfaces.md) guide for how to
define idiomatic and convenient functions that call your actions.

## Ash.get!

The `Ash.get!` function is a convenience function for running a read action, filtering by a unique identifier, and expecting only a single result. It is equivalent to the following code:

```elixir
Ash.get!(Resource, 1)
# action can be omitted to use the primary read action
Ash.get!(Resource, 1, action: :read_action)

# is roughly equivalent to

Resource
|> Ash.Query.filter(id == 1)
|> Ash.Query.limit(2)
|> Ash.Query.for_read(:read_action, %{})
|> Ash.read!()
|> case do
[] -> # raise not found error
Expand Down

0 comments on commit 248fc34

Please sign in to comment.