If I only want to return Proposals that are published but not expired, is this possible with Pundit?
So far, I have this:
class ProposalPolicy < ApplicationPolicy
class Scope < Scope
def resolve
if @user.admin?
scope.all
else
scope.where(published: true)
end
end
end
...
end
One work around is to write extra code in the index
action of my Proposal controller to further filter the list of Proposals instances down to non-expired proposals.
I am hoping there's some magical syntax like this:
scope.where({published: true, expire_date > Time.now })
Any ideas? :D
You can either do this in a single where
command:
scope.where('published = ? AND expire_date > ?', true, Time.now)
Or, split into two:
scope.where(published: true)
.where('expire_date > ?', Time.now)
As for where this logic should go, that's up to you.
If it's really a restriction of the policy (i.e. only admins are allowed to see expired/unpublished proposals), then put it in the Pundit scope.
On the other hand, if non-admins are just being shown a filtered list for convenience (but can still view expired/unpublished proposals by some other means), then I'd rather put this query in the controller.