Can I access to resource.model on pundit policy? I...
# avo-2
r
Can I access to resource.model on pundit policy? I mean, if I have post_policy.rb, can I access to Post.attribute in the
Class Scope < Scope
inside
def resolve
?
l
Yes, you can Angel
> Can I access to resource.model on pundit policy? in a policy method like
def show?
you have access to
record
and
user
> can I access to Post.attribute in the
Class Scope < Scope
inside
def resolve?
Not sure I understand this question though
can you please rephrase it?
Copy code
ruby
class PostPolicy < ApplicationPolicy
  class Scope
    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      if user.admin?
        scope.all
      else
        scope.where(published: true)
      end
    end

    private

    attr_reader :user, :scope
  end

  def update?
    user.admin? or not record.published?
  end
end
this is how the
Scope
class looks like from here https://github.com/varvet/pundit#scopes
so inside the
resolve
method you get the
user
and the
scope
which is the query passed to it (ex:
User.all
)
2 Views