mirror of
https://0xacab.org/sutty/sutty
synced 2024-07-01 12:46:08 +00:00
105 lines
2.7 KiB
Ruby
105 lines
2.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Forma de ingreso a Sutty
|
|
class ApplicationController < ActionController::Base
|
|
include ExceptionHandler
|
|
|
|
protect_from_forgery with: :null_session, prepend: true
|
|
|
|
before_action :prepare_exception_notifier
|
|
before_action :configure_permitted_parameters, if: :devise_controller?
|
|
before_action :redirect_to_site_name!, only: %i[index show edit new], if: :site_id_contains_hostname?
|
|
around_action :set_locale
|
|
|
|
rescue_from ActionController::RoutingError, with: :page_not_found
|
|
rescue_from ActionController::ParameterMissing, with: :page_not_found
|
|
|
|
before_action do
|
|
Rack::MiniProfiler.authorize_request if Rails.env.development?
|
|
end
|
|
|
|
# No tenemos índice de sutty, vamos directamente a ver el listado de
|
|
# sitios
|
|
def index
|
|
redirect_to sites_path
|
|
end
|
|
|
|
private
|
|
|
|
def uuid?(string)
|
|
/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/ =~ string
|
|
end
|
|
|
|
# Encontrar un sitio por su nombre.
|
|
def find_site
|
|
current_usuarie.sites.find_by_name(site_id).tap do |site|
|
|
raise SiteNotFound unless site
|
|
end
|
|
end
|
|
|
|
# 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
|
|
|
|
# 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)
|
|
I18n.with_locale(current_locale(include_params: false), &action)
|
|
end
|
|
|
|
# Muestra una página 404
|
|
def page_not_found
|
|
render 'application/page_not_found', status: :not_found
|
|
end
|
|
|
|
# Retrocompatibilidad con sitios cuyo nombre era su hostname.
|
|
#
|
|
# @see Deploy
|
|
def site_id_contains_hostname?
|
|
site_id&.end_with? '.'
|
|
end
|
|
|
|
# Redirigir a la misma URL con el site_id cambiado.
|
|
#
|
|
# TODO: Eliminar cuando detectemos que no hay más redirecciones.
|
|
def redirect_to_site_name!
|
|
params.permit!
|
|
params[:site_id] = Deploy.site_name_from_hostname(site_id[0..-2])
|
|
|
|
redirect_to params, status: :moved_permanently
|
|
end
|
|
|
|
# Los controladores dentro de SitesController van a usar site_id
|
|
# mientras que SiteController va a usar ID.
|
|
#
|
|
# @see SitesController
|
|
# @return [String,Nil]
|
|
def site_id
|
|
@site_id ||= params[:site_id]
|
|
end
|
|
|
|
# El sitio actual
|
|
#
|
|
# @return [Site]
|
|
def site
|
|
@site ||= find_site
|
|
end
|
|
|
|
protected
|
|
|
|
def configure_permitted_parameters
|
|
devise_parameter_sanitizer.permit(:account_update, keys: %i[lang])
|
|
end
|
|
|
|
def prepare_exception_notifier
|
|
request.env['exception_notifier.exception_data'] = { usuarie: current_usuarie }
|
|
end
|
|
end
|