docs: Clarify resource code in Actions guide (#1156)

This commit is contained in:
Rebecca Le 2024-05-13 19:30:59 +08:00 committed by GitHub
parent 12de8d19a5
commit 6afd9f57f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -98,7 +98,9 @@ end
# in the resource
actions do
defaults [:read, ...]
end
```
And here is the "right way", where the rules about getting the top tickets have been moved into the resource as a nicely named action, and included in the `code_interface` of that resource. The reality of the situation is that `top_tickets/1` is meant to be obsoleted by your Ash resource! Here is how it _should_ be done.
@ -110,6 +112,7 @@ code_interface do
define :top, args: [:user_id]
end
actions do
read :top do
argument :user_id, :uuid do
allow_nil? false
@ -119,6 +122,7 @@ read :top do
filter expr(priority in [:medium, :high] and representative_id == ^arg(:user_id) and status == :open)
end
end
```
Now, whatever code I had that would have called `top_tickets/1` can now call `Helpdesk.Support.Ticket.top(user.id)`. By doing it this way, you get the primary benefit of getting a nice simple interface to call into, but you _also_ have a way to modify how the action is invoked in any way necessary, by going back to the old way of building the query manually. For example, if I also only want to see top tickets that were opened in the last 10 minutes: