Skip to content

Commit 2269452

Browse files
Refactor: extract patient attendance validation
This validation is done on both draft and full vaccination record so it would be better to extract it.
1 parent 5e350ad commit 2269452

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module PatientAttendanceValidation
4+
extend ActiveSupport::Concern
5+
6+
private
7+
8+
def patient_attended_session
9+
return if session.nil? || patient.nil?
10+
11+
unless patient.attending?(session:) || patient.completed?(session:)
12+
date = performed_at.to_date
13+
errors.add(:session_id, :patient_not_attending,
14+
patient_full_name: patient.full_name,
15+
display_date: date.today? ? "Today (#{date.to_fs(:long)})" : date.to_fs(:long),
16+
location_name: location[:name]
17+
)
18+
end
19+
end
20+
end

app/models/draft_vaccination_record.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class DraftVaccinationRecord
44
include RequestSessionPersistable
55
include EditableWrapper
66
include HasDoseVolume
7+
include PatientAttendanceValidation
78
include VaccinationRecordPerformedByConcern
89
include WizardStepConcern
910

@@ -213,16 +214,5 @@ def can_change_outcome?
213214
outcome != "already_had" || editing? || session.nil? || session.today?
214215
end
215216

216-
def patient_attended_session
217-
return if patient.nil? || session.nil?
218217

219-
unless patient.attending?(session:) || patient.completed?(session:)
220-
date = performed_at.to_date
221-
errors.add(:session_id,:patient_not_attending,
222-
patient_full_name: patient.full_name,
223-
display_date: date.today? ? "Today (#{date.to_fs(:long)})" : date.to_fs(:long),
224-
location_name: location[:name]
225-
)
226-
end
227-
end
228218
end

app/models/vaccination_record.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
class VaccinationRecord < ApplicationRecord
5353
include Discard::Model
5454
include HasDoseVolume
55+
include PatientAttendanceValidation
5556
include PendingChangesConcern
5657
include VaccinationRecordPerformedByConcern
5758

@@ -186,18 +187,7 @@ def requires_location_name?
186187
session.nil? || location&.generic_clinic?
187188
end
188189

189-
def patient_attended_session
190-
return if session.nil? || patient.nil?
191-
192-
unless patient.attending?(session:) || patient.completed?(session:)
193-
date = performed_at.to_date
194-
errors.add(:session_id,:patient_not_attending,
195-
patient_full_name: patient.full_name,
196-
display_date: date.today? ? "Today (#{date.to_fs(:long)})" : date.to_fs(:long),
197-
location_name: location[:name]
198-
)
199-
end
200-
end
190+
201191

202192
delegate :maximum_dose_sequence, to: :programme
203193
end

0 commit comments

Comments
 (0)