-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreviews_controller.rb
More file actions
255 lines (218 loc) · 8.2 KB
/
reviews_controller.rb
File metadata and controls
255 lines (218 loc) · 8.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# typed: strict
# frozen_string_literal: true
class ReviewsController < ApplicationController
extend T::Sig
before_action :store_user_location!, if: :storable_location?
before_action :authenticate_user!
class ReviewParams < T::Struct
const :section_id, String
const :grade, T.nilable(String)
const :organization, Integer
const :clarity, Integer
const :overall, Integer
const :weekly_time, String
const :group_project, T::Boolean
const :extra_credit, T::Boolean
const :attendance, T::Boolean
const :midterm_count, Integer
const :final, String
const :textbook, T::Boolean
const :comments, String
end
class NewParams < T::Struct
const :course, T.nilable(Integer)
const :section, T.nilable(Integer)
end
class CreateParams < T::Struct
const :review, ReviewParams
end
class EditParams < T::Struct
const :id, Integer
end
class UpdateParams < T::Struct
const :id, Integer
const :review, ReviewParams
end
class CourseSuggestionsParams < T::Struct
const :q, String
end
class TermSuggestionsParams < T::Struct
const :course_id, String
end
class SectionSuggestionsParams < T::Struct
const :course_id, Integer
const :term, String
end
sig { void }
def initialize
super
@review = T.let(nil, T.nilable(Review))
@initial_suggestion_type = T.let(nil, T.nilable(String))
@course = T.let(nil, T.nilable(Course))
@term = T.let(nil, T.nilable(Term))
@section = T.let(nil, T.nilable(Section))
@sections = T.let(Section.none, T.any(Section::ActiveRecord_Relation, Section::ActiveRecord_AssociationRelation))
@results = T.let(nil, T.untyped)
end
# GET /reviews/new
sig { void }
def new
typed_params = TypedParams[NewParams].new.extract!(params)
@review = Review.new
if typed_params.section.present?
@section = Section.find(typed_params.section)
@initial_suggestion_type = "section"
@course = @section.course
@term = @section.term
@sections = Section.where(course_id: @course.id, term_id: @term.id)
.includes(:term, :instructor)
.order(:index)
elsif typed_params.course.present?
@course = Course.find(typed_params.course)
@initial_suggestion_type = "course"
end
end
# GET /reviews/:id/edit
sig { void }
def edit
typed_params = TypedParams[EditParams].new.extract!(params)
@review = Review.find(typed_params.id)
if current_user != @review.user
flash[:error] = "You can't edit a review that isn't yours."
redirect_back_or_to(my_courses_path)
end
@section = T.must(@review.section)
@initial_suggestion_type = "section"
@course = @section.course
@term = @section.term
@sections = Section.where(course_id: @course.id, term_id: @term.id)
.includes(:term, :instructor)
.order(:index)
end
# POST /reviews
sig { void }
def create
typed_params = TypedParams[CreateParams].new.extract!(params)
review_params = typed_params.review
# This action requires authentication, so we're guaranteed a user
user = T.must(current_user)
return unless review_valid?(review_params, user)
is_first_review = T.let(user.reviews.empty?, T::Boolean)
relationship = Relationship.find_or_create_by(
section_id: review_params.section_id,
user_id: user.id,
)
review = relationship.create_review(
grade: review_params.grade,
organization: review_params.organization,
weekly_time: review_params.weekly_time,
clarity: review_params.clarity,
overall: review_params.overall,
has_group_project: review_params.group_project,
offers_extra_credit: review_params.extra_credit,
requires_attendance: review_params.attendance,
midterm_count: review_params.midterm_count,
final: review_params.final,
recommend_textbook: review_params.textbook,
comments: review_params.comments,
status: "pending",
)
logger.info("Queueing NotifyOnNewReviewJob")
NotifyOnNewReviewJob.perform_later(review)
# Flash doesn't work here since we do the redirect through js,
# so we store a session variable instead
if is_first_review && user.referred_by
session[:referred_review_created] = true
else
session[:review_submitted] = true
end
# Disable turbolinks here, we handle the redirect in JS
redirect_to(my_courses_path, turbolinks: false)
rescue ActionController::BadRequest => e
logger.error(e.inspect)
render(json: { msg: "Could not create review. Make sure you fill out the class and all questions." }, status: :bad_request)
end
# PUT/PATCH /reviews/:id
sig { void }
def update
typed_params = TypedParams[UpdateParams].new.extract!(params)
@review = Review.find(typed_params.id)
review_params = typed_params.review
# This action requires authentication, so we're guaranteed a user
user = T.must(current_user)
if user != @review.user
render(json: { msg: "You can't edit a review that isn't yours." }, status: :bad_request)
return
end
return unless review_valid?(review_params, user, existing_review: true)
@review.update(
grade: review_params.grade,
organization: review_params.organization,
weekly_time: review_params.weekly_time,
clarity: review_params.clarity,
overall: review_params.overall,
has_group_project: review_params.group_project,
offers_extra_credit: review_params.extra_credit,
requires_attendance: review_params.attendance,
midterm_count: review_params.midterm_count,
final: review_params.final,
recommend_textbook: review_params.textbook,
comments: review_params.comments,
status: "pending",
)
logger.info("Queueing NotifyOnNewReviewJob")
NotifyOnNewReviewJob.perform_later(@review, edit: true)
session[:review_submitted] = true
# Disable turbolinks here, we handle the redirect in JS
# Some browsers will try to follow the redirect using the original request method (PUT/PATCH). Use 303 to force GET of /my-courses.
redirect_to(my_courses_path, turbolinks: false, status: :see_other)
rescue ActionController::BadRequest => e
logger.error(e.inspect)
render(json: { msg: "Could not update review. Make sure you fill out the class and all questions." }, status: :bad_request)
end
# GET /reviews/term-suggestions
sig { void }
def course_suggestions
typed_params = TypedParams[CourseSuggestionsParams].new.extract!(params)
@results = Course.search(typed_params.q, limit: 8, track: true)
render(formats: :json)
end
# GET /reviews/term-suggestions
sig { void }
def term_suggestions
typed_params = TypedParams[TermSuggestionsParams].new.extract!(params)
@course = Course.find(typed_params.course_id)
render(formats: :json)
end
# GET /reviews/section-suggestions
sig { void }
def section_suggestions
typed_params = TypedParams[SectionSuggestionsParams].new.extract!(params)
@term = Term.find_by(term: typed_params.term)
return unless @term
@sections = Section.where(course_id: typed_params.course_id, term_id: @term.id)
.includes(:term, :instructor)
.order(:index)
render(formats: :json)
end
private
sig { params(review_params: ReviewParams, user: User, existing_review: T::Boolean).returns(T::Boolean) }
def review_valid?(review_params, user, existing_review: false)
section = Section.find(review_params.section_id)
if user.review_count_for_term(section.term) >= 6
render(json: { msg: "You can only review six classes per term." }, status: :bad_request)
false
elsif !existing_review && user.reviewed_course?(section.course)
render(json: { msg: "You've already reviewed this course." }, status: :bad_request)
false
elsif review_params.comments.length < 40
render(json: { msg: "Your review looks a little short. Tell us a bit more about the class!" }, status: :bad_request)
false
elsif review_params.comments.gibberish?
render(json: { msg: "We had trouble understanding your review. Please make sure everything looks correct!" }, status: :bad_request)
false
else
true
end
end
end