In my App user to have full access to the app needs to pass survey after account confirmation. Survey has 2 steps:
- questions (in which the user answers questions; controller:
TestResultsController
, model:TestResult
) - experience level (inside of which user specify his level of experience; controller:
ExperienceLevelsController
, updatescurrent_user.experience_level
)
Business requirements:
When the user answers the questions it's redirected to redirect_to edit_users_experience_level_path(current_user)
where he sets his experience level (it's inside of ExperienceLevelsController
and method update
). If the user completes the survey but will give up on completing the user experience and come back to it later it would be logical to display only the experience level page. To do so I've prepared below policies:
class TestResultPolicy < ApplicationPolicy
def new?
return false if passed?
if without_result?
redirect_to edit_users_experience_level_path(current_user)
elsif passed?
active?
end
end
def create?
new?
end
private
def passed?
user.test_results.where(test_result: 'passed').any?
end
def without_result?
user.test_results.last.result.nil?
end
end
Is it a good way to define redirection inside of Pundit policy? I know I could use user_not_authorized
but I'm using it already inside of ApplicationController where I redirect to identity_unconfirmed_path
or root_path:
class ApplicationController < ActionController::Base
include Pundit
before_action :set_paper_trail_whodunnit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
private
def user_not_authorized
flash[:alert] = 'You are not authorized to perform this action.'
return redirect_to(request.referrer || root_path) unless current_user.northrow_status == 'failed'
redirect_to identity_unconfirmed_path
end
end
So again, should I use redirect flow inside pundit policy or isn't this a good practice?