This commit is contained in:
barisusakli
2015-09-28 16:09:33 -04:00
parent c72bb54957
commit e7f87f4ffd
3 changed files with 46 additions and 39 deletions

View File

@@ -10,45 +10,52 @@ module.exports = function(Categories) {
Categories.update = function(modified, callback) {
function updateCategory(cid, next) {
Categories.exists(cid, function(err, exists) {
if (err || !exists) {
return next(err);
}
var modifiedFields = modified[cid];
if (modifiedFields.hasOwnProperty('name')) {
modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name);
}
plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
if (err) {
return next(err);
}
var category = categoryData.category;
var fields = Object.keys(category);
async.each(fields, function(key, next) {
updateCategoryField(cid, key, category[key], next);
}, function(err) {
if (err) {
return next(err);
}
plugins.fireHook('action:category.update', {cid: cid, modified: category});
next();
});
});
});
}
var cids = Object.keys(modified);
async.each(cids, updateCategory, function(err) {
async.each(cids, function(cid, next) {
updateCategory(cid, modified[cid], next);
}, function(err) {
callback(err, cids);
});
};
function updateCategory(cid, modifiedFields, callback) {
Categories.exists(cid, function(err, exists) {
if (err || !exists) {
return callback(err);
}
if (modifiedFields.hasOwnProperty('name')) {
modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name);
}
plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
if (err) {
return callback(err);
}
var category = categoryData.category;
var fields = Object.keys(category);
// move parent to front, so its updated first
var parentCidIndex = fields.indexOf('parentCid');
if (parentCidIndex !== -1 && fields.length > 1) {
fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]);
}
async.eachSeries(fields, function(key, next) {
updateCategoryField(cid, key, category[key], next);
}, function(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:category.update', {cid: cid, modified: category});
callback();
});
});
});
}
function updateCategoryField(cid, key, value, callback) {
if (key === 'parentCid') {
return updateParent(cid, value, callback);

View File

@@ -68,7 +68,7 @@ module.exports = function(db, module) {
return callback(err, null);
}
callback(null, item[field] || null);
callback(null, item.hasOwnProperty(field) ? item[field] : null);
});
};