From cf7d73ff37e3d636fd9d4fd810b52e98f93cf712 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Sat, 28 Apr 2012 09:17:33 -0700 Subject: [PATCH] fixed resource rename and delete issues --- lib/collections/resources.js | 39 +++++++++++++++++++++++++++++++----- test/resources.test.js | 20 +++++++++++++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/lib/collections/resources.js b/lib/collections/resources.js index b130981..aec88d3 100644 --- a/lib/collections/resources.js +++ b/lib/collections/resources.js @@ -26,10 +26,22 @@ var resources = module.exports = // dont insert renameFrom delete resource.$renameFrom; - // rename - collection.use(rename).rename(resource.path.replace('/', ''), function (err) { - next(err); - }); + if(resource.type === 'Static') { + // rename + collection.use(rename + '.chunks').rename(resource.path.replace('/', '') + '.chunks', function (err) { + if(err) return next(err); + collection.use(rename + '.files').rename(resource.path.replace('/', '') + '.files', function (err) { + next(err); + }); + }); + } else { + // rename + collection.use(rename).rename(resource.path.replace('/', ''), function (err) { + next(err); + }); + } + + return; } @@ -50,12 +62,29 @@ var resources = module.exports = } }); } - }) + }); if(!renames) { next(); } + } else if(req.method === 'DELETE' && req.query._id) { + resources.get(req.query).first(function (err, res) { + if(err || !res) return next(err); + if(res.type === 'Static') { + collection.use(res.path + '.files').del(function () { + collection.use(res.path + '.chunks').del(function () { + next(); + }) + }) + } else { + collection.use(res.path).del(function (err) { + // continue on collection not found errors - still remove resource + if(err && ~err.message.indexOf('ns not found')) err = undefined; + next(err); + }); + } + }); } else { next(); } diff --git a/test/resources.test.js b/test/resources.test.js index 1d28b59..3897eb4 100644 --- a/test/resources.test.js +++ b/test/resources.test.js @@ -113,7 +113,7 @@ describe('Application Resource Types', function(){ }) }) - describe('DELETE /resources', function(){ + describe('DELETE /resources/', function(){ it('should remove all resources or those that match the query', function(done) { resources.del(function (err) { resources.get(function (error, all) { @@ -123,4 +123,22 @@ describe('Application Resource Types', function(){ }) }) }) + + describe('DELETE /resources/', function(){ + it('should remove the resource and all of its data', function(done) { + todos.post({title: 'another todo...'}, function (err, res) { + resources.get({path: '/todos'}, function (e, res) { + res = res[0]; + resources.use('/' + res._id).del(function (err, upd) { + resources.post(data.resources.todos, function (e) { + todos.get(function (err, res) { + expect(res).to.not.exist; + done(err); + }) + }); + }) + }) + }) + }) + }) }) \ No newline at end of file