Skip to content

Commit b4e7504

Browse files
committed
Refactor AppPatientSessionSearchResultCardComponent
This refactors the component to accept a separate patient and session in preparation for the removal of the `PatientSession` model. Jira-Issue: MAV-1822
1 parent 5c9cc58 commit b4e7504

File tree

15 files changed

+58
-44
lines changed

15 files changed

+58
-44
lines changed

app/components/app_patient_session_search_result_card_component.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class AppPatientSessionSearchResultCardComponent < ViewComponent::Base
4242
end
4343
end
4444
45-
if context != :patient_specific_direction && (note = patient_session.latest_note)
45+
if context != :patient_specific_direction && latest_note
4646
summary_list.with_row do |row|
4747
row.with_key { "Notes" }
48-
row.with_value { render note_to_log_event(note) }
48+
row.with_value { render note_to_log_event(latest_note) }
4949
end
5050
end
5151
end %>
@@ -59,7 +59,7 @@ class AppPatientSessionSearchResultCardComponent < ViewComponent::Base
5959
<% end %>
6060
ERB
6161

62-
def initialize(patient_session, context:, programmes: [])
62+
def initialize(patient:, session:, context:, programmes: [])
6363
unless context.in?(
6464
%i[
6565
patients
@@ -73,10 +73,8 @@ def initialize(patient_session, context:, programmes: [])
7373
raise "Unknown context: #{context}"
7474
end
7575

76-
@patient_session = patient_session
77-
@patient = patient_session.patient
78-
@session = patient_session.session
79-
76+
@patient = patient
77+
@session = session
8078
@context = context
8179

8280
@programmes =
@@ -89,7 +87,7 @@ def initialize(patient_session, context:, programmes: [])
8987

9088
private
9189

92-
attr_reader :patient_session, :patient, :session, :context, :programmes
90+
attr_reader :patient, :session, :context, :programmes
9391

9492
delegate :govuk_button_to,
9593
:govuk_summary_list,
@@ -283,6 +281,14 @@ def patient_specific_direction_status_tag
283281
}
284282
end
285283

284+
def latest_note
285+
patient
286+
.notes
287+
.sort_by(&:created_at)
288+
.reverse
289+
.find { it.session_id == session.id }
290+
end
291+
286292
def note_to_log_event(note)
287293
truncated_body = note.body.truncate_words(80, omission: "…")
288294

app/controllers/sessions/consent_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def show
2929
@session
3030
.patient_sessions
3131
.includes_programmes
32-
.includes(:latest_note, patient: :consent_statuses)
32+
.includes(patient: [:consent_statuses, { notes: :created_by }])
3333
.has_consent_status(
3434
statuses_except_not_required,
3535
programme: @form.programmes

app/controllers/sessions/patients_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ def show
1313

1414
scope =
1515
@session.patient_sessions.includes_programmes.includes(
16-
:latest_note,
17-
patient: :vaccination_statuses
16+
patient: [:vaccination_statuses, { notes: :created_by }]
1817
)
1918

2019
patient_sessions = @form.apply(scope)

app/controllers/sessions/record_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ class Sessions::RecordController < ApplicationController
1717
def show
1818
scope =
1919
@session.patient_sessions.includes(
20-
:latest_note,
21-
patient: %i[consent_statuses triage_statuses vaccination_statuses]
20+
patient: [
21+
:consent_statuses,
22+
:triage_statuses,
23+
:vaccination_statuses,
24+
{ notes: :created_by }
25+
]
2226
)
2327

2428
if @session.requires_registration?

app/controllers/sessions/register_controller.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def show
1515

1616
scope =
1717
@session.patient_sessions.includes_programmes.includes(
18-
:latest_note,
19-
patient: %i[
20-
consent_statuses
21-
registration_statuses
22-
triage_statuses
23-
vaccination_statuses
18+
patient: [
19+
:consent_statuses,
20+
:registration_statuses,
21+
:triage_statuses,
22+
:vaccination_statuses,
23+
{ notes: :created_by }
2424
]
2525
)
2626

app/controllers/sessions/triage_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def show
1515
@session
1616
.patient_sessions
1717
.includes_programmes
18-
.includes(:latest_note, patient: :triage_statuses)
18+
.includes(patient: [:triage_statuses, { notes: :created_by }])
1919
.has_triage_status(@statuses, programme: @form.programmes)
2020

2121
patient_sessions = @form.apply(scope)

app/models/patient_session.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ class PatientSession < ApplicationRecord
5353
-> { where(patient_id: it.patient_id) },
5454
through: :session
5555

56-
has_many :notes, -> { where(session_id: it.session_id) }, through: :patient
57-
58-
has_one :latest_note,
59-
-> { where(session_id: it.session_id).order(created_at: :desc) },
60-
through: :patient,
61-
source: :notes
62-
6356
has_many :pre_screenings,
6457
-> { where(patient_id: it.patient_id) },
6558
through: :session

app/views/sessions/consent/show.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
<%= render AppSearchResultsComponent.new(@pagy, label: "children") do %>
2525
<% @patient_sessions.each do |patient_session| %>
2626
<%= render AppPatientSessionSearchResultCardComponent.new(
27-
patient_session, context: :consent, programmes: @form.programmes,
27+
patient: patient_session.patient,
28+
session: patient_session.session,
29+
context: :consent,
30+
programmes: @form.programmes,
2831
) %>
2932
<% end %>
3033
<% end %>

app/views/sessions/patient_specific_directions/show.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
<%= render AppSessionNeedsReviewWarningComponent.new(session: @session) %>
4040
<%= render AppSearchResultsComponent.new(@pagy, label: "children", heading: "Review PSDs") do %>
4141
<% @patient_sessions.each do |patient_session| %>
42-
<%= render AppPatientSessionSearchResultCardComponent.new(patient_session, context: :patient_specific_direction) %>
42+
<%= render AppPatientSessionSearchResultCardComponent.new(
43+
patient: patient_session.patient,
44+
session: patient_session.session,
45+
context: :patient_specific_direction,
46+
) %>
4347
<% end %>
4448
<% end %>
4549
</div>

app/views/sessions/patients/show.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
<%= render AppSearchResultsComponent.new(@pagy, label: "children") do %>
2525
<% @patient_sessions.each do |patient_session| %>
2626
<%= render AppPatientSessionSearchResultCardComponent.new(
27-
patient_session, context: :patients, programmes: @form.programmes,
27+
patient: patient_session.patient,
28+
session: patient_session.session,
29+
context: :patients,
30+
programmes: @form.programmes,
2831
) %>
2932
<% end %>
3033
<% end %>

0 commit comments

Comments
 (0)