不要怂,就是干,撸起袖子干!

Commit ecf511ae by overlookmotel

Separate conforming options into own function

1 parent 391610ea
Showing with 31 additions and 12 deletions
...@@ -694,7 +694,8 @@ module.exports = (function() { ...@@ -694,7 +694,8 @@ module.exports = (function() {
options = optClone(options || {}); options = optClone(options || {});
if (typeof options === 'object') { if (typeof options === 'object') {
if (options.hasOwnProperty('include') && options.include) { conformOptions(options);
if (options.include) {
hasJoin = true; hasJoin = true;
validateIncludedElements.call(this, options, tableNames); validateIncludedElements.call(this, options, tableNames);
...@@ -818,6 +819,7 @@ module.exports = (function() { ...@@ -818,6 +819,7 @@ module.exports = (function() {
*/ */
Model.prototype.count = function(options) { Model.prototype.count = function(options) {
options = Utils._.clone(options || {}); options = Utils._.clone(options || {});
conformOptions(options);
var col = '*'; var col = '*';
if (options.include) { if (options.include) {
...@@ -935,9 +937,12 @@ module.exports = (function() { ...@@ -935,9 +937,12 @@ module.exports = (function() {
}); });
} }
if (options.hasOwnProperty('include') && options.include && !options.includeValidated) { if (!options.includeValidated) {
conformOptions(options);
if (options.include) {
validateIncludedElements.call(this, options); validateIncludedElements.call(this, options);
} }
}
return new this.Instance(values, options); return new this.Instance(values, options);
}; };
...@@ -946,9 +951,12 @@ module.exports = (function() { ...@@ -946,9 +951,12 @@ module.exports = (function() {
Model.prototype.bulkBuild = function(valueSets, options) { Model.prototype.bulkBuild = function(valueSets, options) {
options = options || { isNewRecord: true, isDirty: true }; options = options || { isNewRecord: true, isDirty: true };
if (options.hasOwnProperty('include') && options.include && !options.includeValidated) { if (!options.includeValidated) {
conformOptions(options);
if (options.include) {
validateIncludedElements.call(this, options); validateIncludedElements.call(this, options);
} }
}
if (options.attributes) { if (options.attributes) {
options.attributes = options.attributes.map(function(attribute) { options.attributes = options.attributes.map(function(attribute) {
...@@ -1703,12 +1711,10 @@ module.exports = (function() { ...@@ -1703,12 +1711,10 @@ module.exports = (function() {
}.bind(this)); }.bind(this));
}; };
var validateIncludedElements = function(options, tableNames) { var conformOptions = function(options) {
tableNames = tableNames || {}; if (!options.include) {
options.includeNames = []; return;
options.includeMap = {}; }
options.hasSingleAssociation = false;
options.hasMultiAssociation = false;
// if include is not an array, wrap in an array // if include is not an array, wrap in an array
if (!Array.isArray(options.include)) { if (!Array.isArray(options.include)) {
...@@ -1718,24 +1724,37 @@ module.exports = (function() { ...@@ -1718,24 +1724,37 @@ module.exports = (function() {
return; return;
} }
// convert all included elements to { Model: Model } form // convert all included elements to { model: Model } form
var includes = options.include = options.include.map(function(include) { options.include = options.include.map(function(include) {
if (include instanceof Association) { if (include instanceof Association) {
include = { association: include }; include = { association: include };
} else if (include instanceof Model) { } else if (include instanceof Model) {
include = { model: include }; include = { model: include };
} else if (typeof include !== 'object') { } else if (typeof include !== 'object') {
throw new Error('Include unexpected. Element has to be either a Model, an Association or an object.'); throw new Error('Include unexpected. Element has to be either a Model, an Association or an object.');
} else if (include.hasOwnProperty('daoFactory')) { } else {
// convert daoFactory to model (for backwards compatibility) // convert daoFactory to model (for backwards compatibility)
if (include.hasOwnProperty('daoFactory')) {
include.model = include.daoFactory; include.model = include.daoFactory;
delete include.daoFactory; delete include.daoFactory;
} }
conformOptions(include);
}
return include; return include;
}); });
};
var validateIncludedElements = function(options, tableNames) {
tableNames = tableNames || {};
options.includeNames = [];
options.includeMap = {};
options.hasSingleAssociation = false;
options.hasMultiAssociation = false;
// validate all included elements // validate all included elements
var includes = options.include;
for (var index = 0; index < includes.length; index++) { for (var index = 0; index < includes.length; index++) {
var include = includes[index]; var include = includes[index];
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!