Skip to content

Commit f35d9bb

Browse files
committed
Refactor consent contact options
This refactors how the options for the "who" step of the draft consent form are determined to simplify the code, and allow for a more complex set up around self-consent. Jira-Issue: MAV-5915
1 parent 4204198 commit f35d9bb

2 files changed

Lines changed: 69 additions & 57 deletions

File tree

app/controllers/draft_consents_controller.rb

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class DraftConsentsController < ApplicationController
1212

1313
include WizardControllerConcern
1414

15-
before_action :set_triage_form, if: :includes_triage_step?
16-
before_action :set_parent_options, if: -> { current_step == :who }
1715
before_action :set_back_link_path
16+
before_action :set_new_or_existing_contact_options, if: :is_who_step?
17+
before_action :set_triage_form, if: :includes_triage_step?
1818

1919
def show
2020
authorize Consent, :edit?
@@ -213,6 +213,64 @@ def set_steps
213213
self.steps = @draft_consent.wizard_steps
214214
end
215215

216+
def set_back_link_path
217+
@back_link_path =
218+
if @draft_consent.editing?
219+
wizard_path("confirm")
220+
elsif current_step == @draft_consent.wizard_steps.first
221+
session_patient_programme_path(@session, @patient, @programme)
222+
else
223+
previous_wizard_path
224+
end
225+
end
226+
227+
def is_who_step? = current_step == :who
228+
229+
NewOrExistingContactOption = Struct.new(:value, :label, :hint)
230+
231+
def set_new_or_existing_contact_options
232+
@new_or_existing_contact_options = []
233+
234+
is_gillick_competent =
235+
@patient
236+
.gillick_assessments
237+
.order(created_at: :desc)
238+
.for_session(@session)
239+
.for_programme(@programme)
240+
&.first
241+
&.gillick_competent?
242+
243+
if is_gillick_competent
244+
@new_or_existing_contact_options << NewOrExistingContactOption.new(
245+
value: "patient",
246+
label: "Child (Gillick competent)"
247+
)
248+
end
249+
250+
parent_relationships =
251+
(
252+
@patient.parent_relationships.includes(:parent) +
253+
@patient
254+
.consents
255+
.where(programme_type: @programme.type)
256+
.filter_map(&:parent_relationship)
257+
).compact.uniq.sort_by(&:label)
258+
259+
@new_or_existing_contact_options +=
260+
parent_relationships.map do |parent_relationship|
261+
NewOrExistingContactOption.new(
262+
value: parent_relationship.parent.id,
263+
label: parent_relationship.label_with_parent,
264+
hint: parent_relationship.parent.contact_label
265+
)
266+
end
267+
268+
@new_or_existing_contact_options << NewOrExistingContactOption.new(
269+
value: "new",
270+
label: "Add a new parental contact"
271+
)
272+
end
273+
216274
def includes_triage_step?
217275
current_step.in?(%i[triage confirm]) && steps.include?("triage")
218276
end
@@ -235,28 +293,6 @@ def set_triage_form
235293
end
236294
end
237295

238-
def set_parent_options
239-
@parent_options =
240-
(
241-
@patient.parent_relationships.includes(:parent) +
242-
@patient
243-
.consents
244-
.select { it.programme_type == @programme.type }
245-
.filter_map(&:parent_relationship)
246-
).compact.uniq.sort_by(&:label)
247-
end
248-
249-
def set_back_link_path
250-
@back_link_path =
251-
if @draft_consent.editing?
252-
wizard_path("confirm")
253-
elsif current_step == @draft_consent.wizard_steps.first
254-
session_patient_programme_path(@session, @patient, @programme)
255-
else
256-
previous_wizard_path
257-
end
258-
end
259-
260296
# Returns:
261297
# {
262298
# question_0: %i[notes response],

app/views/draft_consents/who.html.erb

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,19 @@
22
<%= govuk_back_link(href: @back_link_path) %>
33
<% end %>
44

5-
<% page_title = "Who are you trying to get consent from?" %>
6-
7-
<%= h1 page_title: do %>
8-
<span class="nhsuk-caption-l">
9-
<%= @patient.full_name %>
10-
</span>
11-
<%= page_title %>
12-
<% end %>
13-
14-
<% gillick_competent = @patient.gillick_assessments.order(created_at: :desc).for_session(@session).for_programme(@programme)&.first&.gillick_competent? %>
5+
<% legend = "Who are you trying to get consent from?" %>
6+
<% content_for :page_title, legend %>
157

168
<%= form_with model: @draft_consent, url: wizard_path, method: :put do |f| %>
179
<%= f.mavis_error_summary %>
1810

19-
<%= f.govuk_radio_buttons_fieldset(:new_or_existing_contact, legend: nil) do %>
20-
<% if gillick_competent %>
21-
<%= f.govuk_radio_button :new_or_existing_contact, "patient",
22-
label: { text: "Child (Gillick competent)" },
23-
link_errors: true %>
24-
<% end %>
25-
26-
<% if @parent_options.present? %>
27-
<% @parent_options.each.with_index do |parent_relationship, i| %>
28-
<% parent = parent_relationship.parent %>
29-
<%= f.govuk_radio_button :new_or_existing_contact, parent.id,
30-
label: { text: parent_relationship.label_with_parent },
31-
hint: { text: parent.contact_label },
32-
link_errors: !gillick_competent && i == 0 %>
33-
<% end %>
34-
35-
<%= f.govuk_radio_divider %>
36-
<% end %>
37-
38-
<%= f.govuk_radio_button :new_or_existing_contact, "new",
39-
label: { text: "Add a new parental contact" },
40-
link_errors: !gillick_competent && @parent_options.empty? %>
41-
<% end %>
11+
<%= f.govuk_collection_radio_buttons :new_or_existing_contact,
12+
@new_or_existing_contact_options,
13+
:value,
14+
:label,
15+
:hint,
16+
legend: { text: legend, tag: "h1", size: "l" },
17+
caption: { text: @patient.full_name, size: "l" } %>
4218

4319
<%= f.govuk_submit "Continue" %>
4420
<% end %>

0 commit comments

Comments
 (0)