Skip to content

Commit 2ebf259

Browse files
committed
Use parent fields on consent forms
This updates the consent flow to use the parent fields on the consent forms directly rather than referring to the related parent, which no longer exists.
1 parent 5a81ad4 commit 2ebf259

File tree

18 files changed

+155
-114
lines changed

18 files changed

+155
-114
lines changed

app/components/app_matching_criteria_component.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(consent_form:)
1010
end
1111

1212
def parent_name
13-
@consent_form.parent.name
13+
@consent_form.parent_name
1414
end
1515

1616
def child_full_name
@@ -31,6 +31,6 @@ def address_fields
3131
end
3232

3333
def parent_guardian_or_other
34-
@consent_form.parent.relationship_label
34+
@consent_form.parent_relationship_label
3535
end
3636
end

app/controllers/parent_interface/consent_forms/edit_controller.rb

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class ConsentForms::EditController < ConsentForms::BaseController
77
before_action :set_steps
88
before_action :setup_wizard_translated
99
before_action :validate_params, only: %i[update]
10-
before_action :set_parent
11-
before_action :set_health_answer, if: -> { is_health_question_step? }
10+
before_action :set_health_answer, if: :is_health_question_step?
1211
before_action :set_follow_up_changes_start_page, only: %i[show]
1312

1413
def show
@@ -17,45 +16,36 @@ def show
1716

1817
def update
1918
model = @consent_form
19+
2020
if is_health_question_step?
2121
@health_answer.assign_attributes(health_answer_params)
2222

23-
@consent_form.assign_attributes(
23+
model.assign_attributes(
2424
wizard_step: current_step,
2525
health_question_number: @question_number
2626
)
27-
elsif current_step.in?(%i[parent contact_method])
28-
model = @consent_form.draft_parent
29-
model.assign_attributes(parent_params)
30-
31-
if @consent_form.draft_parent.parental_responsibility == "no"
32-
return(
33-
redirect_to session_parent_interface_consent_form_cannot_consent_responsibility_path(
34-
@session,
35-
@consent_form
36-
)
37-
)
38-
end
39-
40-
if model.valid?
41-
@consent_form.update!(wizard_step: current_step, draft_parent: model)
42-
end
4327
else
4428
@consent_form.assign_attributes(update_params)
4529
end
4630

47-
set_steps # The wizard_steps can change after certain attrs change
48-
setup_wizard_translated # Next/previous steps can change after steps change
49-
5031
if current_step == :school && @consent_form.is_this_their_school == "no"
51-
return(
52-
redirect_to session_parent_interface_consent_form_cannot_consent_school_path(
53-
@session,
54-
@consent_form
55-
)
56-
)
32+
redirect_to session_parent_interface_consent_form_cannot_consent_school_path(
33+
@session,
34+
@consent_form
35+
) and return
5736
end
5837

38+
if current_step == :parent &&
39+
@consent_form.parental_responsibility == "no"
40+
redirect_to session_parent_interface_consent_form_cannot_consent_responsibility_path(
41+
@session,
42+
@consent_form
43+
) and return
44+
end
45+
46+
set_steps # The wizard_steps can change after certain attrs change
47+
setup_wizard_translated # Next/previous steps can change after steps change
48+
5949
skip_to_confirm_or_next_health_question
6050

6151
render_wizard model
@@ -83,7 +73,18 @@ def update_params
8373
date_of_birth(1i)
8474
],
8575
school: %i[is_this_their_school],
86-
contact_method: %i[contact_method_type contact_method_other_details],
76+
parent: %i[
77+
parent_email
78+
parent_name
79+
parent_phone
80+
parent_relationship_other_name
81+
parent_relationship_type
82+
parental_responsibility
83+
],
84+
contact_method: %i[
85+
parent_contact_method_type
86+
parent_contact_method_other_details
87+
],
8788
consent: %i[response],
8889
reason: %i[reason],
8990
reason_notes: %i[reason_notes],
@@ -98,21 +99,6 @@ def update_params
9899
.merge(wizard_step: current_step)
99100
end
100101

101-
def parent_params
102-
params.fetch(:parent, {}).permit(
103-
%i[
104-
name
105-
email
106-
phone
107-
relationship
108-
relationship_other
109-
contact_method_type
110-
contact_method_other_details
111-
parental_responsibility
112-
]
113-
)
114-
end
115-
116102
def health_answer_params
117103
params.fetch(:health_answer, {}).permit(%i[response notes])
118104
end
@@ -132,10 +118,6 @@ def set_health_answer
132118
@health_answer = @consent_form.health_answers[@question_number]
133119
end
134120

135-
def set_parent
136-
@consent_form.build_draft_parent if @consent_form.draft_parent.blank?
137-
end
138-
139121
def validate_params
140122
case current_step
141123
when :date_of_birth

app/controllers/parent_interface/consent_forms_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def confirm
4040

4141
def record
4242
ActiveRecord::Base.transaction do
43-
@consent_form.draft_parent.update!(recorded_at: Time.zone.now)
4443
@consent_form.update!(recorded_at: Time.zone.now)
4544
end
4645

app/mailers/application_mailer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def opts(session, patient, parent)
1919
end
2020

2121
def to
22-
@parent.email
22+
@parent.is_a?(ConsentForm) ? @parent.parent_email : @parent.email
2323
end
2424

2525
def reply_to_id
@@ -61,7 +61,7 @@ def short_patient_name_apos
6161
end
6262

6363
def parent_name
64-
@parent.name
64+
@parent.is_a?(ConsentForm) ? @parent.parent_name : @parent.name
6565
end
6666

6767
def location_name

app/mailers/consent_form_mailer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def opts(consent_form, consent, session = nil)
5353
@consent = consent
5454

5555
patient = consent_form || consent.patient
56-
parent = consent_form&.parent || consent.parent
56+
parent = consent_form || consent.parent
5757

5858
super(session || consent_form.session, patient, parent)
5959
end

app/models/cohort_import_row.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CohortImportRow
1717
validates :parent_name, presence: true
1818
validates :parent_relationship, presence: true
1919
validates :parent_email, presence: true, notify_safe_email: true
20-
validates :parent_phone, phone: true, if: -> { parent_phone.present? }
20+
validates :parent_phone, phone: { allow_blank: true }
2121

2222
validate :zero_or_one_existing_patient
2323

app/models/consent_form.rb

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ class ConsentForm < ApplicationRecord
7979
prefix: "refused_because"
8080
enum :gp_response, %w[yes no dont_know], prefix: true
8181

82+
enum :parent_contact_method_type,
83+
Parent.contact_method_types,
84+
prefix: :parent_contact_method,
85+
validate: {
86+
allow_nil: true
87+
}
88+
enum :parent_relationship_type,
89+
ParentRelationship.types,
90+
prefix: :parent_relationship,
91+
validate: {
92+
allow_nil: true
93+
}
94+
8295
serialize :health_answers, coder: HealthAnswer::ArraySerializer
8396

8497
encrypts :address_line_1,
@@ -90,6 +103,11 @@ class ConsentForm < ApplicationRecord
90103
:gp_name,
91104
:health_answers,
92105
:last_name,
106+
:parent_contact_method_other_details,
107+
:parent_email,
108+
:parent_name,
109+
:parent_phone,
110+
:parent_relationship_other_name,
93111
:reason_notes
94112

95113
validates :address_line_1,
@@ -99,10 +117,23 @@ class ConsentForm < ApplicationRecord
99117
:first_name,
100118
:gp_name,
101119
:last_name,
120+
:parent_contact_method_other_details,
121+
:parent_name,
122+
:parent_relationship_other_name,
102123
length: {
103124
maximum: 300
104125
}
105126

127+
validates :parent_contact_method_other_details,
128+
presence: true,
129+
if: :parent_contact_method_other?
130+
131+
validates :parent_phone, phone: { allow_blank: true }
132+
133+
validates :parent_relationship_other_name,
134+
presence: true,
135+
if: :parent_relationship_other?
136+
106137
validates :reason_notes, length: { maximum: 1000 }
107138

108139
on_wizard_step :name do
@@ -126,6 +157,24 @@ class ConsentForm < ApplicationRecord
126157
validates :is_this_their_school, inclusion: { in: %w[yes no] }
127158
end
128159

160+
on_wizard_step :parent do
161+
validates :parent_name, presence: true
162+
validates :parent_email, notify_safe_email: true
163+
validates :parent_relationship_type, presence: true
164+
end
165+
166+
validates :parental_responsibility,
167+
inclusion: {
168+
in: ["yes"]
169+
},
170+
if: ->(object) do
171+
object.parent_relationship_other? && object.wizard_step == :parent
172+
end
173+
174+
on_wizard_step :contact_method do
175+
validates :parent_contact_method_type, presence: true
176+
end
177+
129178
on_wizard_step :consent do
130179
validates :response, presence: true
131180
end
@@ -209,10 +258,6 @@ def any_health_answers_truthy?
209258
health_answers.any? { _1.response == "yes" }
210259
end
211260

212-
def who_responded
213-
parent&.relationship_label
214-
end
215-
216261
def gelatine_content_status_in_vaccines
217262
# we don't YET track the vaccine type that the user is agreeing to in the consent form,
218263
# so we have to check all vaccines
@@ -278,6 +323,20 @@ def summary_with_route
278323
"#{human_enum_name(:response).capitalize} (online)"
279324
end
280325

326+
def parent_contact_method_description
327+
Parent.new(
328+
contact_method_type: parent_contact_method_type,
329+
contact_method_other_details: parent_contact_method_other_details
330+
).contact_method_description
331+
end
332+
333+
def parent_relationship_label
334+
ParentRelationship.new(
335+
type: parent_relationship_type,
336+
other_name: parent_relationship_other_name
337+
).label
338+
end
339+
281340
private
282341

283342
def refused_and_not_had_it_already?
@@ -320,7 +379,7 @@ def health_answers_valid?
320379
end
321380

322381
def ask_for_contact_method?
323-
Flipper.enabled?(:parent_contact_method) && draft_parent&.phone.present?
382+
Flipper.enabled?(:parent_contact_method) && parent_phone.present?
324383
end
325384

326385
# Because there are branching paths in the consent form journey, fields
@@ -340,6 +399,12 @@ def reset_unused_fields
340399
self.health_answers = []
341400
end
342401

402+
self.parent_contact_method_type = nil if parent_phone.blank?
403+
self.parent_contact_method_other_details =
404+
nil unless parent_contact_method_other?
405+
406+
self.parent_relationship_other_name = nil unless parent_relationship_other?
407+
343408
if consent_given?
344409
self.contact_injection = nil
345410

app/models/parent.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Parent < ApplicationRecord
5050
normalizes :email, with: ->(str) { str.nil? ? nil : str.to_s.downcase.strip }
5151

5252
validates :name, presence: true
53-
validates :phone, phone: true, if: -> { phone.present? }
53+
validates :phone, phone: { allow_blank: true }
5454
validates :email, presence: true, notify_safe_email: true
5555
validates :relationship_other, presence: true, if: -> { relationship_other? }
5656
validate :has_parental_responsibility, if: -> { relationship_other? }
@@ -84,15 +84,9 @@ def contact_method_description
8484

8585
def has_parental_responsibility
8686
return if parental_responsibility == "yes"
87-
if parental_responsibility == "no" && validation_context != :manage_consent
88-
return
89-
end
87+
return if parental_responsibility == "no"
9088

91-
if validation_context == :manage_consent
92-
errors.add(:parental_responsibility, :inclusion)
93-
else
94-
errors.add(:parental_responsibility, :inclusion_on_consent_form)
95-
end
89+
errors.add(:parental_responsibility, :inclusion)
9690
end
9791

9892
private

app/views/consent_forms/review_match.html.erb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="nhsuk-grid-column-two-thirds">
1111
<%= h1 page_title: do %>
1212
<span class="nhsuk-caption-l nhsuk-u-margin-top-2">
13-
Consent response from <%= @consent_form.parent.name %>
13+
Consent response from <%= @consent_form.parent_name %>
1414
</span>
1515
<%= page_title %>
1616
<% end %>
@@ -24,11 +24,11 @@
2424
<%= render AppCardComponent.new do |c|
2525
c.with_heading { "Consent response" }
2626
render AppConsentSummaryComponent.new(
27-
name: @consent_form.parent.name,
28-
relationship: @consent_form.who_responded,
27+
name: @consent_form.parent_name,
28+
relationship: @consent_form.parent_relationship_label,
2929
contact: {
30-
phone: @consent_form.parent.phone,
31-
email: @consent_form.parent.email,
30+
phone: @consent_form.parent_phone,
31+
email: @consent_form.parent_email,
3232
},
3333
response: {
3434
text: @consent_form.summary_with_route,

app/views/consent_forms/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<% page_title = "Search for a child record" %>
99
<%= h1 page_title: do %>
1010
<span class="nhsuk-caption-l nhsuk-u-margin-top-2">
11-
Consent response from <%= @consent_form.parent.name %>
11+
Consent response from <%= @consent_form.parent_name %>
1212
</span>
1313
<%= page_title %>
1414
<% end %>

0 commit comments

Comments
 (0)