Skip to content

Commit

Permalink
Introduces journey session model
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rjlynch committed May 8, 2024
1 parent b6fe794 commit d472974
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,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

Expand Down
16 changes: 16 additions & 0 deletions app/controllers/concerns/journey_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,4 +50,16 @@ def build_new_claims
)
end
end

def find_journey_session
Journeys::Session.find_by(id: 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
4 changes: 4 additions & 0 deletions app/models/journeys.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Journeys
extend self

def self.table_name_prefix
"journeys_"
end

JOURNEYS = [
AdditionalPaymentsForTeaching,
TeacherStudentLoanReimbursement
Expand Down
7 changes: 7 additions & 0 deletions app/models/journeys/session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Journeys
class Session < ApplicationRecord
validates :journey,
presence: true,
inclusion: {in: Journeys.all_routing_names}
end
end
10 changes: 10 additions & 0 deletions db/migrate/20240508081918_create_journeys_sessions.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/journeys/sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :journeys_session, class: "Journeys::Session" do
journey { "additional-payments" }
end
end
15 changes: 15 additions & 0 deletions spec/models/journeys/session_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d472974

Please sign in to comment.