Skip to content

Commit

Permalink
Move ApplicationForm::ColumnSectionMapping to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Oct 24, 2023
1 parent 114698e commit 134465a
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 80 deletions.
68 changes: 68 additions & 0 deletions app/models/application_form/column_section_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class ApplicationForm
module ColumnSectionMapping
def by_column(*column_names)
mapping = ActiveSupport::HashWithIndifferentAccess.new({
# Personal information
'date_of_birth' => 'personal_information',
'first_name' => 'personal_information',
'last_name' => 'personal_information',

# Contact Information
'phone_number' => 'contact_information',
'address_line1' => 'contact_information',
'address_line2' => 'contact_information',
'address_line3' => 'contact_information',
'address_line4' => 'contact_information',
'country' => 'contact_information',
'postcode' => 'contact_information',
'region_code' => 'contact_information',

# Interview Preferences
'interview_preferences' => 'interview_preferences',

# Disability
'disability_disclosure' => 'disability_disclosure',
})

return mapping[column_names.first] if column_names.length == 1

Array(column_names).each_with_object([]) do |column_name, set|
set << mapping[column_name]
end.uniq
end

def by_section(*sections)
mapping = ActiveSupport::HashWithIndifferentAccess.new({
# Personal information
'personal_information' => %w[
date_of_birth
first_name
last_name
],

# Contact Information
'contact_information' => %w[
phone_number
address_line1
address_line2
address_line3
address_line4
country
postcode
region_code
],

# Interview Preferences
'interview_preferences' => ['interview_preferences'],

# Disability
'disability_disclosure' => ['disability_disclosure'],
})

Array(sections).flat_map do |section|
mapping[section]
end.compact
end
module_function :by_column, :by_section
end
end
93 changes: 93 additions & 0 deletions spec/models/application_form/column_section_mapping_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require 'rails_helper'

RSpec.describe ApplicationForm::ColumnSectionMapping do
describe '.by_section' do
subject { described_class.by_section(section_name) }

context 'with nil argument' do
let(:section_name) { nil }

it { is_expected.to eq([]) }
end

context 'with one argument' do
let(:section_name) { 'personal_information' }

it { is_expected.to eq(%w[date_of_birth first_name last_name]) }
end

context 'with one symbol argument' do
let(:section_name) { :personal_information }

it { is_expected.to eq(%w[date_of_birth first_name last_name]) }
end

context 'with two arguments' do
let(:section_name) { %w[personal_information disability_disclosure] }

it 'returns the correct collection of columns' do
expect(described_class.by_section(*section_name)).to eq(%w[date_of_birth first_name last_name disability_disclosure])
end
end

context 'with two arguments when one does not match' do
let(:section_name) { %w[personal_information no_entry] }

it 'returns the correct collection of columns' do
expect(described_class.by_section(*section_name)).to eq(%w[date_of_birth first_name last_name])
end
end
end

describe '.by_column' do
subject { described_class.by_column(column_name) }

context 'with nil argument' do
let(:column_name) { nil }

it { is_expected.to be_nil }
end

context 'with one argument' do
let(:column_name) { 'date_of_birth' }

it { is_expected.to eq('personal_information') }
end

context 'with one symbol argument' do
let(:column_name) { :date_of_birth }

it { is_expected.to eq('personal_information') }
end

context 'with one argument and it is not present' do
let(:column_name) { 'no_entry' }

it { is_expected.to be_nil }
end

context 'with two arguments that resolve to the same value' do
let(:column_names) { %w[date_of_birth first_name] }

it 'returns the value only once' do
expect(described_class.by_column(*column_names)).to eq(%w[personal_information])
end
end

context 'with two arguments' do
let(:column_names) { %w[date_of_birth disability_disclosure] }

it 'returns the values in an array' do
expect(described_class.by_column(*column_names)).to eq(%w[personal_information disability_disclosure])
end
end

context 'with two arguments and one is not present' do
let(:column_names) { %w[date_of_birth no_entry] }

it 'returns array with the position corresponding to the unmatched as nil' do
expect(described_class.by_column(*column_names)).to eq(['personal_information', nil])
end
end
end
end
80 changes: 0 additions & 80 deletions spec/models/application_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1123,84 +1123,4 @@ def application_form_with_course_option_for_provider_with(level:)
end
end
end

describe described_class::ColumnSectionMapping do
describe '.by_section' do
subject { described_class.by_section(section_name) }

context 'with nil argument' do
let(:section_name) { nil }

it { is_expected.to eq([]) }
end

context 'with one argument' do
let(:section_name) { 'personal_information' }

it { is_expected.to eq(%w[date_of_birth first_name last_name]) }
end

context 'with two arguments' do
let(:section_name) { %w[personal_information disability_disclosure] }

it 'returns the correct collection of columns' do
expect(described_class.by_section(*section_name)).to eq(%w[date_of_birth first_name last_name disability_disclosure])
end
end

context 'with two arguments when one does not match' do
let(:section_name) { %w[personal_information no_entry] }

it 'returns the correct collection of columns' do
expect(described_class.by_section(*section_name)).to eq(%w[date_of_birth first_name last_name])
end
end
end

describe '.by_column' do
subject { described_class.by_column(column_name) }

context 'with nil argument' do
let(:column_name) { nil }

it { is_expected.to be_nil }
end

context 'with one argument' do
let(:column_name) { 'date_of_birth' }

it { is_expected.to eq('personal_information') }
end

context 'with one argument and it is not present' do
let(:column_name) { 'no_entry' }

it { is_expected.to be_nil }
end

context 'with two arguments that resolve to the same value' do
let(:column_names) { %w[date_of_birth first_name] }

it 'returns the value only once' do
expect(described_class.by_column(*column_names)).to eq(%w[personal_information])
end
end

context 'with two arguments' do
let(:column_names) { %w[date_of_birth disability_disclosure] }

it 'returns the values in an array' do
expect(described_class.by_column(*column_names)).to eq(%w[personal_information disability_disclosure])
end
end

context 'with two arguments and one is not present' do
let(:column_names) { %w[date_of_birth no_entry] }

it 'returns array with the position corresponding to the unmatched as nil' do
expect(described_class.by_column(*column_names)).to eq(['personal_information', nil])
end
end
end
end
end

0 comments on commit 134465a

Please sign in to comment.