-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlessons_controller.rb
More file actions
128 lines (106 loc) · 4.16 KB
/
Copy pathlessons_controller.rb
File metadata and controls
128 lines (106 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true
module Api
class LessonsController < ApiController
include RemixSelection
include LessonCreation
before_action :authorize_user, except: %i[index show]
before_action :verify_school_class_belongs_to_school, only: :create
before_action :verify_can_create_scratch_projects, only: %i[create create_copy]
load_and_authorize_resource :lesson
def index
accessible_lessons = filtered_lessons_scope.accessible_by(current_ability)
if current_user&.school_teacher?(school) || current_user&.school_owner?(school)
accessible_lessons = accessible_lessons.includes(: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_and_remixes = accessible_lessons.with_users.zip(remixes)
render :student_index, formats: [:json], status: :ok
end
end
def show
@lesson_with_user = @lesson.with_user
render :show, formats: [:json], status: :ok
end
def create
result = Lesson::Create.call(lesson_params: create_params)
if result.success?
track_project_event('Project - Created', result[:lesson].project)
@lesson_with_user = result[:lesson].with_user
render :show, formats: [:json], status: :created
else
render json: { error: result[:error] }, status: :unprocessable_content
end
end
def create_copy
result = Lesson::CreateCopy.call(lesson: @lesson, lesson_params: create_params)
if result.success?
@lesson_with_user = result[:lesson].with_user
render :show, formats: [:json], status: :created
else
render json: { error: result[:error] }, status: :unprocessable_content
end
end
def update
# TODO: Consider removing user_id from the lesson_params for update so users can update other users' lessons without changing ownership
# OR consider dropping user_id on lessons and using teacher id/ids on the class instead
previous_visibility = @lesson.visibility
result = Lesson::Update.call(lesson: @lesson, lesson_params: update_params)
if result.success?
track_project_event('Project - Made visible', result[:lesson].project) if previous_visibility != 'students' && result[:lesson].visibility == 'students'
@lesson_with_user = result[:lesson].with_user
render :show, formats: [:json], status: :ok
else
render json: { error: result[:error] }, status: :unprocessable_content
end
end
def destroy
@lesson.destroy!
head :no_content
end
private
def filtered_lessons_scope
scope = params[:school_class_id] ? Lesson.where(school_class_id: params[:school_class_id]) : Lesson.all
scope = scope.joins(:project).where(projects: { identifier: params[:project_identifier] }) if params[:project_identifier].present?
scope.order(created_at: :asc)
end
def verify_school_class_belongs_to_school
verify_lesson_school_class!(create_params)
end
def verify_can_create_scratch_projects
verify_lesson_scratch!(create_params)
end
def user_remixes(lessons)
lessons.map { |lesson| user_remix(lesson) }
end
def user_remix(lesson)
return nil unless lesson&.project&.remixes&.any?
remix_for_user(
lesson.project,
current_user,
include_feedback: current_user&.school_student?(school)
)
end
def update_params
params.fetch(:lesson, {}).permit(
:name,
:visibility,
{
project_attributes: [:name]
}
)
end
def create_params
source = params.fetch(:lesson, {})
source.permit(*LESSON_ATTRIBUTES, project_attributes: PROJECT_ATTRIBUTES).merge(user_id: current_user.id)
end
def school_owner?
school && current_user.school_owner?(school)
end
def school
@school ||= @lesson&.school || School.find_by(id: create_params[:school_id]) || SchoolClass.find_by(id: params[:school_class_id])&.school
end
end
end