Skip to content

Commit 766ca77

Browse files
committed
fixing n+1 and tests
1 parent eacd7e8 commit 766ca77

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

app/controllers/api/school_classes_controller.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ class SchoolClassesController < ApiController
77
before_action :load_and_authorize_school_class
88

99
def index
10-
school_classes = @school.classes.accessible_by(current_ability)
11-
school_classes = school_classes.joins(:teachers).where(teachers: { teacher_id: current_user.id }) if params[:my_classes] == 'true'
10+
if current_user&.school_teacher?(@school) || current_user&.school_owner?(@school)
11+
school_classes = @school.classes.accessible_by(current_ability).includes(:lessons)
12+
else
13+
school_classes = @school.classes.accessible_by(current_ability)
14+
end
15+
16+
school_classes = school_classes.joins(:teachers).where(teachers: { teacher_id: current_user&.id }) if params[:my_classes] == 'true'
1217
@school_classes_with_teachers = school_classes.with_teachers
18+
1319
if current_user&.school_teacher?(@school) || current_user&.school_owner?(@school)
1420
render :teacher_index, formats: [:json], status: :ok
1521
else
16-
render :index, formats: [:json], status: :ok
22+
render :student_index, formats: [:json], status: :ok
1723
end
1824
end
1925

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

3-
json.call(teacher, :id, :name)
4-
json.type('teacher')
3+
if teacher.present?
4+
json.call(teacher, :id, :name)
5+
json.type('teacher')
56

6-
include_email = local_assigns.fetch(:include_email, true)
7+
include_email = local_assigns.fetch(:include_email, true)
78

8-
json.email(teacher.email) if include_email
9+
json.email(teacher.email) if include_email
10+
end

0 commit comments

Comments
 (0)