#728·pundit

Per-query level verify_pundit_policy_scoped

Author: MyklClasonCreated Mar 18, 2022Updated Oct 1, 2024
Labelsfeature requestwaiting for response

Have similar issue as this one, but have a solution in mind: https://github.com/varvet/pundit/issues/663

The issue is to be explicit and potentially excessive. Though we can make use of lazy query logic to prevent doing too much work: Basically given this:

records = Record.all # Lazy, so doesn't run here unless you try to view the result
records.pluck(:name) # This actually runs the database query

We want it to raise an error unless it has something like one of these two:

records = policy_scope(Record.all)
records.pluck(:name)

or

records = Record.all
policy_scope(records).pluck(:name)

This might be as simple as policy_scope setting a "pundit_policy_scoped" flag on Record.all (or worst case, a global/instance variable or just using the cache) and to_sql (or another method that is called when actually sending the query to the database) raising an error if the flag isn't set. We can skip doing both unless policy_scope is defined.

Even if it's not an official solution, it would be good to have a code example that allows for it.