Skip to content
Draft
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
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run test file (RSpec)",
"type": "shell",
"command": "./bin/rspec",
"args": [
"${file}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": "test",
"presentation": {
"reveal": "always",
"panel": "dedicated",
"clear": true
},
"problemMatcher": []
},
{
"label": "Run test at current line (RSpec)",
"type": "shell",
"command": "./bin/rspec",
"args": [
"${file}:${lineNumber}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": "test",
"presentation": {
"reveal": "always",
"panel": "dedicated",
"clear": true
},
"problemMatcher": []
}
]
}
6 changes: 4 additions & 2 deletions app/controllers/api/lessons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class LessonsController < ApiController
def index
accessible_lessons = filtered_lessons_scope
.accessible_by(current_ability)
.includes(project: :remixes)
@lessons_with_users = accessible_lessons.with_users

if current_user&.school_teacher?(school) || current_user&.school_owner?(school)
accessible_lessons = accessible_lessons.includes(project: { remixes: %i[submitted_school_project] })
@lessons_with_users = accessible_lessons.with_users
render :teacher_index, formats: [:json], status: :ok
else
remixes = user_remixes(accessible_lessons)
accessible_lessons = accessible_lessons.includes(project: :remixes)
@lessons_with_users = accessible_lessons.with_users
@lessons_with_users_and_remixes = @lessons_with_users.zip(remixes)
render :student_index, formats: [:json], status: :ok
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/lesson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def with_user
def submitted_count
return 0 unless project

project.remixes.count { |remix| remix.school_project&.submitted? }
project.remixes.count(&:submitted_school_project)
end

private
Expand Down
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Types
has_many_attached :videos
has_many_attached :audio
has_one :school_project, dependent: :destroy
has_one :submitted_school_project, -> { in_state(:submitted) }, class_name: 'SchoolProject', dependent: :destroy, inverse_of: :project

accepts_nested_attributes_for :components
accepts_nested_attributes_for :scratch_component
Expand Down
16 changes: 16 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")
12 changes: 9 additions & 3 deletions spec/features/lesson/listing_lessons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@
end

it 'includes the submitted_count for each lesson' do
student_2 = create(:student, school:)
create(:class_student, school_class:, student_id: student_2.id)

lesson.update!(school_class_id: school_class.id)
remix = create(:project, school:, remixed_from_id: lesson.project.id, user_id: student.id)
remix.school_project.transition_status_to!(:submitted, student.id)

[student, student_2].each do |student|
remix = create(:project, school:, remixed_from_id: lesson.project.id, user_id: student.id)
remix.school_project.transition_status_to!(:submitted, student.id)
end

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

expect(data.first[:submitted_count]).to eq(1)
expect(data.first[:submitted_count]).to eq(2)
end

context 'when filtering by project_identifier' do
Expand Down
Loading