2019-03-26 15:32:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Forma de ingreso a Sutty
|
2018-01-02 17:19:25 +00:00
|
|
|
class ApplicationController < ActionController::Base
|
2019-07-10 22:07:39 +00:00
|
|
|
include ExceptionHandler
|
2021-08-04 15:17:49 +00:00
|
|
|
include Pundit
|
2019-07-10 22:07:39 +00:00
|
|
|
|
2021-03-26 15:45:43 +00:00
|
|
|
protect_from_forgery with: :null_session, prepend: true
|
2020-08-10 23:41:26 +00:00
|
|
|
|
|
|
|
before_action :prepare_exception_notifier
|
2019-08-02 18:58:15 +00:00
|
|
|
before_action :configure_permitted_parameters, if: :devise_controller?
|
2020-05-23 15:38:03 +00:00
|
|
|
around_action :set_locale
|
2018-01-29 18:09:30 +00:00
|
|
|
|
2021-08-04 15:17:49 +00:00
|
|
|
rescue_from Pundit::NilPolicyError, with: :page_not_found
|
2020-06-17 13:40:59 +00:00
|
|
|
rescue_from ActionController::RoutingError, with: :page_not_found
|
2020-10-30 21:57:46 +00:00
|
|
|
rescue_from ActionController::ParameterMissing, with: :page_not_found
|
2020-06-17 13:40:59 +00:00
|
|
|
|
2020-05-11 21:56:42 +00:00
|
|
|
before_action do
|
2020-10-04 00:32:32 +00:00
|
|
|
Rack::MiniProfiler.authorize_request if current_usuarie&.email&.ends_with?('@' + ENV.fetch('SUTTY', 'sutty.nl'))
|
2020-05-11 21:56:42 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# No tenemos índice de sutty, vamos directamente a ver el listado de
|
|
|
|
# sitios
|
2018-01-29 18:09:30 +00:00
|
|
|
def index
|
|
|
|
redirect_to sites_path
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-09-25 17:07:54 +00:00
|
|
|
def uuid?(string)
|
|
|
|
/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/ =~ string
|
|
|
|
end
|
|
|
|
|
2019-07-03 23:25:23 +00:00
|
|
|
# Encontrar un sitio por su nombre
|
2018-01-29 22:19:10 +00:00
|
|
|
def find_site
|
2019-07-10 22:07:39 +00:00
|
|
|
id = params[:site_id] || params[:id]
|
|
|
|
|
2021-08-04 15:17:49 +00:00
|
|
|
unless (site = current_usuarie&.sites&.find_by_name(id))
|
2019-07-10 22:07:39 +00:00
|
|
|
raise SiteNotFound
|
|
|
|
end
|
2019-07-04 16:23:43 +00:00
|
|
|
|
2019-07-10 22:07:39 +00:00
|
|
|
site
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2018-01-30 15:20:19 +00:00
|
|
|
|
2021-05-10 17:48:48 +00:00
|
|
|
# Devuelve el idioma actual y si no lo encuentra obtiene uno por
|
|
|
|
# defecto.
|
|
|
|
#
|
|
|
|
# Esto se refiere al idioma de la interfaz, no de los artículos.
|
|
|
|
def current_locale(include_params: true, site: nil)
|
|
|
|
return params[:locale] if include_params && params[:locale].present?
|
|
|
|
|
|
|
|
current_usuarie&.lang || I18n.locale
|
|
|
|
end
|
|
|
|
|
2020-05-23 15:38:03 +00:00
|
|
|
# El idioma es el preferido por le usuarie, pero no necesariamente se
|
|
|
|
# corresponde con el idioma de los artículos, porque puede querer
|
|
|
|
# traducirlos.
|
|
|
|
def set_locale(&action)
|
2021-05-10 17:48:48 +00:00
|
|
|
I18n.with_locale(current_locale(include_params: false), &action)
|
2019-08-02 18:58:15 +00:00
|
|
|
end
|
|
|
|
|
2020-06-08 23:25:05 +00:00
|
|
|
# Muestra una página 404
|
|
|
|
def page_not_found
|
|
|
|
render 'application/page_not_found', status: :not_found
|
|
|
|
end
|
|
|
|
|
2021-08-02 20:50:32 +00:00
|
|
|
# Necesario para poder acceder a Blazer. Solo les usuaries de este
|
|
|
|
# sitio pueden acceder al panel.
|
|
|
|
def require_usuarie
|
2021-08-02 21:37:15 +00:00
|
|
|
site = find_site
|
2021-08-04 15:17:49 +00:00
|
|
|
authorize SiteBlazer.new(site)
|
2021-08-02 20:54:27 +00:00
|
|
|
|
|
|
|
# Necesario para los breadcrumbs.
|
2021-08-02 21:31:17 +00:00
|
|
|
ActionView::Base.include Loaf::ViewExtensions unless ActionView::Base.included_modules.include? Loaf::ViewExtensions
|
2021-08-02 20:54:27 +00:00
|
|
|
|
|
|
|
breadcrumb current_usuarie.email, main_app.edit_usuarie_registration_path
|
|
|
|
breadcrumb 'sites.index', main_app.sites_path, match: :exact
|
2021-08-02 21:37:15 +00:00
|
|
|
breadcrumb site.title, main_app.site_path(site), match: :exact
|
2021-08-02 20:54:27 +00:00
|
|
|
breadcrumb 'stats.index', root_path, match: :exact
|
2021-08-02 20:50:32 +00:00
|
|
|
end
|
|
|
|
|
2019-08-02 18:58:15 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def configure_permitted_parameters
|
|
|
|
devise_parameter_sanitizer.permit(:account_update, keys: %i[lang])
|
2018-02-26 22:08:21 +00:00
|
|
|
end
|
2020-08-10 23:41:26 +00:00
|
|
|
|
|
|
|
def prepare_exception_notifier
|
2020-10-01 12:20:16 +00:00
|
|
|
request.env['exception_notifier.exception_data'] = { usuarie: current_usuarie }
|
2020-08-10 23:41:26 +00:00
|
|
|
end
|
2018-01-02 17:19:25 +00:00
|
|
|
end
|