mercredi 4 septembre 2019

Pundit with second devise model

I manage the authorization of users in my app with the pundit gem. Everything works fine for the user. Now I created a second devise model: Employers. I want to show a specific page to both logged in user as well as logged in employers. How do I do that?

Here is my policy for the model:

class CurriculumPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      scope.all
    end
  end

  def create?
    return true
  end

  def show?
    record.user == user || user.admin
  end

  def update?
    record.user == user || user.admin
  end

  def destroy?
    record.user == user || user.admin
  end
end

And here is my controller for the index page which I want to make accessible:

class CurriculumsController < ApplicationController
    skip_before_action :authenticate_user!, only: [:new, :create, :index]
    before_action :set_curriculum, only: [:show, :edit, :update, :destroy]

  def index
    # @curriculums = policy_scope(Curriculum).order(created_at: :desc)
    if params[:query]
  @curriculums = policy_scope(Curriculum).joins(:user)
               .where('users.job_category ILIKE ?', "%#{params[:query]}%")
               .where(
                 'job_category ILIKE :query', query: "%#{params[:query]}%"
                )
    else
    @curriculums = policy_scope(Curriculum).order(created_at: :desc)
    end
  end

  private

   def set_curriculum
      @curriculum = Curriculum.find(params[:id])
    end

  def curriculum_params
      params.require(:curriculum).permit(:doc)
  end

end

Aucun commentaire:

Enregistrer un commentaire