Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suitability record models #2300

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/models/suitability_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: suitability_records
#
# id :bigint not null, primary key
# archive_note :text default(""), not null
# archived_at :datetime
# country_code :text default(""), not null
# date_of_birth :date
# note :text not null
# created_at :datetime not null
# updated_at :datetime not null
#
class SuitabilityRecord < ApplicationRecord
has_and_belongs_to_many :application_forms
has_many :emails, class_name: "SuitabilityRecord::Email"
has_many :names, class_name: "SuitabilityRecord::Name"

scope :active, -> { where(archived_at: nil) }

validates :note, presence: true
validates :archive_note, presence: true, if: :archived?

def archived?
archived_at.present?
end

class Email < ApplicationRecord
self.table_name = "suitability_record_emails"

validates :value, presence: true
validates :canonical, presence: true

def value=(value)
self.value = value
self.canonical = EmailAddress.canonical(value)
end
end

class Name < ApplicationRecord
self.table_name = "suitability_record_names"

validates :value, presence: true
end
end
23 changes: 23 additions & 0 deletions config/analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
- written_statement_confirmation
- written_statement_optional
- written_statement_status
:application_forms_suitability_records:
- application_form_id
- suitability_record_id
:assessment_sections:
- assessed_at
- assessment_id
Expand Down Expand Up @@ -314,6 +317,26 @@
- updated_at
- verify_permission
- withdraw_permission
:suitability_record_emails:
- canonical
- created_at
- id
- suitability_record_id
- updated_at
- value
:suitability_record_names:
- created_at
- id
- suitability_record_id
- updated_at
- value
:suitability_records:
- archived_at
- country_code
- created_at
- date_of_birth
- id
- updated_at
:teachers:
- canonical_email
- created_at
Expand Down
3 changes: 3 additions & 0 deletions config/analytics_blocklist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
- invitation_token
- reset_password_token
- unlock_token
:suitability_records:
- archive_note
- note
:teachers:
- access_your_teaching_qualifications_url
:timeline_events:
Expand Down
7 changes: 7 additions & 0 deletions config/analytics_hidden_pii.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
- last_sign_in_ip
- name
- unconfirmed_email
:suitability_record_emails:
- canonical
- value
:suitability_record_names:
- value
:suitability_records:
- date_of_birth
:teachers:
- canonical_email
- current_sign_in_ip
Expand Down
32 changes: 32 additions & 0 deletions db/migrate/20240711100342_add_suitability_records.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class AddSuitabilityRecords < ActiveRecord::Migration[7.1]
def change
create_table :suitability_records do |t|
t.text :country_code, limit: 7, null: false, default: ""
t.date :date_of_birth

t.text :note, null: false

t.datetime :archived_at
t.text :archive_note, null: false, default: ""

t.timestamps
end

create_table :suitability_record_emails do |t|
t.text :value, null: false
t.text :canonical, null: false
t.references :suitability_record, null: false, foreign_key: true
t.timestamps
end

create_table :suitability_record_names do |t|
t.text :value, null: false
t.references :suitability_record, null: false, foreign_key: true
t.timestamps
end

create_join_table :suitability_records, :application_forms
end
end
36 changes: 35 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified docs/diagram.pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions spec/factories/suitability_records.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

FactoryBot.define do
factory :suitability_record do
note { Faker::Lorem.sentence }

trait :archived do
archived_at { Time.zone.now }
archive_note { Faker::Lorem.sentence }
end
end

factory :suitability_record_email do
association :suitability_record

value { Faker::Name.name }
end

factory :suitability_record_name do
association :suitability_record

value { Faker::Internet.email }
canonical { EmailAddress.canonical(value) }
end
end
20 changes: 20 additions & 0 deletions spec/models/suitability_record_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe SuitabilityRecord, type: :model do
subject(:suitability_record) { build(:suitability_record) }

describe "validations" do
it { is_expected.to be_valid }

it { is_expected.to validate_presence_of(:note) }
it { is_expected.not_to validate_presence_of(:archive_note) }

context "when archived" do
before { suitability_record.archived_at = Time.zone.now }

it { is_expected.to validate_presence_of(:archive_note) }
end
end
end
Loading