diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 48368d31..3ca30168 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -4,11 +4,11 @@ module Api module V1 # API class BaseController < ActionController::Base + include ExceptionHandler + protect_from_forgery with: :null_session respond_to :json - rescue_from ActionController::RoutingError, with: :page_not_found - private # Realiza la inversa de Site#hostname @@ -26,18 +26,6 @@ module Api def origin request.headers['Origin'] end - - # El primer sitio es el sitio por defecto - # - # @return [Site] - def default_site - Site.first - end - - # Muestra una página 404 - def page_not_found - render 'application/page_not_found', status: :not_found - end end end end diff --git a/app/controllers/api/v1/protected_controller.rb b/app/controllers/api/v1/protected_controller.rb index 579732f7..bce9555c 100644 --- a/app/controllers/api/v1/protected_controller.rb +++ b/app/controllers/api/v1/protected_controller.rb @@ -128,7 +128,7 @@ module Api # # @return [TrueClass] def log_entry - LogEntry.create site: site || default_site, text: { + LogEntry.create site: site || Site.default, text: { reason: @reason, status: response.status, headers: request.headers.to_h.select { |k, _| /\A[A-Z]/ =~ k }, diff --git a/app/controllers/concerns/exception_handler.rb b/app/controllers/concerns/exception_handler.rb index 3be84124..8c4f54c8 100644 --- a/app/controllers/concerns/exception_handler.rb +++ b/app/controllers/concerns/exception_handler.rb @@ -5,12 +5,20 @@ module ExceptionHandler extend ActiveSupport::Concern class SiteNotFound < StandardError; end + class PageNotFound < StandardError; end included do rescue_from SiteNotFound, with: :site_not_found + rescue_from PageNotFound, with: :page_not_found + rescue_from ActionController::RoutingError, with: :page_not_found + rescue_from Pundit::NilPolicyError, with: :page_not_found end def site_not_found redirect_to sites_path end + + def page_not_found + send_file Rails.root.join('public', '404.html') + end end diff --git a/app/controllers/private_controller.rb b/app/controllers/private_controller.rb index 7cca1319..36a7a785 100644 --- a/app/controllers/private_controller.rb +++ b/app/controllers/private_controller.rb @@ -7,7 +7,6 @@ class PrivateController < ApplicationController skip_forgery_protection include Pundit - rescue_from Pundit::NilPolicyError, with: :page_not_found # Enviar el archivo si existe, agregar una / al final siempre para no # romper las direcciones relativas. @@ -56,7 +55,7 @@ class PrivateController < ApplicationController @path rescue Errno::ENOENT - File.join(deploy_private.destination, '404.html') + not_found_path end # Devuelve la ruta del archivo, limpieza copiada desde Jekyll @@ -75,4 +74,15 @@ class PrivateController < ApplicationController @file = @file.gsub('..', '/').gsub('./', '').squeeze('/') end + + # Devuelve una página 404.html + def not_found_path + return site_not_found_path if File.exist? site_not_found_path + + raise PageNotFound + end + + def site_not_found_path + @site_not_found_path ||= File.join(deploy_private.destination, '404.html') + end end diff --git a/app/models/site.rb b/app/models/site.rb index 8f74b3c8..c99bc145 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -384,6 +384,10 @@ class Site < ApplicationRecord File.join(Rails.root, '_sites') end + def self.default + find_by(name: Site.domain + '.') + end + def reset @read = false @layouts = nil