[ Pundit ] policy_scope

def index
@posts = policy_scope(Post)
end

is equals to

@posts = PostPolicy::Scope.new(current_user, Post).resolve

declaraction

/app/policies/post_policy.rb

class PostPolicy
def index?
false
end

def create?
user.admin?
end
class Scope

def initialize(user, scope)
fail Pundit::NotAuthorizedError, 'must log in' unless user

@user = user

@scope = scope

end

def resolve

scope

end

end

end

NOTE : that the @post will be returned to the controller, so we can do something like

@posts = policy_scope(Post).reorder(created_at: :desc)

ref : https://github.com/elabs/pundit

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s