Skip to content

Commit ed1ecb5

Browse files
zetter-rpfCopilot
andcommitted
Use new cached column
In [1] we started caching the value of submitted projects. Now we can use that value to simplify the code and reduce N+1 queries. [1] #804 Co-authored-by: Copilot <copilot@github.com>
1 parent 07cc1ce commit ed1ecb5

8 files changed

Lines changed: 12 additions & 60 deletions

File tree

app/controllers/api/school_classes_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def find_or_create_school_class(school_class_params)
131131

132132
def accessible_school_classes
133133
if current_user&.school_teacher?(@school) || current_user&.school_owner?(@school)
134-
@school.classes.accessible_by(current_ability).includes(lessons: { project: { remixes: { school_project: :school_project_transitions } } })
134+
@school.classes.accessible_by(current_ability).includes(:lessons)
135135
else
136136
@school.classes.accessible_by(current_ability)
137137
end

app/models/lesson.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ def with_user
3434
[self, User.from_userinfo(ids: user_id).first]
3535
end
3636

37-
def submitted_count
38-
return 0 unless project
39-
40-
project.remixes.count { |remix| remix.school_project&.submitted? }
41-
end
42-
4337
def recalculate_submitted_projects_count!
4438
count = school_projects.in_state(:submitted).count
4539
update!(submitted_projects_count: count)

app/models/school_class.rb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,8 @@ def assign_class_code
5858
errors.add(:code, 'could not be generated')
5959
end
6060

61-
def submitted_count
62-
return 0 if lessons.empty?
63-
64-
Lesson
65-
.joins(project: { remixes: { school_project: :school_project_transitions } })
66-
.where(school_class_id: id)
67-
.where(
68-
school_project_transitions: {
69-
to_state: 'submitted',
70-
most_recent: true
71-
}
72-
)
73-
.count
61+
def submitted_projects_count
62+
lessons.to_a.sum(&:submitted_projects_count)
7463
end
7564

7665
private

app/views/api/lessons/teacher_index.json.jbuilder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
json.array!(@lessons_with_users) do |lesson, user|
44
json.partial! 'lesson', lesson: lesson, user: user
5-
json.submitted_count(lesson.submitted_count)
5+
json.submitted_count(lesson.submitted_projects_count)
66
end

app/views/api/school_classes/teacher_index.json.jbuilder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
json.array!(@school_classes_with_teachers) do |school_class, teachers|
44
json.partial! 'school_class', school_class: school_class, teachers: teachers
55

6-
json.submitted_count(school_class.submitted_count)
6+
json.submitted_count(school_class.submitted_projects_count)
77
end

spec/features/lesson/listing_lessons_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,12 @@
8686
end
8787

8888
it 'includes the submitted_count for each lesson' do
89-
lesson.update!(school_class_id: school_class.id)
90-
remix = create(:project, school:, remixed_from_id: lesson.project.id, user_id: student.id)
91-
remix.school_project.transition_status_to!(:submitted, student.id)
89+
lesson.update!(school_class_id: school_class.id, submitted_projects_count: 17)
9290

9391
get("/api/lessons?school_class_id=#{school_class.id}", headers:)
9492
data = JSON.parse(response.body, symbolize_names: true)
9593

96-
expect(data.first[:submitted_count]).to eq(1)
94+
expect(data.first[:submitted_count]).to eq(17)
9795
end
9896

9997
context 'when filtering by project_identifier' do

spec/models/lesson_spec.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,28 +206,6 @@
206206
end
207207
end
208208

209-
describe '#submitted_count' do
210-
it 'returns 0 if there is no project' do
211-
lesson = create(:lesson, project: nil)
212-
expect(lesson.submitted_count).to eq(0)
213-
end
214-
215-
it 'returns the count of submitted remixes of the lesson project' do
216-
student = create(:student, school:)
217-
lesson = create(:lesson, school:, user_id: teacher.id)
218-
219-
remix_1 = create(:project, school:, remixed_from_id: lesson.project.id, user_id: student.id)
220-
remix_1.school_project.transition_status_to!(:submitted, remix_1.user_id)
221-
222-
remix_2 = create(:project, school:, remixed_from_id: lesson.project.id, user_id: student.id)
223-
remix_2.school_project.transition_status_to!(:submitted, remix_2.user_id)
224-
225-
create(:project, school:, remixed_from_id: lesson.project.id, user_id: student.id) # Not submitted
226-
227-
expect(lesson.submitted_count).to eq(2)
228-
end
229-
end
230-
231209
describe '#recalculate_submitted_projects_count!' do
232210
it 'sets the submitted projects count to 0 if there is no project' do
233211
lesson = create(:lesson, project: nil, submitted_projects_count: 3)

spec/models/school_class_spec.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,26 +262,19 @@
262262
end
263263
end
264264

265-
describe '#submitted_count' do
265+
describe '#submitted_projects_count' do
266266
it 'returns 0 if there are no lessons' do
267267
school_class = create(:school_class, teacher_ids: [teacher.id], school:)
268-
expect(school_class.submitted_count).to eq(0)
268+
expect(school_class.submitted_projects_count).to eq(0)
269269
end
270270

271271
it 'returns the sum of submitted counts from all lessons' do
272272
school_class = create(:school_class, teacher_ids: [teacher.id], school:)
273273

274-
lesson_1 = create(:lesson, school_class:, user_id: teacher.id)
275-
remix_1 = create(:project, school:, remixed_from_id: lesson_1.project.id, user_id: student.id)
276-
remix_1.school_project.transition_status_to!(:submitted, remix_1.user_id)
274+
create(:lesson, school_class:, user_id: teacher.id, submitted_projects_count: 5)
275+
create(:lesson, school_class:, user_id: teacher.id, submitted_projects_count: 3)
277276

278-
lesson_2 = create(:lesson, school_class:, user_id: teacher.id)
279-
remix_2 = create(:project, school:, remixed_from_id: lesson_2.project.id, user_id: student.id)
280-
remix_2.school_project.transition_status_to!(:submitted, remix_2.user_id)
281-
remix_3 = create(:project, school:, remixed_from_id: lesson_2.project.id, user_id: student.id)
282-
remix_3.school_project.transition_status_to!(:submitted, remix_3.user_id)
283-
284-
expect(school_class.submitted_count).to eq(3)
277+
expect(school_class.submitted_projects_count).to eq(8)
285278
end
286279
end
287280

0 commit comments

Comments
 (0)