From 516205efd949173f62345e00d7bafaee4a77acfe Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 29 Aug 2024 16:15:07 +0200 Subject: [PATCH 1/2] add path_rewrite model --- app/models/path_rewrite.rb | 4 ++++ db/migrate/20240829141049_create_path_rewrites.rb | 11 +++++++++++ db/schema.rb | 10 +++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 app/models/path_rewrite.rb create mode 100644 db/migrate/20240829141049_create_path_rewrites.rb diff --git a/app/models/path_rewrite.rb b/app/models/path_rewrite.rb new file mode 100644 index 000000000..7cb983ca8 --- /dev/null +++ b/app/models/path_rewrite.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class PathRewrite < ApplicationRecord +end diff --git a/db/migrate/20240829141049_create_path_rewrites.rb b/db/migrate/20240829141049_create_path_rewrites.rb new file mode 100644 index 000000000..7d215fd21 --- /dev/null +++ b/db/migrate/20240829141049_create_path_rewrites.rb @@ -0,0 +1,11 @@ +class CreatePathRewrites < ActiveRecord::Migration[7.0] + def change + create_table :path_rewrites do |t| + t.string :from, null: false + t.string :to, null: false + + t.timestamps + end + add_index :path_rewrites, :from, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index c8fc6ebfa..bbef3938d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_08_26_130121) do +ActiveRecord::Schema[7.0].define(version: 2024_08_29_141049) do # These are extensions that must be enabled in order to support this database enable_extension "pg_buffercache" enable_extension "pg_stat_statements" @@ -858,6 +858,14 @@ ActiveRecord::Schema[7.0].define(version: 2024_08_26_130121) do t.index ["procedure_id"], name: "index_module_api_cartos_on_procedure_id", unique: true end + create_table "path_rewrites", force: :cascade do |t| + t.datetime "created_at", null: false + t.string "from", null: false + t.string "to", null: false + t.datetime "updated_at", null: false + t.index ["from"], name: "index_path_rewrites_on_from", unique: true + end + create_table "procedure_presentations", id: :serial, force: :cascade do |t| t.integer "assign_to_id" t.datetime "created_at", precision: nil From 6677f31652bdcdd265b575c5a0e7378c15e244f2 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 29 Aug 2024 16:16:07 +0200 Subject: [PATCH 2/2] use path_rewrite in commencer controller --- app/controllers/users/commencer_controller.rb | 11 +++++++++++ db/migrate/20240829141049_create_path_rewrites.rb | 2 ++ spec/controllers/users/commencer_controller_spec.rb | 12 ++++++++++++ 3 files changed, 25 insertions(+) diff --git a/app/controllers/users/commencer_controller.rb b/app/controllers/users/commencer_controller.rb index 4e9399cb0..76fbcf93e 100644 --- a/app/controllers/users/commencer_controller.rb +++ b/app/controllers/users/commencer_controller.rb @@ -4,6 +4,17 @@ module Users class CommencerController < ApplicationController layout 'procedure_context' + before_action :path_rewrite, only: [:commencer, :commencer_test, :dossier_vide_pdf, :dossier_vide_pdf_test, :sign_in, :sign_up, :france_connect, :procedure_for_help, :closing_details] + + # TODO: REMOVE THIS + # this was only added because a administration needed new urls + # check from 07/2025 if this is still needed + def path_rewrite + path_rewrite = PathRewrite.find_by(from: params[:path]) + + params[:path] = path_rewrite.to if path_rewrite.present? + end + def commencer @procedure = retrieve_procedure diff --git a/db/migrate/20240829141049_create_path_rewrites.rb b/db/migrate/20240829141049_create_path_rewrites.rb index 7d215fd21..5ceba9897 100644 --- a/db/migrate/20240829141049_create_path_rewrites.rb +++ b/db/migrate/20240829141049_create_path_rewrites.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreatePathRewrites < ActiveRecord::Migration[7.0] def change create_table :path_rewrites do |t| diff --git a/spec/controllers/users/commencer_controller_spec.rb b/spec/controllers/users/commencer_controller_spec.rb index ccf525dd3..50b86994a 100644 --- a/spec/controllers/users/commencer_controller_spec.rb +++ b/spec/controllers/users/commencer_controller_spec.rb @@ -19,6 +19,18 @@ describe Users::CommencerController, type: :controller do end end + context 'when a path rewrite is present' do + let(:path) { 'from' } + let!(:path_rewrite) { PathRewrite.create(from: 'from', to: published_procedure.path) } + + it 'redirects to the new path' do + expect(subject.status).to eq(200) + expect(subject).to render_template('show') + expect(assigns(:procedure)).to eq published_procedure + expect(assigns(:revision)).to eq published_procedure.published_revision + end + end + context 'when the path is for a draft procedure' do let(:path) { draft_procedure.path }