Skip to content

Commit

Permalink
refactor application controller
Browse files Browse the repository at this point in the history
  • Loading branch information
diebas committed Dec 11, 2024
1 parent d1c063d commit 15e7da3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 41 deletions.
66 changes: 29 additions & 37 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
}

# 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

# Raise an error on page load if there are pending migrations.
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
Expand Down
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,3 @@
match "/404", to: "errors#not_found", via: :all
match "/500", to: "errors#internal_server_error", via: :all
end

0 comments on commit 15e7da3

Please sign in to comment.