1 # frozen_string_literal: true
3 class ApplicationController
< ActionController
::Base
4 # Prevent CSRF attacks by raising an exception.
5 # For APIs, you may want to use :null_session instead.
6 protect_from_forgery with
: :exception
8 force_ssl
if: :https_enabled?
11 include UserTrackingConcern
13 helper_method
:current_account
14 helper_method
:single_user_mode?
16 rescue_from ActionController
::RoutingError, with
: :not_found
17 rescue_from ActiveRecord
::RecordNotFound, with
: :not_found
18 rescue_from ActionController
::InvalidAuthenticityToken, with
: :unprocessable_entity
20 before_action
:store_current_location, except
: :raise_not_found, unless: :devise_controller?
21 before_action
:check_suspension, if: :user_signed_in?
24 raise ActionController
::RoutingError, "No route matches #{params[:unmatched_route]}"
30 Rails
.env.production
? && ENV['LOCAL_HTTPS'] == 'true'
33 def store_current_location
34 store_location_for(:user, request
.url
)
38 redirect_to root_path
unless current_user
&.admin
?
42 head
403 if current_user
.account
.suspended
?
48 respond_to
do |format
|
49 format
.any
{ head
404 }
50 format
.html
{ respond_with_error(404) }
55 respond_to
do |format
|
56 format
.any
{ head
410 }
57 format
.html
{ respond_with_error(410) }
62 respond_to
do |format
|
63 format
.any
{ head
403 }
64 format
.html
{ render
'errors/403', layout
: 'error', status
: 403 }
68 def unprocessable_entity
69 respond_to
do |format
|
70 format
.any
{ head
422 }
71 format
.html
{ respond_with_error(422) }
76 @single_user_mode ||= Rails
.configuration
.x
.single_user_mode
&& Account
.first
80 @current_account ||= current_user
.try(:account)
83 def cache_collection(raw
, klass
)
84 return raw
unless klass
.respond_to
?(:with_includes)
86 raw
= raw
.cache_ids
.to_a
if raw
.is_a
?(ActiveRecord
::Relation)
88 cached_keys_with_value
= Rails
.cache
.read_multi(*raw
.map(&:cache_key))
91 uncached_ids
<< item
.id
unless cached_keys_with_value
.key
?(item
.cache_key
)
94 klass
.reload_stale_associations!
(cached_keys_with_value
.values
) if klass
.respond_to
?(:reload_stale_associations!
)
96 unless uncached_ids
.empty
?
97 uncached
= klass
.where(id
: uncached_ids
).with_includes
.map
{ |item
| [item
.id
, item
] }.to_h
99 uncached
.values
.each
do |item
|
100 Rails
.cache
.write(item
.cache_key
, item
)
104 raw
.map
{ |item
| cached_keys_with_value
[item
.cache_key
] || uncached
[item
.id
] }.compact
107 def respond_with_error(code
)
109 render
"errors/#{code}", layout
: 'error', status
: code