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

@@ -40,6 +40,7 @@ api.marketBuy = function(req, res, next){
})
}
/*
------------------------------------------------------------------------
Tasks
@@ -64,15 +65,12 @@ api.verifyTaskExists = function(req, res, next) {
};
function deleteTask(user, task) {
var i = _.findIndex(user[task.type+'s'], {id:task.id});
if (~i) {
user[task.type+'s'].splice(i, 1)
//user.markModified(task.type+'s')
}
user[task.type+'s'].id(task.id).remove();
};
function addTask(user, task) {
task = helpers.taskDefaults(task);
task._id = task.id;
user[task.type+'s'].unshift(task);
return task;
}

View File

@@ -2,6 +2,7 @@ var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var helpers = require('habitrpg-shared/script/helpers');
var _ = require('lodash');
var TaskSchema = require('./task').schema;
var Group = require('./group').model;
var ChallengeSchema = new Schema({
@@ -9,10 +10,10 @@ var ChallengeSchema = new Schema({
name: String,
shortName: String,
description: String,
habits: [Schema.Types.Mixed],
dailys: [Schema.Types.Mixed],
todos: [Schema.Types.Mixed],
rewards: [Schema.Types.Mixed],
habits: [TaskSchema],
dailys: [TaskSchema],
todos: [TaskSchema],
rewards: [TaskSchema],
leader: {type: String, ref: 'User'},
group: {type: String, ref: 'Group'},
timestamp: {type: Date, 'default': Date.now},
@@ -30,11 +31,6 @@ ChallengeSchema.virtual('tasks').get(function () {
// FIXME this isn't always triggered, since we sometimes use update() or findByIdAndUpdate()
// @see https://github.com/LearnBoost/mongoose/issues/964
ChallengeSchema.pre('save', function(next){
// @see comment in user.js pre(save)
this.markModified('habits');
this.markModified('dailys');
this.markModified('todos');
this.markModified('rewards');
this.memberCount = _.size(this.members);
next()
})
@@ -43,11 +39,11 @@ ChallengeSchema.methods.toJSON = function(){
var doc = this.toObject();
doc.memberCount = doc.members ? _.size(doc.members) : doc.memberCount; // @see pre('save') comment above
doc._isMember = this._isMember;
// _.each(['habits','dailys','todos','rewards'], function(type){
// _.each(doc[type],function(task){
// task.id = task._id || task.id;
// })
// })
_.each(['habits','dailys','todos','rewards'], function(type){
_.each(doc[type],function(task){
task.id = task._id;
})
})
return doc;
}

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;