Skip to content

Commit b5f5a71

Browse files
committed
Create ConsentRefusalFollowUpForm
Use a form class for the follow-up journey on the nurse/SAIS side. This means validations no longer happen in the controller. Uses session persistence so decisions from previous steps in the journey are retained.
1 parent 364c0b8 commit b5f5a71

9 files changed

Lines changed: 198 additions & 43 deletions

File tree

app/controllers/patient_sessions/consents_controller.rb

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
class PatientSessions::ConsentsController < PatientSessions::BaseController
44
before_action :set_consent, except: %i[create send_request]
5+
before_action :set_consent_follow_up_form,
6+
only: %i[edit_follow_up update_follow_up]
7+
before_action :set_consent_confirm_refusal_form,
8+
only: %i[edit_confirm_refusal update_confirm_refusal]
59
before_action :ensure_can_follow_up,
610
only: %i[
711
edit_follow_up
@@ -63,25 +67,29 @@ def edit_follow_up
6367
end
6468

6569
def update_follow_up
66-
if follow_up_params[:decision_stands].nil?
67-
@consent.errors.add(:decision_stands, :blank)
68-
render :follow_up, status: :unprocessable_content
69-
elsif follow_up_params[:decision_stands] == "true"
70-
redirect_to confirm_refusal_session_patient_programme_consent_path
71-
else
72-
@draft_consent = DraftConsent.new(request_session: session, current_user:)
73-
@draft_consent.clear_attributes
74-
@draft_consent.assign_attributes(create_params)
75-
@draft_consent.follow_up_consent_id = @consent.id
76-
@draft_consent.follow_up_flow = true
77-
@draft_consent.new_or_existing_contact = @consent.parent_id.to_s
78-
@draft_consent.route = "phone"
79-
80-
if @draft_consent.save
81-
redirect_to draft_consent_path("agree")
70+
@form.assign_attributes(follow_up_params)
71+
72+
if @form.valid?
73+
if @form.decision_stands?
74+
redirect_to confirm_refusal_session_patient_programme_consent_path
8275
else
83-
render :follow_up, status: :unprocessable_content
76+
@draft_consent =
77+
DraftConsent.new(request_session: session, current_user:)
78+
@draft_consent.clear_attributes
79+
@draft_consent.assign_attributes(create_params)
80+
@draft_consent.follow_up_consent_id = @consent.id
81+
@draft_consent.follow_up_flow = true
82+
@draft_consent.new_or_existing_contact = @consent.parent_id.to_s
83+
@draft_consent.route = "phone"
84+
85+
if @draft_consent.save
86+
redirect_to draft_consent_path("agree")
87+
else
88+
render :follow_up, status: :unprocessable_content
89+
end
8490
end
91+
else
92+
render :follow_up, status: :unprocessable_content
8593
end
8694
end
8795

@@ -90,29 +98,30 @@ def edit_confirm_refusal
9098
end
9199

92100
def update_confirm_refusal
93-
if confirm_refusal_params[:confirmed].nil?
94-
@consent.errors.add(:confirmed, :blank)
95-
render :confirm_refusal, status: :unprocessable_content
96-
elsif confirm_refusal_params[:confirmed] == "true"
97-
ActiveRecord::Base.transaction do
101+
@form.assign_attributes(confirm_refusal_params)
102+
103+
if @form.valid?
104+
if @form.confirmed?
98105
@consent.resolve_follow_up!(
99106
outcome: :confirmed,
100107
notes: confirm_refusal_params[:notes].to_s
101108
)
102-
end
103109

104-
@consent.notifier.send_confirmation(
105-
session: @session,
106-
triage: nil,
107-
sent_by: current_user
108-
)
110+
@consent.notifier.send_confirmation(
111+
session: @session,
112+
triage: nil,
113+
sent_by: current_user
114+
)
109115

110-
redirect_to session_patient_programme_consent_path,
111-
flash: {
112-
success: "Consent from #{@consent.name} updated."
113-
}
116+
redirect_to session_patient_programme_consent_path,
117+
flash: {
118+
success: "Consent from #{@consent.name} updated."
119+
}
120+
else
121+
redirect_to session_patient_programme_consent_path
122+
end
114123
else
115-
redirect_to session_patient_programme_consent_path
124+
render :confirm_refusal, status: :unprocessable_content
116125
end
117126
end
118127

@@ -173,6 +182,14 @@ def set_consent
173182
.find(params[:id])
174183
end
175184

185+
def set_consent_follow_up_form
186+
@form = ConsentFollowUpForm.new
187+
end
188+
189+
def set_consent_confirm_refusal_form
190+
@form = ConsentConfirmRefusalForm.new
191+
end
192+
176193
def update_patient_status
177194
@consent.invalidate_all_triages_and_patient_specific_directions!
178195

@@ -201,11 +218,11 @@ def create_params
201218
end
202219

203220
def follow_up_params
204-
params.permit(consent: :decision_stands)[:consent] || {}
221+
params.fetch(:consent_follow_up_form, {}).permit(:decision_stands)
205222
end
206223

207224
def confirm_refusal_params
208-
params.permit(consent: %i[confirmed notes])[:consent] || {}
225+
params.fetch(:consent_confirm_refusal_form, {}).permit(:confirmed, :notes)
209226
end
210227

211228
def withdraw_params
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
class ConsentConfirmRefusalForm
4+
include ActiveModel::Model
5+
include ActiveModel::Attributes
6+
7+
attribute :confirmed, :string
8+
attribute :notes, :string
9+
10+
validates :confirmed,
11+
inclusion: {
12+
in: %w[true false],
13+
message: "Select yes or no"
14+
}
15+
validates :notes, length: { maximum: 1000 }
16+
17+
def confirmed? = confirmed == "true"
18+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
class ConsentFollowUpForm
4+
include ActiveModel::Model
5+
include ActiveModel::Attributes
6+
7+
attribute :decision_stands, :string
8+
9+
validates :decision_stands,
10+
inclusion: {
11+
in: %w[true false],
12+
message: "Select yes or no"
13+
}
14+
15+
def decision_stands? = decision_stands == "true"
16+
end

app/models/consent.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class Consent < ApplicationRecord
6666
include Notable
6767
include Refusable
6868

69-
attr_accessor :confirmed, :decision_stands
70-
7169
audited associated_with: :patient
7270

7371
belongs_to :patient

app/views/patient_sessions/consents/confirm_refusal.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<%= govuk_back_link(href: follow_up_session_patient_programme_consent_path) %>
33
<% end %>
44

5-
<%= form_with model: @consent, url: confirm_refusal_session_patient_programme_consent_path, method: :post do |f| %>
5+
<%= form_with model: @form, url: confirm_refusal_session_patient_programme_consent_path, method: :post do |f| %>
66
<%= f.mavis_error_summary %>
77

88
<% page_title = "Update consent response" %>

app/views/patient_sessions/consents/follow_up.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<%= govuk_back_link(href: session_patient_programme_consent_path) %>
33
<% end %>
44

5-
<%= form_with model: @consent, url: follow_up_session_patient_programme_consent_path, method: :post do |f| %>
5+
<%= form_with model: @form, url: follow_up_session_patient_programme_consent_path, method: :post do |f| %>
66
<%= f.mavis_error_summary %>
77

88
<% page_title = "Follow up refusal" %>

config/locales/en.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ en:
259259
Choose which nurse identified and pre-screened the child and supplied the vaccine
260260
vaccine_method:
261261
inclusion: Choose if they are ready to vaccinate
262+
consent_refusal_follow_up_form:
263+
attributes:
264+
decision_stands:
265+
inclusion: Select yes or no
266+
confirmed:
267+
inclusion: Select yes or no
268+
notes:
269+
too_long: Enter notes that are less than %{count} characters long
262270
vaccination_report:
263271
attributes:
264272
academic_year:
@@ -447,10 +455,6 @@ en:
447455
blank: Choose a file
448456
consent:
449457
attributes:
450-
confirmed:
451-
blank: Select yes or no
452-
decision_stands:
453-
blank: Select yes or no
454458
notes:
455459
blank: Enter notes
456460
too_long: Enter notes that are less than %{count} characters long
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
describe ConsentConfirmRefusalForm do
4+
subject(:form) { described_class.new }
5+
6+
describe "validations" do
7+
it "is invalid without confirmed" do
8+
expect(form).not_to be_valid
9+
end
10+
11+
context "with confirmed set to true" do
12+
before { form.confirmed = "true" }
13+
14+
it { should be_valid }
15+
end
16+
17+
context "with confirmed set to false" do
18+
before { form.confirmed = "false" }
19+
20+
it { should be_valid }
21+
end
22+
23+
context "with notes exceeding 1000 characters" do
24+
before do
25+
form.confirmed = "true"
26+
form.notes = "a" * 1001
27+
end
28+
29+
it { should_not be_valid }
30+
end
31+
32+
context "with notes at exactly 1000 characters" do
33+
before do
34+
form.confirmed = "true"
35+
form.notes = "a" * 1000
36+
end
37+
38+
it { should be_valid }
39+
end
40+
end
41+
42+
describe "#confirmed?" do
43+
it "returns true when confirmed is 'true'" do
44+
form.confirmed = "true"
45+
expect(form.confirmed?).to be true
46+
end
47+
48+
it "returns false when confirmed is 'false'" do
49+
form.confirmed = "false"
50+
expect(form.confirmed?).to be false
51+
end
52+
53+
it "returns false when confirmed is nil" do
54+
expect(form.confirmed?).to be false
55+
end
56+
end
57+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
describe ConsentFollowUpForm do
4+
subject(:form) { described_class.new }
5+
6+
describe "validations" do
7+
it "is invalid without decision_stands" do
8+
expect(form).not_to be_valid
9+
end
10+
11+
context "with decision_stands set to true" do
12+
before { form.decision_stands = "true" }
13+
14+
it { should be_valid }
15+
end
16+
17+
context "with decision_stands set to false" do
18+
before { form.decision_stands = "false" }
19+
20+
it { should be_valid }
21+
end
22+
23+
context "with an invalid decision_stands value" do
24+
before { form.decision_stands = "maybe" }
25+
26+
it { should_not be_valid }
27+
end
28+
end
29+
30+
describe "#decision_stands?" do
31+
it "returns true when decision_stands is 'true'" do
32+
form.decision_stands = "true"
33+
expect(form.decision_stands?).to be true
34+
end
35+
36+
it "returns false when decision_stands is 'false'" do
37+
form.decision_stands = "false"
38+
expect(form.decision_stands?).to be false
39+
end
40+
41+
it "returns false when decision_stands is nil" do
42+
expect(form.decision_stands?).to be false
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)