From 8c19425f85587a957f1fd5abfa50fbf5e4531237 Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Wed, 8 May 2024 09:59:25 +0100 Subject: [PATCH] Introduces journey session model As part of the efforts to simplify multi claim handling we want to move away from creating a claim at the start of the journey and instead only create the claim at the final step where the teacher submits their answers. This commit introduces a new model `Journeys::Session` which will be used to store the answers entered by the teacher. The answers will be stored in a json column on the new model, once validated by the form objects. `Journeys::Session` is currently the best name we could come up with but we're all ears for any better suggestions. --- app/controllers/claims_controller.rb | 1 + app/controllers/concerns/journey_concern.rb | 16 ++++++++++++++++ app/models/journeys.rb | 4 ++++ app/models/journeys/session.rb | 7 +++++++ .../20240508081918_create_journeys_sessions.rb | 10 ++++++++++ db/schema.rb | 9 ++++++++- spec/factories/journeys/sessions.rb | 5 +++++ spec/models/journeys/session_spec.rb | 15 +++++++++++++++ 8 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/models/journeys/session.rb create mode 100644 db/migrate/20240508081918_create_journeys_sessions.rb create mode 100644 spec/factories/journeys/sessions.rb create mode 100644 spec/models/journeys/session_spec.rb diff --git a/app/controllers/claims_controller.rb b/app/controllers/claims_controller.rb index 47b9cdc14f..4109a8641a 100644 --- a/app/controllers/claims_controller.rb +++ b/app/controllers/claims_controller.rb @@ -162,6 +162,7 @@ def persist current_claim.save! session[:claim_id] = current_claim.claim_ids + session[journey_session_key] = journey_session.id redirect_to claim_path(current_journey_routing_name, page_sequence.slugs.first.to_sym) end diff --git a/app/controllers/concerns/journey_concern.rb b/app/controllers/concerns/journey_concern.rb index 408200b787..ba66482304 100644 --- a/app/controllers/concerns/journey_concern.rb +++ b/app/controllers/concerns/journey_concern.rb @@ -21,6 +21,10 @@ def current_claim @current_claim ||= claim_from_session || build_new_claim end + def journey_session + @journey_session ||= find_journey_session || create_journey_session! + end + private def claim_from_session @@ -46,4 +50,16 @@ def build_new_claims ) end end + + def find_journey_session + Journeys::Session.find_by(id: session[journey_session_key]) + end + + def create_journey_session! + Journeys::Session.create!(journey: params[:journey]) + end + + def journey_session_key + :"#{params[:journey]}_journeys_session_id" + end end diff --git a/app/models/journeys.rb b/app/models/journeys.rb index acd1cba0dd..7808a8e6b4 100644 --- a/app/models/journeys.rb +++ b/app/models/journeys.rb @@ -1,6 +1,10 @@ module Journeys extend self + def self.table_name_prefix + "journeys_" + end + JOURNEYS = [ AdditionalPaymentsForTeaching, TeacherStudentLoanReimbursement diff --git a/app/models/journeys/session.rb b/app/models/journeys/session.rb new file mode 100644 index 0000000000..f523c83e2d --- /dev/null +++ b/app/models/journeys/session.rb @@ -0,0 +1,7 @@ +module Journeys + class Session < ApplicationRecord + validates :journey, + presence: true, + inclusion: {in: Journeys.all_routing_names} + end +end diff --git a/db/migrate/20240508081918_create_journeys_sessions.rb b/db/migrate/20240508081918_create_journeys_sessions.rb new file mode 100644 index 0000000000..201180c3d0 --- /dev/null +++ b/db/migrate/20240508081918_create_journeys_sessions.rb @@ -0,0 +1,10 @@ +class CreateJourneysSessions < ActiveRecord::Migration[7.0] + def change + create_table :journeys_sessions, id: :uuid do |t| + t.jsonb :answers, default: {} + t.string :journey, null: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index fa5a26c38d..ea49a44bad 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_04_24_134854) do +ActiveRecord::Schema[7.0].define(version: 2024_05_08_081918) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" enable_extension "pgcrypto" @@ -188,6 +188,13 @@ t.index ["created_at"], name: "index_journey_configurations_on_created_at" end + create_table "journeys_sessions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.jsonb "answers", default: {} + t.string "journey", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "levelling_up_premium_payments_awards", force: :cascade do |t| t.string "academic_year", limit: 9, null: false t.integer "school_urn", null: false diff --git a/spec/factories/journeys/sessions.rb b/spec/factories/journeys/sessions.rb new file mode 100644 index 0000000000..6a3cc5d2e9 --- /dev/null +++ b/spec/factories/journeys/sessions.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :journeys_session, class: "Journeys::Session" do + journey { "additional-payments" } + end +end diff --git a/spec/models/journeys/session_spec.rb b/spec/models/journeys/session_spec.rb new file mode 100644 index 0000000000..08f64d9140 --- /dev/null +++ b/spec/models/journeys/session_spec.rb @@ -0,0 +1,15 @@ +require "rails_helper" + +RSpec.describe Journeys::Session, type: :model do + describe "validations" do + describe "journey" do + it { is_expected.to validate_presence_of(:journey) } + + it do + is_expected.to( + validate_inclusion_of(:journey).in_array(Journeys.all_routing_names) + ) + end + end + end +end