Skip to content

Commit 28e4a15

Browse files
committed
Add Programmes::SessionsController
This encapsulates logic related to displaying the sessions tab when viewing a programme to reduce the size of the existing ProgrammesController and make the code easier to maintain. By making the controllers consistent we're also able to refactor `AppProgrammeNavigationComponent` to simplify the logic and reduce duplication.
1 parent 4e4ed8a commit 28e4a15

6 files changed

Lines changed: 42 additions & 46 deletions

File tree

app/components/app_programme_navigation_component.rb

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,19 @@ def call
1616
selected: active == :overview
1717
)
1818

19-
nav.with_item(
20-
href: programme_cohorts_path(programme),
21-
text: I18n.t("programmes.cohorts.index.title"),
22-
selected: active == :cohorts
23-
)
24-
25-
nav.with_item(
26-
href: sessions_programme_path(programme),
27-
text: I18n.t("sessions.index.title"),
28-
selected: active == :sessions
29-
)
30-
31-
nav.with_item(
32-
href: programme_patients_path(programme),
33-
text: I18n.t("programmes.patients.index.title"),
34-
selected: active == :patients
35-
)
36-
37-
nav.with_item(
38-
href: programme_vaccinations_path(programme),
39-
text: I18n.t("programmes.vaccinations.index.title"),
40-
selected: active == :vaccinations
41-
)
19+
SECTIONS.each do |section|
20+
nav.with_item(
21+
href: public_send("programme_#{section}_path", programme),
22+
text: I18n.t("title", scope: [:programmes, section, :index]),
23+
selected: active == session
24+
)
25+
end
4226
end
4327
end
4428

4529
private
4630

4731
attr_reader :programme, :active
32+
33+
SECTIONS = %i[cohorts sessions patients vaccinations].freeze
4834
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 Programmes::SessionsController < ApplicationController
4+
before_action :set_programme
5+
6+
layout "full"
7+
8+
def index
9+
@sessions =
10+
policy_scope(Session)
11+
.has_programme(@programme)
12+
.for_current_academic_year
13+
.includes(:location, :session_dates)
14+
.order("locations.name")
15+
end
16+
17+
private
18+
19+
def set_programme
20+
@programme = policy_scope(Programme).find_by!(type: params[:programme_type])
21+
end
22+
end

app/controllers/programmes_controller.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ def show
1616
policy_scope(Consent).where(patient: patients, programme: @programme)
1717
end
1818

19-
def sessions
20-
@sessions =
21-
policy_scope(Session)
22-
.has_programme(@programme)
23-
.for_current_academic_year
24-
.includes(:location, :session_dates)
25-
.order("locations.name")
26-
end
27-
2819
def consent_form
2920
send_file(
3021
"public/consent_forms/#{@programme.type}.pdf",

app/views/programmes/sessions.html.erb renamed to app/views/programmes/sessions/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= content_for :page_title, "#{@programme.name} – Sessions" %>
1+
<%= content_for :page_title, "#{@programme.name} – #{t(".title")}" %>
22

33
<% content_for :before_main do %>
44
<%= render AppBreadcrumbComponent.new(items: [

config/locales/en.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,8 @@ en:
590590
index:
591591
title: Children
592592
sessions:
593-
table_headings:
594-
completed: All sessions completed
595-
scheduled: Sessions scheduled
596-
unscheduled: No sessions scheduled
593+
index:
594+
title: Sessions
597595
vaccinations:
598596
index:
599597
title: Vaccinations

config/routes.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,15 @@
166166
end
167167

168168
resources :programmes, only: %i[index show], param: :type do
169-
member do
170-
get "sessions"
171-
172-
get "consent-form", action: "consent_form"
169+
get "consent-form", on: :member
170+
171+
scope module: :programmes do
172+
resources :cohorts, only: %i[index show]
173+
resources :patients, only: :index
174+
resources :reports, only: :create
175+
resources :sessions, only: :index
176+
resources :vaccinations, only: :index
173177
end
174-
175-
resources :cohorts, only: %i[index show], controller: "programmes/cohorts"
176-
resources :patients, only: :index, controller: "programmes/patients"
177-
resources :reports, only: :create, controller: "programmes/reports"
178-
resources :vaccinations, only: :index, controller: "programmes/vaccinations"
179178
end
180179

181180
resources :school_moves, path: "school-moves", only: %i[index show update]

0 commit comments

Comments
 (0)