2015-12-11 15:26:51 -05:00
|
|
|
# controllers/application_controller.rb
|
2015-07-08 10:24:41 -04:00
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
protect_from_forgery with: :exception
|
2015-07-10 00:19:01 -04:00
|
|
|
|
|
|
|
helper_method :current_user
|
|
|
|
helper_method :current_session
|
|
|
|
helper_method :logged_in?
|
|
|
|
|
2015-12-14 13:28:39 -05:00
|
|
|
include Pundit
|
|
|
|
|
|
|
|
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
|
|
|
|
2015-07-10 00:19:01 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
@current_user ||= User.find(current_session[:user_id]) if current_session
|
2015-12-14 13:28:39 -05:00
|
|
|
@current_user ||= GuestUser.new
|
2015-07-10 00:19:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_session
|
|
|
|
@current_session ||= UserSession.authenticate(cookies[:user_session])
|
|
|
|
end
|
|
|
|
|
|
|
|
def logged_in?
|
2015-12-14 13:28:39 -05:00
|
|
|
current_user.registered?
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_not_authorized
|
|
|
|
flash[:alert] = 'You are not authorized to perform this action.'
|
|
|
|
redirect_to(request.referrer || root_path)
|
2015-07-10 00:19:01 -04:00
|
|
|
end
|
2015-07-08 10:24:41 -04:00
|
|
|
end
|