Fix classroom courses temporary population

Can't count on toObject getting called, such as on the '/db/classroom/:handle/levels', which
gets the courses property directly. Use middleware instead.
This commit is contained in:
Scott Erickson 2016-04-27 13:16:30 -07:00
parent 9433cefcdc
commit edea13483a
2 changed files with 8 additions and 5 deletions

View file

@ -140,4 +140,4 @@ module.exports =
# finish
database.validateDoc(classroom)
classroom = yield classroom.save()
res.status(201).send(classroom.toObject({req: req}))
res.status(201).send(classroom.toObject({req: req}))

View file

@ -43,6 +43,12 @@ ClassroomSchema.pre('save', (next) ->
next()
)
ClassroomSchema.post('init', (next) ->
# TODO: Remove this once classrooms are populated. This is only for when we are testing locked course content.
if not @get('courses')
@set('courses', coursesData)
)
ClassroomSchema.methods.isOwner = (userID) ->
return userID.equals(@get('ownerID'))
@ -53,9 +59,6 @@ ClassroomSchema.statics.jsonSchema = jsonSchema
ClassroomSchema.set('toObject', {
transform: (doc, ret, options) ->
# TODO: Remove this once classrooms are populated. This is only for when we are testing locked course content.
if not ret.courses
ret.courses = coursesData
return ret unless options.req
user = options.req.user
unless user and (user.isAdmin() or user._id.equals(doc.get('ownerID')))
@ -87,4 +90,4 @@ co ->
_.extend(levelData, _.pick(level, 'type', 'slug', 'name'))
courseData.levels.push(levelData)
coursesData.push(courseData)
console.log 'Populated courses data.'
console.log 'Populated courses data.'