Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/controllers/admin/school_classes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@

module Admin
class SchoolClassesController < Admin::ApplicationController
helper_method :class_teacher_users_by_id

private

def class_teacher_users_by_id
@class_teacher_users_by_id ||= User.from_userinfo(ids: requested_resource.teachers.map(&:user_id).uniq).index_by(&:id)
end
end
end
2 changes: 1 addition & 1 deletion app/dashboards/school_class_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SchoolClassDashboard < Administrate::BaseDashboard
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
school: Field::BelongsTo,
teachers: Field::HasMany,
teachers: ClassTeachersField,
students: Field::HasMany,
lessons: Field::HasMany,
id: Field::String,
Expand Down
20 changes: 20 additions & 0 deletions app/fields/class_teachers_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'administrate/field/base'

class ClassTeachersField < Administrate::Field::Base
def teachers
@teachers ||= data.sort_by(&:created_at)
end

def user_display(teacher, users_by_id = {})
user = users_by_id[teacher.user_id]
user.present? ? user_dashboard.display_resource(user) : teacher.user_id
end

private

def user_dashboard
@user_dashboard ||= UserDashboard.new
end
end
20 changes: 20 additions & 0 deletions app/views/fields/class_teachers_field/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% if field.teachers.any? %>
<table>
<thead>
<tr>
<th>Teacher</th>
<th>Added to class</th>
</tr>
</thead>
<tbody>
<% field.teachers.each do |teacher| %>
<tr>
<td><%= field.user_display(teacher, class_teacher_users_by_id) %></td>
<td><%= l(teacher.created_at) %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<%= t('administrate.fields.has_many.none', default: '–') %>
<% end %>
46 changes: 46 additions & 0 deletions spec/features/admin/school_classes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Admin school classes', type: :request do
let(:admin_user) { create(:admin_user) }
let(:school) { create(:school) }
let(:teacher) { create(:user, name: 'Tariq Teacher', email: 'teacher@example.com') }
let(:other_teacher) { create(:user, name: 'Olivia Teacher', email: 'olivia@example.com') }
let(:school_class) { create(:school_class, school:, teacher_ids: [teacher.id, other_teacher.id]) }

before do
allow(User).to receive(:from_omniauth).and_return(admin_user)
get '/auth/callback'
allow(User).to receive(:from_userinfo).with(ids: contain_exactly(teacher.id, other_teacher.id)).and_return([other_teacher, teacher])
end

it 'displays each teacher name and email using a single batch lookup' do
get admin_school_class_path(school_class)

expect(response).to have_http_status(:success)
expect(response.body).to include('Tariq Teacher (teacher@example.com)', 'Olivia Teacher (olivia@example.com)')
expect(response.body).to include('<th>Added to class</th>')
expect(response.body).not_to include('<th>Updated</th>')
expect(response.body).not_to include(teacher.id, other_teacher.id)
expect(User).to have_received(:from_userinfo).with(ids: contain_exactly(teacher.id, other_teacher.id)).once
end

it 'falls back to the UUID when a teacher is missing from user info' do
allow(User).to receive(:from_userinfo).and_return([teacher])

get admin_school_class_path(school_class)

expect(response).to have_http_status(:success)
expect(response.body).to include('Tariq Teacher (teacher@example.com)', other_teacher.id)
end
Comment thread
adrian-rpf marked this conversation as resolved.

it 'renders an empty teacher list without looking up users' do
school_class.teachers.destroy_all

get admin_school_class_path(school_class)

expect(response).to have_http_status(:success)
expect(User).not_to have_received(:from_userinfo)
end
end
Loading