Skip to content

Instantly share code, notes, and snippets.

@tquiroga
Last active August 29, 2015 14:20
Show Gist options
  • Save tquiroga/571a81b7781a69dd33ab to your computer and use it in GitHub Desktop.
Save tquiroga/571a81b7781a69dd33ab to your computer and use it in GitHub Desktop.
Pundit authorization for custom action?
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
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