-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ApplicationForm::ColumnSectionMapping to new file
- Loading branch information
1 parent
114698e
commit 134465a
Showing
3 changed files
with
161 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
93
spec/models/application_form/column_section_mapping_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters