@@ -4,7 +4,7 @@ class VaccinateForm
44 include ActiveModel ::Model
55 include ActiveModel ::Attributes
66
7- attr_accessor :patient_session , :current_user , :todays_batch
7+ attr_accessor :patient_session , :programme , : current_user, :todays_batch
88
99 attribute :knows_vaccination , :boolean
1010 attribute :not_already_had , :boolean
@@ -18,18 +18,23 @@ class VaccinateForm
1818 attribute :delivery_method , :string
1919 attribute :delivery_site , :string
2020 attribute :dose_sequence , :integer
21- attribute :programme_id , :integer
2221 attribute :vaccine_id , :integer
2322
24- validates :knows_vaccination , inclusion : { in : [ true , nil ] }
25- validates :not_already_had , inclusion : { in : [ true , nil ] }
26- validates :feeling_well , inclusion : { in : [ true , nil ] }
27- validates :no_allergies , inclusion : { in : [ true , nil ] }
28- validates :not_taking_medication , inclusion : { in : [ true , nil ] }
29- validates :not_pregnant , inclusion : { in : [ true , nil ] }
23+ validates :administered , inclusion : [ true , false ]
3024
31- validate :valid_administered_values
32- validates :programme_id , presence : true
25+ with_options if : :administered do
26+ validates :knows_vaccination , presence : true
27+ validates :not_already_had , presence : true
28+ validates :no_allergies , presence : true
29+ end
30+
31+ with_options if : -> { administered && ask_not_taking_medication? } do
32+ validates :not_taking_medication , presence : true
33+ end
34+
35+ with_options if : -> { administered && ask_not_pregnant? } do
36+ validates :not_pregnant , presence : true
37+ end
3338
3439 with_options if : :administered do
3540 validates :delivery_method , presence : true
@@ -59,13 +64,21 @@ def save(draft_vaccination_record:)
5964 draft_vaccination_record . performed_at = Time . current
6065 draft_vaccination_record . performed_by_user = current_user
6166 draft_vaccination_record . performed_ods_code = organisation . ods_code
62- draft_vaccination_record . programme_id = programme_id
67+ draft_vaccination_record . programme = programme
6368 draft_vaccination_record . session_id = patient_session . session_id
6469 draft_vaccination_record . vaccine_id = vaccine_id
6570
6671 draft_vaccination_record . save # rubocop:disable Rails/SaveBang
6772 end
6873
74+ def ask_not_taking_medication?
75+ programme . doubles?
76+ end
77+
78+ def ask_not_pregnant?
79+ programme . hpv? || programme . td_ipv?
80+ end
81+
6982 private
7083
7184 delegate :organisation , to : :patient_session
@@ -81,25 +94,12 @@ def pre_screening
8194 not_taking_medication : not_taking_medication || false ,
8295 notes : pre_screening_notes ,
8396 performed_by : current_user ,
84- programme_id :,
97+ programme :,
8598 session_date_id :
8699 )
87100 end
88101
89102 def session_date_id
90103 patient_session . session . session_dates . today . first &.id
91104 end
92-
93- def valid_administered_values
94- if administered . nil?
95- errors . add ( :administered , "Choose if they are ready to vaccinate" )
96- end
97-
98- vaccination_allowed =
99- pre_screening . invalid? || pre_screening . allows_vaccination?
100-
101- if administered && !vaccination_allowed
102- errors . add ( :administered , "Patient should not be vaccinated" )
103- end
104- end
105105end
0 commit comments