From 15e7da3c69d55fbf643c287ae089b2b2d697b042 Mon Sep 17 00:00:00 2001 From: Diego Basterrech Date: Wed, 11 Dec 2024 11:43:40 -0300 Subject: [PATCH] refactor application controller --- app/controllers/application_controller.rb | 66 ++++++++++------------- app/controllers/errors_controller.rb | 2 +- config/environments/test.rb | 4 +- config/routes.rb | 1 - 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f66a8f4291..a492bcd50a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -118,46 +118,20 @@ def acting_as? acting_user.object_id != session_user.object_id end - # Global exception handlers - rescue_from ActiveRecord::RecordNotFound do |exception| - Rails.logger.debug("#{exception.message}: #{exception.backtrace.join("\n")}") unless Rails.env.production? - render_404(exception) - end - - rescue_from ActionController::RoutingError do |exception| - Rails.logger.debug("#{exception.message}: #{exception.backtrace.join("\n")}") unless Rails.env.production? - render_404(exception) - end - - def render_404(_exception) - # Add html fallback in case the 404 is a PDF or XML so the view can be found - render "errors/not_found", status: 404, layout: "application", formats: formats_with_html_fallback - end - - rescue_from NUCore::PermissionDenied, CanCan::AccessDenied, with: :render_403 - def render_403(_exception) - # if current_user is nil, the user should be redirected to login - if current_user - render "errors/forbidden", status: 403, layout: "application", formats: formats_with_html_fallback - else - store_location_for(:user, request.fullpath) - redirect_to new_user_session_path + # Test exception handlers + if Rails.env.test? + rescue_from ActiveRecord::RecordNotFound do |exception| + Rails.logger.debug("#{exception.message}: #{exception.backtrace.join("\n")}") + render_404 end - end - rescue_from NUCore::NotPermittedWhileActingAs, with: :render_acting_error - def render_acting_error - render "error/acting_error", status: 403, layout: "application", formats: formats_with_html_fallback - end - - rescue_from NUCore::PermissionDenied, CanCan::AccessDenied, with: :render_403 - def render_403(_exception) - if current_user - render "errors/forbidden", status: 403, layout: "application", formats: formats_with_html_fallback - else - store_location_for(:user, request.fullpath) - redirect_to new_user_session_path + rescue_from ActionController::RoutingError do |exception| + Rails.logger.debug("#{exception.message}: #{exception.backtrace.join("\n")}") + render_404 end + + rescue_from NUCore::PermissionDenied, CanCan::AccessDenied, with: :render_403 + rescue_from NUCore::NotPermittedWhileActingAs, with: :render_acting_error end def after_sign_out_path_for(_) @@ -218,4 +192,22 @@ def formats_with_html_fallback request.formats.map(&:ref) + [:html] end + def render_403(_exception) + if current_user + render "errors/forbidden", status: 403, layout: "application", formats: formats_with_html_fallback + else + store_location_for(:user, request.fullpath) + redirect_to new_user_session_path + end + end + + def render_acting_error + render "error/acting_error", status: 403, layout: "application", formats: formats_with_html_fallback + end + + def render_404 + # Add html fallback in case the 404 is a PDF or XML so the view can be found + render "errors/not_found", status: 404, layout: "application", formats: formats_with_html_fallback + end + end diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index 3a00e6bad5..a6c2e48c34 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -26,7 +26,7 @@ def forbidden private def render_error(template, status) - render template, status: status.to_sym, formats: formats_with_html_fallback + render template, status: status, formats: formats_with_html_fallback rescue ActionController::UnknownFormat head status end diff --git a/config/environments/test.rb b/config/environments/test.rb index 9899e4e334..7ee231e145 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -23,7 +23,7 @@ } # Show full error reports and disable caching. - config.consider_all_requests_local = false + config.consider_all_requests_local = true config.action_controller.perform_caching = false config.cache_store = :null_store @@ -31,7 +31,7 @@ config.active_record.migration_error = :page_load # Raise exceptions instead of rendering exception templates. - config.action_dispatch.show_exceptions = true + config.action_dispatch.show_exceptions = false # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false diff --git a/config/routes.rb b/config/routes.rb index 73b1d0529f..55825991d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -455,4 +455,3 @@ match "/404", to: "errors#not_found", via: :all match "/500", to: "errors#internal_server_error", via: :all end -