Skip to content

Commit 867d46f

Browse files
committed
Add validation on vaccine_method
This adds some validation on the `vaccine_method` column of the `Consent` model to ensure that it's always set when the parent has given consent. This value will be used as part of generating the overall consent status so it's important that it's always present. We can't validate that the `vaccine_method` is not set when refused consent because it's possible for a parent to give consent and then withdrawn it, in which case we would want to see what the method was when consent was given. Jira-Issue: MAV-1230
1 parent 01b51df commit 867d46f

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

app/models/consent.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ class Consent < ApplicationRecord
7070
{ website: 0, phone: 1, paper: 2, in_person: 3, self_consent: 4 },
7171
prefix: "via",
7272
validate: true
73-
enum :vaccine_method, { injection: 0, nasal: 1 }, prefix: true
73+
enum :vaccine_method,
74+
{ injection: 0, nasal: 1 },
75+
prefix: true,
76+
validate: {
77+
if: :response_given?
78+
}
7479

7580
enum :reason_for_refusal,
7681
{

spec/factories/consent_form_programmes.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
consent_form
2626
programme
2727

28-
traits_for_enum :response
28+
trait :given do
29+
response { "given" }
30+
vaccine_method { "injection" }
31+
end
32+
33+
trait :refused do
34+
response { "refused" }
35+
vaccine_method { nil }
36+
end
2937
end
3038
end

spec/factories/consent_forms.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@
123123
end
124124

125125
after(:create) do |consent_form, evaluator|
126+
vaccine_method = evaluator.response == "given" ? "injection" : nil
127+
126128
consent_form.consent_form_programmes.update_all(
127-
response: evaluator.response
129+
response: evaluator.response,
130+
vaccine_method:
128131
)
129132
end
130133

spec/factories/consents.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
end
6666

6767
response { "given" }
68+
vaccine_method { "injection" }
6869
route { "website" }
6970

7071
health_answers do
@@ -75,8 +76,6 @@
7576

7677
submitted_at { consent_form&.recorded_at || Time.current }
7778

78-
traits_for_enum :response
79-
8079
trait :given_verbally do
8180
given
8281
route { "phone" }
@@ -94,10 +93,16 @@
9493
end
9594

9695
trait :refused do
97-
response { :refused }
98-
reason_for_refusal { :personal_choice }
96+
response { "refused" }
97+
reason_for_refusal { "personal_choice" }
9998
health_answers { [] }
10099
notes { "Refused." }
100+
vaccine_method { nil }
101+
end
102+
103+
trait :not_provided do
104+
response { "not_provided" }
105+
vaccine_method { nil }
101106
end
102107

103108
trait :from_mum do

spec/models/consent_spec.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@
4343
describe Consent do
4444
describe "validations" do
4545
it { should validate_length_of(:notes).is_at_most(1000) }
46+
47+
context "when response is given" do
48+
subject(:consent) { build(:consent, :given) }
49+
50+
it do
51+
expect(consent).to validate_inclusion_of(:vaccine_method).in_array(
52+
%w[injection nasal]
53+
)
54+
end
55+
end
56+
57+
context "when response is refused" do
58+
subject(:consent) { build(:consent, :refused) }
59+
60+
it do
61+
expect(consent).not_to validate_inclusion_of(:vaccine_method).in_array(
62+
%w[injection nasal]
63+
)
64+
end
65+
end
4666
end
4767

4868
describe "#verbal_routes" do
@@ -202,8 +222,14 @@
202222
end
203223

204224
before do
205-
consent_form.consent_form_programmes.first.update!(response: "given")
206-
consent_form.consent_form_programmes.second.update!(response: "refused")
225+
consent_form.consent_form_programmes.first.update!(
226+
response: "given",
227+
vaccine_method: "injection"
228+
)
229+
consent_form.consent_form_programmes.second.update!(
230+
response: "refused",
231+
vaccine_method: nil
232+
)
207233
end
208234

209235
it "creates a consent per programme" do

0 commit comments

Comments
 (0)