ash_archival/documentation/topics/unarchiving.md

42 lines
1 KiB
Markdown
Raw Normal View History

2024-01-13 09:02:02 +13:00
# Un-archiving
2023-01-18 11:16:52 +13:00
If you want to unarchive a resource that uses a base filter, you will need to define a separate resource that uses the same storage and has no base filter. The rest of this guide applies for folks who _aren't_ using a `base_filter`.
Un-archiving can be accomplished by creating a read action that is skipped, using `exclude_read_actions`. Then, you can create an update action that sets that attribute to `nil`. For example:
2023-01-18 11:16:52 +13:00
```elixir
archive do
...
exclude_read_actions :archived
end
actions do
read :archived do
filter expr(not is_nil(archived_at))
2023-01-18 11:16:52 +13:00
end
update :unarchive do
update set_attribute(:archived_at, nil)
end
2023-01-18 11:16:52 +13:00
end
```
You could then do something like this:
2023-01-18 11:16:52 +13:00
```elixir
Resource
|> Ash.get!(id, action: :archived)
2024-05-21 10:54:31 +12:00
|> Ash.Changeset.for_update(:unarchive, %{})
|> Ash.update!()
```
More idiomatically, you would define a code interfaceon the domain, and call that:
```elixir
# to unarchive by `id`
Resource
|> Ash.Query.for_read(:archived, %{})
|> Ash.Query.filter(id == ^id)
|> Domain.unarchive!()
2024-03-30 03:19:24 +13:00
```