Skip to content

Commit 8462fe9

Browse files
committed
Refactor SchoolsController
This refactors the controller and splits it up in to separate controllers for each of the various parts of the schools section of the service. This improves maintability as the class was getting quite big. Jira-Issue: MAV-3886
1 parent b19e780 commit 8462fe9

10 files changed

Lines changed: 121 additions & 106 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
class Schools::BaseController < ApplicationController
4+
before_action :set_location
5+
before_action :set_academic_year
6+
7+
layout "full"
8+
9+
private
10+
11+
def set_location
12+
urn_and_site = params[:school_urn_and_site]
13+
14+
@location =
15+
if urn_and_site.in?([Location::URN_UNKNOWN, Location::URN_HOME_EDUCATED])
16+
policy_scope(Location).generic_clinic.sole
17+
else
18+
policy_scope(Location).school.find_by_urn_and_site!(
19+
params[:school_urn_and_site]
20+
)
21+
end
22+
23+
authorize @location, policy_class: SchoolPolicy
24+
end
25+
26+
def set_academic_year
27+
@academic_year = AcademicYear.current
28+
end
29+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
class Schools::ImportController < Schools::BaseController
4+
def new
5+
draft_import = DraftImport.new(request_session: session, current_user:)
6+
7+
draft_import.clear_attributes
8+
draft_import.update!(location: @location, type: "class")
9+
10+
steps = draft_import.wizard_steps
11+
steps.delete(:type)
12+
steps.delete(:location)
13+
14+
redirect_to draft_import_path(I18n.t(steps.first, scope: :wicked))
15+
end
16+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
class Schools::PatientsController < Schools::BaseController
4+
include PatientSearchFormConcern
5+
6+
before_action :set_programme_statuses
7+
before_action :set_patient_search_form
8+
9+
def index
10+
scope =
11+
Patient
12+
.joins(:patient_locations)
13+
.where(
14+
patient_locations: {
15+
location: @location,
16+
academic_year: @academic_year
17+
}
18+
)
19+
.where(school_id: @location.school_id)
20+
.includes_statuses
21+
.includes(:clinic_notifications)
22+
23+
patients = @form.apply(scope)
24+
25+
@pagy, @patients = pagy(patients)
26+
end
27+
28+
private
29+
30+
def set_programme_statuses
31+
@programme_statuses =
32+
Patient::ProgrammeStatus.statuses.keys -
33+
%w[
34+
not_eligible
35+
needs_consent_request_not_scheduled
36+
needs_consent_request_scheduled
37+
needs_consent_request_failed
38+
needs_consent_follow_up_requested
39+
]
40+
end
41+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
class Schools::SessionsController < Schools::BaseController
4+
def index
5+
sessions =
6+
policy_scope(Session).for_academic_year(@academic_year).for_location(
7+
@location
8+
)
9+
10+
@patient_count_by_session_id =
11+
Patient
12+
.joins_sessions
13+
.joins_session_programme_year_groups
14+
.where("sessions.id = ANY(ARRAY[?]::bigint[])", sessions.pluck(:id))
15+
.group("sessions.id")
16+
.count("DISTINCT patients.id")
17+
18+
@unscheduled_sessions = sessions.unscheduled
19+
@scheduled_sessions = sessions.scheduled
20+
@completed_sessions = sessions.completed
21+
end
22+
end

app/controllers/schools_controller.rb

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22

33
class SchoolsController < ApplicationController
44
include LocationSearchFormConcern
5-
include PatientSearchFormConcern
65

7-
before_action :set_location_search_form, only: :index
8-
before_action :set_location, except: :index
9-
before_action :set_programme_statuses,
10-
:set_patient_search_form,
11-
only: :patients
6+
before_action :set_location_search_form
127

138
layout "full"
149

@@ -25,7 +20,12 @@ def index
2520
@patient_count_by_school_id =
2621
Patient
2722
.joins(:patient_locations)
28-
.where(patient_locations: { location: @locations, academic_year: })
23+
.where(
24+
patient_locations: {
25+
location: @locations,
26+
academic_year: AcademicYear.pending
27+
}
28+
)
2929
.distinct
3030
.group(:school_id)
3131
.count
@@ -37,84 +37,5 @@ def index
3737
.group("team_location.location_id")
3838
.where("date >= ?", Date.current)
3939
.minimum(:date)
40-
41-
render layout: "full"
42-
end
43-
44-
def import
45-
draft_import = DraftImport.new(request_session: session, current_user:)
46-
47-
draft_import.clear_attributes
48-
draft_import.update!(location: @location, type: "class")
49-
50-
steps = draft_import.wizard_steps
51-
steps.delete(:type)
52-
steps.delete(:location)
53-
54-
redirect_to draft_import_path(I18n.t(steps.first, scope: :wicked))
55-
end
56-
57-
def patients
58-
scope =
59-
Patient
60-
.joins(:patient_locations)
61-
.where(patient_locations: { location: @location, academic_year: })
62-
.where(school_id: @location.school_id)
63-
.includes_statuses
64-
.includes(:clinic_notifications)
65-
66-
patients = @form.apply(scope)
67-
68-
@pagy, @patients = pagy(patients)
69-
end
70-
71-
def sessions
72-
sessions =
73-
policy_scope(Session).for_academic_year(academic_year).for_location(
74-
@location
75-
)
76-
77-
@patient_count_by_session_id =
78-
Patient
79-
.joins_sessions
80-
.joins_session_programme_year_groups
81-
.where("sessions.id = ANY(ARRAY[?]::bigint[])", sessions.pluck(:id))
82-
.group("sessions.id")
83-
.count("DISTINCT patients.id")
84-
85-
@unscheduled_sessions = sessions.unscheduled
86-
@scheduled_sessions = sessions.scheduled
87-
@completed_sessions = sessions.completed
88-
end
89-
90-
private
91-
92-
def academic_year = AcademicYear.pending
93-
94-
def set_location
95-
urn_and_site = params[:school_urn_and_site]
96-
97-
@location =
98-
if urn_and_site.in?([Location::URN_UNKNOWN, Location::URN_HOME_EDUCATED])
99-
policy_scope(Location).generic_clinic.sole
100-
else
101-
policy_scope(Location).school.find_by_urn_and_site!(
102-
params[:school_urn_and_site]
103-
)
104-
end
105-
106-
authorize @location, policy_class: SchoolPolicy
107-
end
108-
109-
def set_programme_statuses
110-
@programme_statuses =
111-
Patient::ProgrammeStatus.statuses.keys -
112-
%w[
113-
not_eligible
114-
needs_consent_request_not_scheduled
115-
needs_consent_request_scheduled
116-
needs_consent_request_failed
117-
needs_consent_follow_up_requested
118-
]
11940
end
12041
end

app/policies/school_policy.rb

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

33
class SchoolPolicy < LocationPolicy
4-
def import? = team.has_point_of_care_access?
5-
6-
def patients? = team.has_point_of_care_access?
7-
8-
def sessions? = team.has_point_of_care_access?
9-
104
def new? = team.has_point_of_care_access?
115

126
def edit? = team.has_point_of_care_access?
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
]) %>
66
<% end %>
77

8-
<%= render "header" %>
8+
<%= render "schools/header" %>
99

1010
<% if @location.school? %>
1111
<%= govuk_button_link_to "Import class lists", school_import_path(@location), secondary: true %>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
]) %>
77
<% end %>
88

9-
<%= render "header" %>
9+
<%= render "schools/header" %>
1010

1111
<%= govuk_button_link_to "Add a new session", new_session_path(school_id: @location.id), secondary: true %>
1212

config/routes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@
263263
end
264264

265265
resources :schools, only: :index, param: :urn_and_site do
266-
get "import"
267-
get "patients"
268-
get "sessions"
266+
resources :import, only: :new, controller: "schools/import"
267+
resources :patients, only: :index, controller: "schools/patients"
268+
resources :sessions, only: :index, controller: "schools/sessions"
269269
end
270270

271271
resources :sessions, only: %i[index new show edit], param: :slug do

spec/policies/school_policy_spec.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
end
1212
let(:location) { create(:school) }
1313

14-
permissions :index?,
15-
:create?,
16-
:edit?,
17-
:import?,
18-
:new?,
19-
:patients?,
20-
:sessions?,
21-
:show?,
22-
:update? do
14+
permissions :index?, :create?, :edit?, :new?, :show?, :update? do
2315
it { should permit(point_of_care_user, location) }
2416
it { should_not permit(national_reporting_user, location) }
2517
end

0 commit comments

Comments
 (0)