Last active
August 29, 2015 14:20
-
-
Save tquiroga/571a81b7781a69dd33ab to your computer and use it in GitHub Desktop.
Pundit authorization for custom action?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotePolicy < ApplicationPolicy | |
# For index action | |
class Scope | |
attr_reader :user, :scope | |
def initialize(user, scope) | |
@user = user | |
@scope = scope | |
end | |
def resolve | |
scope.where(project: user.projects) | |
end | |
end | |
attr_reader :user, :note | |
def initialize(user, note) | |
@user = user | |
@note = note | |
end | |
def show? | |
ProjectPolicy.new(user, Project.find(note.project_id)).show? | |
end | |
def create? | |
note.user == user or ProjectPolicy.new(user, Project.find(note.project_id)).show? | |
end | |
def update? | |
note.user == user or ProjectPolicy.new(user, Project.find(note.project_id)).show? | |
end | |
def destroy? | |
note.user == user or ProjectPolicy.new(user, Project.find(note.project_id)).show? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::NotesController < Api::V1::BaseController | |
def shared | |
@notes = policy_scope(Note) | |
@notes = @notes.where(query_params) | |
.page(page_params[:page]) | |
.per(page_params[:page_size]) | |
render :index | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment