Skip to content

Commit

Permalink
R2-3008 - SubformSummaryFieldsService does not need to be a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
dhernandez-quoin committed Dec 19, 2024
1 parent a451a0e commit 36dbb8e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/services/subform_summary_fields_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SubformSummaryFieldsService
attr_accessor :with_cache

def self.instance
@instance ||= new(Rails.configuration.use_app_cache)
new(Rails.configuration.use_app_cache)
end

alias with_cache? with_cache
Expand Down
36 changes: 28 additions & 8 deletions spec/services/permitted_form_fields_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@

describe '#permitted_fields' do
it 'lists all writeable fields' do
permitted_fields = service.permitted_fields(role, 'case', 'primeromodule-cpa', true)
permitted_fields = service.permitted_fields([role], 'case', 'primeromodule-cpa', true)
expect(permitted_fields.size).to eq(11)
expect(permitted_fields.map(&:name)).to match_array(
%w[name age sex national_id_no consent_for_services current_address protection_concerns
Expand All @@ -364,7 +364,7 @@
end

it 'lists all readable fields' do
permitted_fields = service.permitted_fields(role, 'case', 'primeromodule-cpa', false)
permitted_fields = service.permitted_fields([role], 'case', 'primeromodule-cpa', false)
expect(permitted_fields.size).to eq(14)
expect(permitted_fields.map(&:name)).to match_array(
%w[name age sex national_id_no consent_for_services current_address protection_concerns
Expand All @@ -373,7 +373,7 @@
end

it 'includes action subforms when writeable for the module' do
permitted_fields = service.permitted_fields(role_with_actions, 'case', 'primeromodule-cpa', true)
permitted_fields = service.permitted_fields([role_with_actions], 'case', 'primeromodule-cpa', true)
expect(permitted_fields.size).to eq(13)
expect(permitted_fields.map(&:name)).to match_array(
%w[name age sex national_id_no consent_for_services current_address protection_concerns other_documents
Expand All @@ -382,37 +382,57 @@
end

it 'excludes action subforms when readable' do
permitted_fields = service.permitted_fields(role_with_actions, 'case', 'primeromodule-cpa', false)
permitted_fields = service.permitted_fields([role_with_actions], 'case', 'primeromodule-cpa', false)
expect(permitted_fields.size).to eq(14)
expect(permitted_fields.map(&:name)).to_not include(:notes_section, :services_section)
end

it 'includes mrm subforms when writeable' do
permitted_fields = service.permitted_fields(role_mrm, 'incident', PrimeroModule::MRM, true)
permitted_fields = service.permitted_fields([role_mrm], 'incident', PrimeroModule::MRM, true)
expect(permitted_fields.size).to eq(2)
expect(permitted_fields.map(&:name)).to match_array(%w[another_field killing])
end

context 'when a user has access to the notes_section field and can add notes' do
it 'returns fields for the specified parent_form and module' do
permitted_fields = service.permitted_fields(role_with_notes, 'case', 'primeromodule-cpa', true)
permitted_fields = service.permitted_fields([role_with_notes], 'case', 'primeromodule-cpa', true)
expect(permitted_fields.size).to eq(1)
expect(permitted_fields.map { |field| field.form_section.parent_form }).to match_array(%w[case])
expect(permitted_fields.map(&:name)).to match_array(%w[notes_section])
end

it 'returns the notes_section fields for the cp module' do
permitted_fields = service.permitted_fields(role_cp, 'case', PrimeroModule::CP, true)
permitted_fields = service.permitted_fields([role_cp], 'case', PrimeroModule::CP, true)
expect(permitted_fields.size).to eq(1)
expect(permitted_fields.first.subform.fields.map(&:name)).to match_array(%w[name comment])
end

it 'returns the notes_section fields for the gbv module' do
permitted_fields = service.permitted_fields(role_gbv, 'case', PrimeroModule::GBV, true)
permitted_fields = service.permitted_fields([role_gbv], 'case', PrimeroModule::GBV, true)
expect(permitted_fields.size).to eq(1)
expect(permitted_fields.first.subform.fields.map(&:name)).to match_array(%w[name age])
end
end

describe 'when with_cache?' do
let(:service_with_cache) { PermittedFormFieldsService.new(true) }

it 'lists all writeable and readable fields' do
permitted_writeable_fields = service_with_cache.permitted_fields([role], 'case', 'primeromodule-cpa', true)
permitted_readable_fields = service_with_cache.permitted_fields([role], 'case', 'primeromodule-cpa', false)

expect(permitted_writeable_fields.size).to eq(11)
expect(permitted_writeable_fields.map(&:name)).to match_array(
%w[name age sex national_id_no consent_for_services current_address protection_concerns
registration_date created_on family_details other_documents]
)
expect(permitted_readable_fields.size).to eq(14)
expect(permitted_readable_fields.map(&:name)).to match_array(
%w[name age sex national_id_no consent_for_services current_address protection_concerns
registration_date created_on separator1 other_documents family_details interview_date consent_for_referral]
)
end
end
end

describe '#permitted_field_names' do
Expand Down
78 changes: 78 additions & 0 deletions spec/services/subform_summary_fields_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

# Copyright (c) 2014 - 2023 UNICEF. All rights reserved.

require 'rails_helper'

describe SubformSummaryFieldsService do
before :each do
clean_data(Field, FormSection)
end

let(:service) { SubformSummaryFieldsService.new }

let!(:form_section1) do
score_section = FormSection.create!(
unique_id: 'scores_section', parent_form: 'case', name_en: 'Scores', is_nested: true,
fields: [
Field.new(name: 'date', type: Field::DATE_FIELD, display_name_en: 'Date'),
Field.new(name: 'score', type: Field::NUMERIC_FIELD, display_name_en: 'Score')
]
)

FormSection.create!(
unique_id: 'form_section1', parent_form: 'case', name_en: 'form_section1',
fields: [
Field.new(name: 'name', type: Field::TEXT_FIELD, display_name_en: 'Name'),
Field.new(name: 'age', type: Field::NUMERIC_FIELD, display_name_en: 'Age'),
Field.new(name: 'sex', type: Field::SELECT_BOX, display_name_en: 'Sex'),
Field.new(
name: 'most_recent_score',
type: Field::NUMERIC_FIELD,
display_name_en: 'Most Recent Score',
subform_summary: {
subform_field_name: 'scores_section',
first: {
field_name: 'score',
order_by: 'date',
order: 'asc'
}
}
),
Field.new(
name: 'last_score',
type: Field::NUMERIC_FIELD,
display_name_en: 'Last Score',
visible: false,
subform_summary: {
subform_field_name: 'scores_section',
first: {
field_name: 'score',
order_by: 'date',
order: 'asc'
}
}
),
Field.new(name: 'scores_section', display_name_en: 'Scores', type: Field::SUBFORM, subform: score_section)
]
)
end

it 'returns all the subform summary fields' do
summary_fields = SubformSummaryFieldsService.instance.subform_summary_fields('case')

expect(summary_fields.size).to eq(1)
expect(summary_fields.map(&:name)).to match_array(%w[most_recent_score])
end

describe 'when with_cache?' do
let(:service) { SubformSummaryFieldsService.new(true) }

it 'returns all the subform summary fields' do
summary_fields = SubformSummaryFieldsService.instance.subform_summary_fields('case')

expect(summary_fields.size).to eq(1)
expect(summary_fields.map(&:name)).to match_array(%w[most_recent_score])
end
end
end

0 comments on commit 36dbb8e

Please sign in to comment.