1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/controllers/application_controller.rb

57 lines
1.3 KiB
Ruby
Raw Normal View History

2018-11-22 14:58:12 -05:00
# frozen_string_literal: true
2018-11-22 14:33:08 -05:00
class ApplicationController < ActionController::Base
2018-11-29 19:02:04 -05:00
include Pundit
2018-11-29 08:51:03 -05:00
2018-11-29 09:57:40 -05:00
before_action :set_raven_context
2018-11-30 20:39:26 -05:00
protect_from_forgery with: :exception, prepend: true, unless: :json_request?
2018-11-29 19:17:26 -05:00
after_action :verify_authorized, except: :index
after_action :verify_policy_scoped, only: :index
2018-11-29 08:51:03 -05:00
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from NotAuthorizedError, with: :unauthorized
2018-12-01 21:28:34 -05:00
helper_method :current_account
private
2018-12-01 21:28:34 -05:00
def current_account
2018-12-05 18:20:50 -05:00
@current_account ||= current_user&.account
@current_account ||= Account.guests.find_by(id: session[:guest_account_id])
2018-12-01 21:28:34 -05:00
end
2018-12-02 06:04:30 -05:00
def pundit_user
@pundit_user ||= ApplicationPolicy::Context.new(
account: current_account&.guest? ? nil : current_account,
guest_account: current_account,
2018-12-02 06:04:30 -05:00
)
end
2018-12-01 21:28:34 -05:00
2018-11-29 09:57:40 -05:00
def set_raven_context
2018-11-29 16:21:33 -05:00
Raven.user_context id: current_user.id if user_signed_in?
2018-11-29 09:57:40 -05:00
Raven.extra_context params: params.to_unsafe_h, url: request.url
end
def json_request?
request.format.json?
end
2018-11-29 08:51:03 -05:00
def not_found
render status: :not_found, json: {}
end
def unauthorized
render status: :unauthorized, json: {}
end
2018-12-04 20:49:26 -05:00
def method_not_allowed
render status: :method_not_allowed, json: {}
end
2018-12-04 20:49:26 -05:00
def remember_if_guest_account(account)
session[:guest_account_id] = account.id if account.guest?
end
2018-11-22 14:33:08 -05:00
end