From 0a3e642c9e7820aec53bdaa28b443480e99cc581 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Fri, 21 Jun 2013 14:56:21 -0700 Subject: [PATCH] Fix memory adapter updateAttributes issue. Callback with an error if an id was not provided or if a model was not found by that id. --- lib/adapters/memory.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/adapters/memory.js b/lib/adapters/memory.js index f40d1753..ee840558 100644 --- a/lib/adapters/memory.js +++ b/lib/adapters/memory.js @@ -229,9 +229,26 @@ Memory.prototype.count = function count(model, callback, where) { }; Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) { + if(!id) { + var err = new Error('You must provide an id when updating attributes!'); + if(cb) { + return cb(err); + } else { + throw err; + } + } + data.id = id; - var base = JSON.parse(this.cache[model][id]); - this.save(model, merge(base, data), cb); + + var cachedModels = this.cache[model]; + var modelAsString = cachedModels && this.cache[model][id]; + var modelData = modelAsString && JSON.parse(modelAsString); + + if(modelData) { + this.save(model, merge(base, data), cb); + } else { + cb(new Error('Could not update attributes. Object with id ' + id + ' does not exist!')); + } }; Memory.prototype.transaction = function () {