I'm working on a small Rails project right now. I'm using Pundit for authorization of controller actions and stuff. The job right now is to use Pundit for the index action and use Pundit's policy_scope
in my controller to get the Projects a user can see.
There are 3 ways a user should be able to see a project: - He's the owner - He was invited - The project is public
I've tried several solutions right now but ended up doing the following:
scope :public_or_involved, -> (user) {
joins('LEFT JOIN invitations ON invitations.project_id = projects.id').
where('projects.is_public = TRUE OR projects.owner_id = ? OR invitations.reciever_id = ?', user.id, user.id)
}
This is the only way I got something like a "public_or_involved" to work with the following pundit scope in project_policy.rb:
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
scope.public_or_involved(user)
end
end
So my Question, finally:
Is there an elegant way of doing what I'm doing now? It feels ugly and not rails-like!