Skip to content

Commit

Permalink
Add status codes to URL rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
sfnelson committed Dec 11, 2023
1 parent f158ff3 commit ef63e6c
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
katalyst-koi (4.3.1)
katalyst-koi (4.3.2)
bcrypt
importmap-rails
katalyst-content
Expand Down
5 changes: 4 additions & 1 deletion app/assets/javascripts/koi/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ import "koi/controllers";
/** Let GOVUK know that we've got JS enabled */
window.addEventListener("turbo:load", () => {
document.body.classList.toggle("js-enabled", true);
document.body.classList.toggle("govuk-frontend-supported", ('noModule' in HTMLScriptElement.prototype));
document.body.classList.toggle(
"govuk-frontend-supported",
"noModule" in HTMLScriptElement.prototype,
);
});
2 changes: 1 addition & 1 deletion app/controllers/admin/url_rewrites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def destroy
private

def url_rewrite_params
params.require(:url_rewrite).permit(:from, :to, :active)
params.require(:url_rewrite).permit(:from, :to, :status_code, :active)
end

def set_url_rewrite
Expand Down
16 changes: 12 additions & 4 deletions app/models/url_rewrite.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# frozen_string_literal: true

class UrlRewrite < ApplicationRecord
StatusCode = Struct.new(:code, :label)

STATUS_CODES = [
StatusCode.new(303, "See other"),
StatusCode.new(307, "Temporary redirect"),
StatusCode.new(308, "Permanent redirect"),
].freeze

enum status_code: STATUS_CODES.map(&:code).index_by(&:itself)

attribute :status_code, :integer, default: 303

validates :from, :to, presence: true
validates :from, format: { with: %r{\A/.*\z}, message: "should start with /" }

Expand All @@ -15,10 +27,6 @@ def to_s
where(arel_table[:from].matches("%#{query}%").or(arel_table[:to].matches("%#{query}%")))
end

def self.redirect_path_for(path)
active.find_by(from: path).try(:to)
end

def title
from.delete_prefix("/")
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/url_rewrites/_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= form.govuk_text_field :from, hint: { text: "Should begin with /" } %>
<%= form.govuk_text_field :to %>
<%= form.govuk_collection_radio_buttons :status_code, UrlRewrite::STATUS_CODES, :code, :label %>
<%= form.govuk_check_box_field :active %>
3 changes: 0 additions & 3 deletions app/views/admin/url_rewrites/_form_fields.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/admin/url_rewrites/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% end %>

<%= form_with(model: url_rewrite, url: admin_url_rewrite_path(url_rewrite)) do |form| %>
<%= render "form_fields", form: %>
<%= render "fields", form: %>

<div class="actions">
<%= form.admin_save %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/url_rewrites/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% end %>

<%= form_with(model: url_rewrite, url: admin_url_rewrites_path) do |form| %>
<%= render "form_fields", form: %>
<%= render "fields", form: %>

<div class="actions">
<%= form.admin_save %>
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20231211005214_add_status_code_to_url_rewrites.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddStatusCodeToUrlRewrites < ActiveRecord::Migration[7.1]
def change
add_column :url_rewrites, :status_code, :integer, null: false, default: 303
end
end
2 changes: 1 addition & 1 deletion katalyst-koi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "katalyst-koi"
s.version = "4.3.1"
s.version = "4.3.2"
s.authors = ["Katalyst Interactive"]
s.email = ["[email protected]"]

Expand Down
12 changes: 4 additions & 8 deletions lib/koi/middleware/url_redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def call(env)

current_path = env["REQUEST_URI"]

if status.to_i == 404 && (new_path = find_redirect(current_path))
if status.to_i == 404 && (redirect = UrlRewrite.active.find_by(from: current_path))
request = ActionDispatch::Request.new(env)

# Close the body as we will not use it for a 301 redirect
body.close

# Issue a "Moved permanently" response with the redirect location
[301, { "Location" => new_location(current_path, new_path, request) },
["#{current_path} moved. The document has moved to #{new_path}"]]
# Return a redirect response
[redirect.status_code, { "Location" => new_location(current_path, redirect.to, request) },
["#{redirect.from} moved. The document has moved to #{redirect.to}"]]
else
# Not a 404 or no redirect found, just send the response as is
[status, headers, body]
Expand All @@ -35,9 +35,5 @@ def new_location(current_path, new_path, request)
request.original_url.gsub(current_path, new_path)
end
end

def find_redirect(path)
UrlRewrite.redirect_path_for(path)
end
end
end
6 changes: 0 additions & 6 deletions spec/models/url_rewrite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@

it { is_expected.to include(url_rewrite) }
end

describe "#redirect_path_for" do
subject { described_class.redirect_path_for(url_rewrite.from) }

it { is_expected.to eq(url_rewrite.to) }
end
end

0 comments on commit ef63e6c

Please sign in to comment.