Skip to content

Commit 74d30f6

Browse files
authored
Merge pull request #3658 from nhsuk/refactor-patient-session-controllers
Refactor patient session controllers
2 parents e0552f5 + b40a324 commit 74d30f6

20 files changed

+121
-139
lines changed

app/components/app_patient_page_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
destroy_programme_vaccination_record_path(
118118
programme,
119119
vaccination_record,
120-
return_to: session_patient_programme_path(patient_id: patient.id),
120+
return_to: session_patient_programme_path(session, patient, programme),
121121
) %>
122122
<% end %>
123123
</div>

app/controllers/concerns/patient_session_programme_concern.rb

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class PatientSessions::ActivitiesController < PatientSessions::BaseController
4+
before_action :record_access_log_entry, only: :show
5+
6+
def show
7+
end
8+
9+
private
10+
11+
def access_log_entry_action = "log"
12+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
class PatientSessions::BaseController < ApplicationController
4+
before_action :set_session
5+
before_action :set_patient
6+
before_action :set_patient_session
7+
before_action :set_programme
8+
before_action :set_breadcrumb_item
9+
10+
layout "three_quarters"
11+
12+
private
13+
14+
def set_session
15+
@session =
16+
policy_scope(Session).includes(:location, :programmes).find_by!(
17+
slug: params.fetch(:session_slug, params[:slug])
18+
)
19+
end
20+
21+
def set_patient
22+
@patient =
23+
policy_scope(Patient).includes(parent_relationships: :parent).find(
24+
params.fetch(:patient_id, params[:id])
25+
)
26+
end
27+
28+
def set_patient_session
29+
@patient_session =
30+
PatientSession.find_by!(patient_id: @patient.id, session_id: @session.id)
31+
32+
# Assigned to already loaded objects
33+
@patient_session.patient = @patient
34+
@patient_session.session = @session
35+
end
36+
37+
def set_programme
38+
return unless params.key?(:programme_type) || params.key?(:type)
39+
40+
@programme =
41+
@patient_session.programmes.find do |programme|
42+
programme.type == params[:programme_type] ||
43+
programme.type == params[:type]
44+
end
45+
46+
raise ActiveRecord::RecordNotFound if @programme.nil?
47+
end
48+
49+
def set_breadcrumb_item
50+
return_to = params[:return_to]
51+
return nil if return_to.blank?
52+
53+
known_return_to = %w[consent triage register record outcome]
54+
return unless return_to.in?(known_return_to)
55+
56+
@breadcrumb_item = {
57+
text: t(return_to, scope: %i[sessions tabs]),
58+
href: send(:"session_#{return_to}_path")
59+
}
60+
end
61+
62+
def record_access_log_entry
63+
@patient.access_log_entries.create!(
64+
user: current_user,
65+
controller: "patient_sessions",
66+
action: access_log_entry_action
67+
)
68+
end
69+
end

app/controllers/consents_controller.rb renamed to app/controllers/patient_sessions/consents_controller.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

3-
class ConsentsController < ApplicationController
4-
include PatientSessionProgrammeConcern
5-
3+
class PatientSessions::ConsentsController < PatientSessions::BaseController
64
before_action :set_consent, except: %i[create send_request]
75
before_action :ensure_can_withdraw, only: %i[edit_withdraw update_withdraw]
86
before_action :ensure_can_invalidate,
@@ -19,7 +17,7 @@ def create
1917
if @draft_consent.save
2018
redirect_to draft_consent_path(Wicked::FIRST_STEP)
2119
else
22-
render "patient_sessions/show", status: :unprocessable_entity
20+
render "patient_sessions/programmes/show", status: :unprocessable_entity
2321
end
2422
end
2523

app/controllers/gillick_assessments_controller.rb renamed to app/controllers/patient_sessions/gillick_assessments_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

3-
class GillickAssessmentsController < ApplicationController
4-
include PatientSessionProgrammeConcern
5-
3+
class PatientSessions::GillickAssessmentsController < PatientSessions::BaseController
64
before_action :set_gillick_assessment
75

86
def edit
Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
# frozen_string_literal: true
22

3-
class PatientSessionsController < ApplicationController
4-
include PatientSessionProgrammeConcern
5-
6-
before_action :set_breadcrumb_item
7-
8-
before_action :record_access_log_entry, except: :record_already_vaccinated
9-
10-
layout "three_quarters"
3+
class PatientSessions::ProgrammesController < PatientSessions::BaseController
4+
before_action :record_access_log_entry, only: :show
115

126
def show
137
end
148

15-
def log
16-
end
17-
189
def record_already_vaccinated
1910
unless @patient_session.can_record_as_already_vaccinated?(
2011
programme: @programme
@@ -42,24 +33,5 @@ def record_already_vaccinated
4233

4334
private
4435

45-
def set_breadcrumb_item
46-
return_to = params[:return_to]
47-
return nil if return_to.blank?
48-
49-
known_return_to = %w[consent triage register record outcome]
50-
return unless return_to.in?(known_return_to)
51-
52-
@breadcrumb_item = {
53-
text: t(return_to, scope: %i[sessions tabs]),
54-
href: send(:"session_#{return_to}_path")
55-
}
56-
end
57-
58-
def record_access_log_entry
59-
@patient.access_log_entries.create!(
60-
user: current_user,
61-
controller: "patient_sessions",
62-
action: action_name
63-
)
64-
end
36+
def access_log_entry_action = :show
6537
end

app/controllers/session_attendances_controller.rb renamed to app/controllers/patient_sessions/session_attendances_controller.rb

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
# frozen_string_literal: true
22

3-
class SessionAttendancesController < ApplicationController
4-
before_action :set_session
5-
before_action :set_patient
6-
before_action :set_patient_session
3+
class PatientSessions::SessionAttendancesController < PatientSessions::BaseController
74
before_action :set_session_date
85
before_action :set_session_attendance
96

10-
layout "three_quarters"
11-
127
def edit
138
end
149

@@ -35,8 +30,9 @@ def update
3530
end
3631

3732
redirect_to session_patient_programme_path(
38-
patient_id: @patient.id,
39-
programme_type: @patient_session.programmes.first.type
33+
@session,
34+
@patient,
35+
@patient_session.programmes.first
4036
)
4137
else
4238
render :edit, status: :unprocessable_entity
@@ -45,19 +41,6 @@ def update
4541

4642
private
4743

48-
def set_session
49-
@session = policy_scope(Session).find_by!(slug: params[:session_slug])
50-
end
51-
52-
def set_patient
53-
@patient = policy_scope(Patient).find(params[:patient_id])
54-
end
55-
56-
def set_patient_session
57-
@patient_session =
58-
PatientSession.find_by!(patient: @patient, session: @session)
59-
end
60-
6144
def set_session_date
6245
@session_date = @session.session_dates.find_by!(value: Date.current)
6346
end

app/controllers/triages_controller.rb renamed to app/controllers/patient_sessions/triages_controller.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
# frozen_string_literal: true
22

3-
class TriagesController < ApplicationController
4-
include PatientSessionProgrammeConcern
3+
class PatientSessions::TriagesController < PatientSessions::BaseController
54
include TriageMailerConcern
65

7-
before_action :set_session
8-
before_action :set_patient
9-
before_action :set_patient_session
10-
before_action :set_programme
116
before_action :set_triage
127

138
after_action :verify_authorized
@@ -32,12 +27,12 @@ def create
3227
heading: "Triage outcome updated for",
3328
heading_link_text: @patient.full_name,
3429
heading_link_href:
35-
session_patient_programme_path(patient_id: @patient.id)
30+
session_patient_programme_path(@session, @patient, @programme)
3631
}
3732

3833
redirect_to redirect_path
3934
else
40-
render "patient_sessions/show", status: :unprocessable_entity
35+
render "patient_sessions/programmes/show", status: :unprocessable_entity
4136
end
4237
end
4338

app/controllers/vaccinations_controller.rb renamed to app/controllers/patient_sessions/vaccinations_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

3-
class VaccinationsController < ApplicationController
4-
include PatientSessionProgrammeConcern
3+
class PatientSessions::VaccinationsController < PatientSessions::BaseController
54
include TodaysBatchConcern
65
include VaccinationMailerConcern
76

@@ -42,7 +41,7 @@ def create
4241
I18n.t(steps.first, scope: :wicked)
4342
)
4443
else
45-
render "patient_sessions/show", status: :unprocessable_entity
44+
render "patient_sessions/programmes/show", status: :unprocessable_entity
4645
end
4746
end
4847

0 commit comments

Comments
 (0)