Skip to content

Commit e5d3e83

Browse files
authored
Extract shared not-found handler (#977)
## Summary - centralize not-found logic via `ApplicationController#render_not_found` - call shared handler from pages and friendly resource controllers - add request specs covering page and post not-found scenarios ## Testing - `bundle exec rubocop` *(fails: Could not find font-awesome-sass-6.7.2, sassc-2.4.0)* - `bundle exec brakeman -q -w2` *(fails: Could not find font-awesome-sass-6.7.2, sassc-2.4.0)* - `bundle exec bundler-audit --update` *(fails: Could not find font-awesome-sass-6.7.2, sassc-2.4.0)* - `bin/codex_style_guard` *(fails: Could not find font-awesome-sass-6.7.2, sassc-2.4.0)* - `bin/ci` *(fails: Could not find font-awesome-sass-6.7.2, sassc-2.4.0)* ------ https://chatgpt.com/codex/tasks/task_e_689a5593b9088321b39d55aa47834ad0
2 parents 03de690 + c2ef228 commit e5d3e83

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

app/controllers/better_together/application_controller.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class ApplicationController < ActionController::Base # rubocop:todo Metrics/Clas
2323
Rack::MiniProfiler.authorize_request if current_user&.permitted_to?('manage_platform')
2424
end
2525

26-
rescue_from ActiveRecord::RecordNotFound, with: :handle404
27-
rescue_from ActionController::RoutingError, with: :handle404
26+
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
27+
rescue_from ActionController::RoutingError, with: :render_not_found
2828
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
2929
rescue_from StandardError, with: :handle_error
3030

@@ -126,11 +126,7 @@ def valid_platform_invitation_token_present?
126126

127127
private
128128

129-
def handle404
130-
render_404
131-
end
132-
133-
def render_404 # rubocop:todo Naming/VariableNumber
129+
def render_not_found
134130
render 'errors/404', status: :not_found
135131
end
136132

app/controllers/better_together/friendly_resource_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def set_resource_instance
2323
# 2. By friendly on all available locales
2424
@resource ||= find_by_translatable
2525

26-
handle404 && return if @resource.nil?
26+
render_not_found && return if @resource.nil?
2727

2828
@resource
2929
end

app/controllers/better_together/pages_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def index
1717

1818
def show
1919
if @page.nil? || !@page.published?
20-
render_404
20+
render_not_found
2121
else
2222
authorize @page
2323
@content_blocks = @page.content_blocks
@@ -95,14 +95,14 @@ def id_param
9595

9696
private
9797

98-
def handle404
98+
def render_not_found
9999
path = params[:path]
100100

101101
# If page is not found and the path is one of the variants of the root path, render community engine promo page
102102
if ['home-page', 'home', "/#{I18n.locale}/", "/#{I18n.locale}", I18n.locale.to_s, 'bt', '/'].include?(path)
103103
render 'better_together/static_pages/community_engine'
104104
else
105-
render_404
105+
super
106106
end
107107
end
108108

@@ -122,7 +122,7 @@ def safe_page_redirect_url
122122
def set_page
123123
@page = set_resource_instance
124124
rescue ActiveRecord::RecordNotFound
125-
handle404
125+
render_not_found && return
126126
end
127127

128128
def page_params # rubocop:todo Metrics/MethodLength

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
end
205205

206206
if Rails.env.development?
207-
get '/404', to: 'application#render_404'
207+
get '/404', to: 'application#render_not_found'
208208
get '/500', to: 'application#render_500'
209209
end
210210

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'NotFoundHandler', type: :request do
6+
include BetterTogether::DeviseSessionHelpers
7+
8+
before do
9+
configure_host_platform
10+
post better_together.user_session_path, params: {
11+
user: { email: '[email protected]', password: 'password12345' }
12+
}
13+
end
14+
15+
describe 'pages' do
16+
it 'renders 404 for missing page' do
17+
get '/en/nonexistent-page'
18+
expect(response).to have_http_status(:not_found)
19+
end
20+
21+
it 'renders promo page for root variants' do
22+
get '/en/home-page'
23+
expect(response).to have_http_status(:ok)
24+
expect(response.body).to include('Community Engine')
25+
end
26+
end
27+
28+
describe 'other resources' do
29+
it 'renders 404 for missing post' do
30+
get '/en/posts/nonexistent'
31+
expect(response).to have_http_status(:not_found)
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)