From edea13483ae69ada0715bc39568f50c207acf17f Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Wed, 27 Apr 2016 13:16:30 -0700 Subject: [PATCH] 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. --- server/middleware/classrooms.coffee | 2 +- server/models/Classroom.coffee | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/server/middleware/classrooms.coffee b/server/middleware/classrooms.coffee index 77d1b8a3a..3e8980863 100644 --- a/server/middleware/classrooms.coffee +++ b/server/middleware/classrooms.coffee @@ -140,4 +140,4 @@ module.exports = # finish database.validateDoc(classroom) classroom = yield classroom.save() - res.status(201).send(classroom.toObject({req: req})) \ No newline at end of file + res.status(201).send(classroom.toObject({req: req})) diff --git a/server/models/Classroom.coffee b/server/models/Classroom.coffee index 26edcb769..1e09464fa 100644 --- a/server/models/Classroom.coffee +++ b/server/models/Classroom.coffee @@ -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.' \ No newline at end of file + console.log 'Populated courses data.'