fixed resource rename and delete issues

This commit is contained in:
Ritchie Martori
2012-04-28 09:17:33 -07:00
parent 5ea6b9780e
commit cf7d73ff37
2 changed files with 53 additions and 6 deletions

View File

@@ -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();
}

View File

@@ -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/<ObjectID>', 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);
})
});
})
})
})
})
})
})