Skip to content

Commit

Permalink
Withdrawal reasons models and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
elceebee committed Dec 20, 2024
1 parent e34d965 commit f0539c7
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/application_choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ApplicationChoice < ApplicationRecord

has_many :notes, dependent: :destroy
has_many :interviews, dependent: :destroy
has_many :withdrawal_reasons, dependent: :destroy
has_many :draft_withdrawal_reasons, -> { draft }, class_name: 'WithdrawalReason'

has_many :work_experiences, as: :experienceable, class_name: 'ApplicationWorkExperience'
has_many :volunteering_experiences, as: :experienceable, class_name: 'ApplicationVolunteeringExperience'
Expand Down
28 changes: 28 additions & 0 deletions app/models/withdrawal_reason.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class WithdrawalReason < ApplicationRecord
belongs_to :application_choice
validates :reason, presence: true

PERSONAL_CIRCUMSTANCES_KEY = 'personal-circumstances-have-changed'.freeze
CONFIG_PATH = 'config/candidate_withdrawal_reasons.yml'.freeze

enum :status, {
draft: 'draft',
published: 'published',
}

def publish!
update!(status: 'published')
end

def self.selectable_reasons
YAML.load_file(CONFIG_PATH).fetch('candidate-withdrawal-reasons')
end

def self.find_reason_options(reason = '')
if reason.empty?
selectable_reasons
else
selectable_reasons.dig(*reason.split('.'))
end
end
end
36 changes: 36 additions & 0 deletions config/candidate_withdrawal_reasons.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
candidate-withdrawal-reasons:
applying-to-another-provider:
accepted-another-offer: {}
seen-a-course-that-suits-me-better: {}
provider-has-not-replied-to-me: {}
location-is-too-far-away: {}
personal-circumstances-have-changed:
concerns-about-cost-of-doing-course: {}
concerns-about-having-enough-time-to-train: {}
concerns-about-training-with-a-disability-or-health-condition: {}
other: {}
course-no-longer-available: {}
other: {}
change-or-update-application-with-this-provider:
update-my-application-correct-an-error-or-add-information: {}
change-study-pattern: {}
apply-for-a-different-subject-with-the-same-provider: {}
other: {}
apply-in-the-future:
personal-circumstances-have-changed:
concerns-about-cost-of-doing-course: {}
concerns-about-having-enough-time-to-train: {}
concerns-about-training-with-a-disability-or-health-condition: {}
other: {}
gain-more-experience: {}
improve-qualifications: {}
other: {}
do-not-want-to-train-anymore:
personal-circumstances-have-changed:
concerns-about-cost-of-doing-course: {}
concerns-about-having-enough-time-to-train: {}
concerns-about-training-with-a-disability-or-health-condition: {}
other: {}
another-career-path-or-accepted-a-job-offer: {}
other: {}
other: {}
1 change: 1 addition & 0 deletions spec/models/application_choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
it { is_expected.to have_many(:work_experiences).class_name('ApplicationWorkExperience') }
it { is_expected.to have_many(:volunteering_experiences).class_name('ApplicationVolunteeringExperience') }
it { is_expected.to have_many(:work_history_breaks).class_name('ApplicationWorkHistoryBreak') }
it { is_expected.to have_many(:withdrawal_reasons) }

describe 'auditing', :with_audited do
it 'creates audit entries' do
Expand Down
90 changes: 90 additions & 0 deletions spec/models/withdrawal_reason_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'rails_helper'

RSpec.describe WithdrawalReason do
describe 'validations' do
it { is_expected.to validate_presence_of(:reason) }
end

describe 'associations' do
it { is_expected.to belong_to(:application_choice) }
end

describe '#selectable_reasons' do
it 'returns a hash of selectable reasons' do
expect(described_class.selectable_reasons).to eq(selectable_reasons)
end
end

describe '#find_reason_options' do
context 'without a reason_id' do
it 'returns all selectable reasons' do
expect(described_class.find_reason_options).to eq(selectable_reasons)
end
end

context 'with primary reason id' do
it 'returns all the reasons under that id' do
expect(
described_class.find_reason_options('applying-to-another-provider'),
).to eq(selectable_reasons['applying-to-another-provider'])
end
end

context 'with nested reason id' do
it 'returns all secondary reasons when there are any' do
expect(
described_class.find_reason_options('applying-to-another-provider.personal-circumstances-have-changed'),
).to eq({ 'concerns-about-cost-of-doing-course' => {},
'concerns-about-having-enough-time-to-train' => {},
'concerns-about-training-with-a-disability-or-health-condition' => {},
'other' => {} })
end

it 'returns an empty hash when there are none' do
expect(
described_class.find_reason_options('applying-to-another-provider.location-is-too-far-away'),
).to eq({})
end
end
end

private

def selectable_reasons
{ 'applying-to-another-provider' =>
{ 'accepted-another-offer' => {},
'seen-a-course-that-suits-me-better' => {},
'provider-has-not-replied-to-me' => {},
'location-is-too-far-away' => {},
'personal-circumstances-have-changed' =>
{ 'concerns-about-cost-of-doing-course' => {},
'concerns-about-having-enough-time-to-train' => {},
'concerns-about-training-with-a-disability-or-health-condition' => {},
'other' => {} },
'course-no-longer-available' => {},
'other' => {} },
'change-or-update-application-with-this-provider' =>
{ 'update-my-application-correct-an-error-or-add-information' => {},
'change-study-pattern' => {},
'apply-for-a-different-subject-with-the-same-provider' => {},
'other' => {} },
'apply-in-the-future' =>
{ 'personal-circumstances-have-changed' =>
{ 'concerns-about-cost-of-doing-course' => {},
'concerns-about-having-enough-time-to-train' => {},
'concerns-about-training-with-a-disability-or-health-condition' => {},
'other' => {} },
'gain-more-experience' => {},
'improve-qualifications' => {},
'other' => {} },
'do-not-want-to-train-anymore' =>
{ 'personal-circumstances-have-changed' =>
{ 'concerns-about-cost-of-doing-course' => {},
'concerns-about-having-enough-time-to-train' => {},
'concerns-about-training-with-a-disability-or-health-condition' => {},
'other' => {} },
'another-career-path-or-accepted-a-job-offer' => {},
'other' => {} },
'other' => {} }
end
end

0 comments on commit f0539c7

Please sign in to comment.