Calling policy modules "directly"
Is there a way to invoke a policy module on an "ad-hoc" manner, similar to doing
Resource.my_calculation
?
I have this:
Currently tied to an action:
But now I want to show/hide some UI elements based on the logic inside the policy module, so I'm wondering if I can call it "directly" if that makes sense.14 Replies
You want to use
Ash.can?/3
. There are a lot of options that can cover pretty much any scenario: https://hexdocs.pm/ash/Ash.html#can?/3Ash.can?
or for your code interfaces there is a generated can_
function
i.e define :create_post
gets you can_create_post?(....)
So would this be called like:
?
It didn't like that:
It depends on the interface. If using
Ash.can
the arguments are different.
See h AshUser.can_update_current_dashboard_organization?
The actor is passed in as a positional argumentOoh right, I forgot about those new ones. Still haven't gotten around to using them. 😄
Doing
h AshUser.can_update_current_dashboard_organization?
doesn't say anything useful. It just repeats the Ash docs. I'm trying to figure out how to invoke the function. I posted the policy above.
The docs are quite sparse. There are no "real life" examples of usage, just stuff like
Where the third argument isn't even valid Elixir syntax.h AshUser.can_update_current_dashboard_organization?
should at least show you the arguments doesn't it?
I'm adding some examplesDoing this seems to have worked:
So that is an update action on users?
If so yes that makes sense
Yeah, the user is attempting to switch to another org view basically
The auto-generated
can?
stuff is pretty useful
Thanks for updating the docs
I think more broadly, me and the team would appreciate it if the docs were treated more as a first-class citizen. There are a LOT of gaps. It seems like the dev team is prioritizing adding more features over writing thorough documentation with real examples, guides, tutorials, etc. I point it out when I can, but don't exactly have the kind of spare time where I could do PRs myself (especially since my own knowledge of the framework is minimal)We're doing both. I know its frustrating, but its also hard for me to hear that we aren't treating docs as a first class citizen when I spend multiple hours a week working on docs, reference material, guides etc.
Every day I'm committing improvements to the docs, working on new guides (like this one: https://hexdocs.pm/ash/multi-step-actions.html) addressing these concerns. A lot of it isn't about the docs not being first class, its that all the questions you asked here are technically documented, just not where someone would have expected to find them, and its hard to understand what makes these things accessible or know up front what these deficiencies are.
This is ultimately a community project, but at the end of the day, it ends up just feeling like a me project, despite the hundreds of companies using Ash in production on money-making ventures 🤷♂️
Anyway, here is the docs commit for this issue: https://github.com/ash-project/ash/commit/a793776b505c5050bb0c9e88eb7b31a4398458cc
Are you using multitenancy?
I can't comment on the you-project vs. community-project aspect, Zach. But I'll post a specific recent example in the #documentation channel and propose a solution.