Skip to content

Commit

Permalink
Merge pull request #226 from uclibs/217-make-error-pages-load-dynamic…
Browse files Browse the repository at this point in the history
…ally

217 part 1: make error pages load dynamically
  • Loading branch information
crowesn authored Oct 4, 2023
2 parents 81d2e17 + 9f9a6b5 commit 81b209d
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 10 deletions.
16 changes: 16 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
def not_found
render template: 'errors/404', layout: 'application', status: :not_found
end

def unprocessable_entity
render template: 'errors/422', layout: 'application', status: :unprocessable_entity
end

def internal_server_error
render template: 'errors/500', layout: 'application', status: :internal_server_error
end
end
2 changes: 1 addition & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def show
if valid_page?
render template: "pages/#{safe_page}"
else
render file: 'public/404.html', status: :not_found
render template: 'errors/404', status: :not_found
end
end

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Aaec
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
config.exceptions_app = routes

# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
Expand Down
26 changes: 20 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
# frozen_string_literal: true

Rails.application.routes.draw do
resources :colleges
# Resource routes
resources :artworks
resources :books
resources :book_chapters
resources :books
resources :colleges
resources :digital_projects
resources :editings
resources :films
resources :journal_articles
resources :musical_scores
resources :other_publications
resources :photographies
resources :physical_media
resources :plays
resources :public_performances
resources :other_publications
resources :submitters

# Admin-related routes
get 'citations', to: 'admin#citations'
get 'toggle_links', to: 'admin#toggle_links'
get 'publications', to: 'publications#index'
get 'publications/:id', to: 'publications#index'
get 'manage', to: 'admin#login'
post 'manage/validate', to: 'admin#validate'
get '/csv/:controller_name', to: 'admin#csv', as: 'controller_name'

# Publications and submission routes
get 'publications', to: 'publications#index'
get 'publications/:id', to: 'publications#index'
get 'finished', to: 'submitters#finished'

# Dynamic pages
get '/pages/:page' => 'pages#show'
get '/csv/:controller_name', to: 'admin#csv', as: 'controller_name'

# Root URL
root 'submitters#new'

# Custom Error Pages
match '/404', to: 'errors#not_found', via: :all
match '/500', to: 'errors#internal_server_error', via: :all
match '/422', to: 'errors#unprocessable_entity', via: :all
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
RSpec.describe ApplicationController, type: :controller do
include ApplicationHelper

controller do
controller(ApplicationController) do
def index
render plain: 'Hello, world!'
end
end

describe 'before_action :check_date' do
it 'should be called before every action' do
expect(controller).to receive(:check_date)
get :index
end

context 'when EXPIRATION_DATE is in the past and user is not admin' do
it 'redirects to the closed page' do
allow(ENV).to receive(:fetch).with('EXPIRATION_DATE').and_return('2000-01-01')
Expand Down Expand Up @@ -39,7 +44,9 @@ def index

context 'when EXPIRATION_DATE is missing' do
it 'raises a KeyError' do
# Stub ENV to simulate KeyError
allow(ENV).to receive(:fetch).with('EXPIRATION_DATE').and_raise(KeyError)

expect { get :index }.to raise_error(KeyError)
end
end
Expand Down
50 changes: 50 additions & 0 deletions spec/controllers/errors_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

# spec/controllers/errors_controller_spec.rb

require 'rails_helper'

RSpec.describe ErrorsController, type: :controller do
describe 'GET #not_found' do
before do
get :not_found
end
it 'renders the not_found template' do
expect(response).to render_template('errors/404')
expect(response).to render_template('layouts/application')
end

it 'returns HTTP status 404' do
expect(response).to have_http_status(404)
end
end

describe 'GET #unprocessable_entity' do
before do
get :unprocessable_entity
end

it 'renders the unprocessable_entity template' do
expect(response).to render_template('errors/422')
expect(response).to render_template('layouts/application')
end

it 'returns HTTP status 422' do
expect(response).to have_http_status(422)
end
end

describe 'GET #internal_server_error' do
before do
get :internal_server_error
end
it 'renders the internal_server_error template' do
expect(response).to render_template('errors/500')
expect(response).to render_template('layouts/application')
end

it 'returns HTTP status 500' do
expect(response).to have_http_status(500)
end
end
end
8 changes: 6 additions & 2 deletions spec/controllers/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
end

context 'when page is invalid' do
it 'with invalid params' do
it 'returns a 404 status' do
get :show, params: { page: 'bad' }
expect(response.status).to eq(404)
expect(response.body).to have_text('The page you requested cannot be found')
end

it 'renders the 404 template' do
get :show, params: { page: 'bad' }
expect(response).to render_template('errors/404')
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions spec/routing/admin_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# spec/routes/admin_routes_spec.rb

require 'rails_helper'

RSpec.describe 'Admin Routes', type: :routing do
describe 'GET #citations' do
it 'routes to admin#citations' do
expect(get: '/citations').to route_to('admin#citations')
end
end

describe 'GET #toggle_links' do
it 'routes to admin#toggle_links' do
expect(get: '/toggle_links').to route_to('admin#toggle_links')
end
end

describe 'GET #manage' do
it 'routes to admin#login' do
expect(get: '/manage').to route_to('admin#login')
end
end

describe 'POST #manage/validate' do
it 'routes to admin#validate' do
expect(post: '/manage/validate').to route_to('admin#validate')
end
end

describe 'GET #csv/:controller_name' do
it 'routes to admin#csv' do
expect(get: '/csv/example_controller').to route_to('admin#csv', controller_name: 'example_controller')
end
end
end
23 changes: 23 additions & 0 deletions spec/routing/dynamic_pages_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PagesController, type: :routing do
describe 'routing' do
it 'routes to #show with about as parameter' do
expect(get: '/pages/about').to route_to('pages#show', page: 'about')
end

it 'routes to #show with contact as parameter' do
expect(get: '/pages/contact').to route_to('pages#show', page: 'contact')
end

it 'routes to #show with faq as parameter' do
expect(get: '/pages/faq').to route_to('pages#show', page: 'faq')
end

it 'does not route to #show without a page parameter' do
expect(get: '/pages/').not_to be_routable
end
end
end
21 changes: 21 additions & 0 deletions spec/routing/error_pages_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

# spec/routing/error_pages_routing_spec.rb

require 'rails_helper'

RSpec.describe 'Error Pages', type: :routing do
%w[get post put patch delete].each do |http_method|
it "routes /404 to errors#not_found for #{http_method.upcase}" do
expect({ http_method.to_s => '/404' }).to route_to(controller: 'errors', action: 'not_found')
end

it "routes /500 to errors#internal_server_error for #{http_method.upcase}" do
expect({ http_method.to_s => '/500' }).to route_to(controller: 'errors', action: 'internal_server_error')
end

it "routes /422 to errors#unprocessable_entity for #{http_method.upcase}" do
expect({ http_method.to_s => '/422' }).to route_to(controller: 'errors', action: 'unprocessable_entity')
end
end
end
25 changes: 25 additions & 0 deletions spec/routing/publications_and_submitters_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# spec/routes/publications_and_submitters_routes_spec.rb

require 'rails_helper'

RSpec.describe 'Publications and Submitters Routes', type: :routing do
describe 'GET #publications' do
it 'routes to publications#index' do
expect(get: '/publications').to route_to('publications#index')
end
end

describe 'GET #publications/:id' do
it 'routes to publications#index with an ID' do
expect(get: '/publications/1').to route_to('publications#index', id: '1')
end
end

describe 'GET #finished' do
it 'routes to submitters#finished' do
expect(get: '/finished').to route_to('submitters#finished')
end
end
end
11 changes: 11 additions & 0 deletions spec/routing/root_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# spec/routing/root_routing_spec.rb

require 'rails_helper'

RSpec.describe 'Root route', type: :routing do
it 'routes the root URL to submitters#new' do
expect(get: '/').to route_to(controller: 'submitters', action: 'new')
end
end

0 comments on commit 81b209d

Please sign in to comment.