Revert "[#1711] back to Schema.Types.Mixed for user.tasks, this makes me sad :("

This reverts commit c0292d0842.
This commit is contained in:
Tyler Renelle
2013-11-02 12:24:05 -07:00
parent c0292d0842
commit 01536a2e29
3 changed files with 25 additions and 38 deletions

View File

@@ -8,6 +8,7 @@ var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var helpers = require('habitrpg-shared/script/helpers');
var _ = require('lodash');
var TaskSchema = require('./task').schema;
// User Schema
// -----------
@@ -190,10 +191,10 @@ var UserSchema = new Schema({
challenges: [{type: 'String', ref:'Challenge'}],
habits: [Schema.Types.Mixed],
dailys: [Schema.Types.Mixed],
todos: [Schema.Types.Mixed],
rewards: [Schema.Types.Mixed],
habits: [TaskSchema],
dailys: [TaskSchema],
todos: [TaskSchema],
rewards: [TaskSchema],
}, {
strict: true,
@@ -208,11 +209,12 @@ UserSchema.methods.toJSON = function() {
doc.filters = {};
doc._tmp = this._tmp; // be sure to send down drop notifs
// _.each(['habits','dailys','todos','rewards'], function(type){
// _.each(doc[type],function(task){
// task.id = task._id || task.id;
// })
// })
// TODO why isnt' this happening automatically given the TaskSchema.methods.toJSON above?
_.each(['habits','dailys','todos','rewards'], function(type){
_.each(doc[type],function(task){
task.id = task._id;
})
})
return doc;
};
@@ -227,16 +229,7 @@ UserSchema.virtual('tasks').get(function () {
// Custom setter/getter virtuals?
UserSchema.pre('save', function(next) {
// I finally figured out when markModified() is required - when using Schema.Type.Mixed or Array. Let's say you
// have `tasks: [{text:String, notes:String}]`. Because it's a defined schema, Mongoose can track attribute changes.
// But if you have `tasks: [Schema.Types.Mixed]`, Mongoose can't track underlying attr changes - only top-level changes
// (via splice(), push/unshift(), etc). Adding a workaround here, which is very inefficient - we'll want to find
// each location where a task attr is modified and use `user.markModified(task.type+'s')`
// (@see https://github.com/HabitRPG/habitrpg/issues/1711#issuecomment-27626467)
this.markModified('habits');
this.markModified('dailys');
this.markModified('todos');
this.markModified('rewards');
//this.markModified('tasks');
if (!this.profile.name) {
var fb = this.auth.facebook;