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

Commit d9debb39 by Mick Hansen

Remove a concat from build() and fix tests

1 parent 9b22f127
......@@ -655,16 +655,7 @@ module.exports = (function() {
validateIncludedElements.call(this, options)
}
if (options.includeNames) {
options.includeNames = options.includeNames.concat(options.includeNames.map(function (key) {
return key.slice(0,1).toLowerCase() + key.slice(1)
}))
}
var self = this
, instance = new this.DAO(values, options)
return instance
return new this.DAO(values, options)
}
DAOFactory.prototype.create = function(values, fieldsOrOptions) {
......@@ -1343,6 +1334,7 @@ module.exports = (function() {
options.includeMap[include.as] = include
options.includeNames.push(include.as)
options.includeNames.push(include.as.substr(0,1).toLowerCase() + include.as.substr(1))
if (include.association.isMultiAssociation || include.hasMultiAssociation) options.hasMultiAssociation = true
if (include.association.isSingleAssociation || include.hasSingleAssociation) options.hasSingleAssociation = true
......
......@@ -230,7 +230,10 @@ module.exports = (function() {
// Queries with include
} else if (this.options.hasJoin === true) {
results = groupJoinData(results, this.options)
results = groupJoinData(results, this.options, {
checkExisting: this.options.hasMultiAssociation
})
result = results.map(function(result) {
return this.callee.build(result, {
isNewRecord: false,
......@@ -328,22 +331,24 @@ module.exports = (function() {
]
*/
var groupJoinData = function(data, options) {
// includeOptions are 'level'-specific where options is a general directive
var groupJoinData = function(data, includeOptions, options) {
var results = []
, existingResult
, calleeData
, child
, calleeDataIgnore = ['__children']
if (options.includeNames) {
calleeDataIgnore = calleeDataIgnore.concat(options.includeNames)
if (includeOptions.includeNames) {
calleeDataIgnore = calleeDataIgnore.concat(includeOptions.includeNames)
}
data.forEach(function (row) {
row = Dot.transform(row)
calleeData = _.omit(row, calleeDataIgnore)
existingResult = options.hasMultiAssociation && _.find(results, function (result) {
// If there are :M associations included we need to see if the main result of the row has already been identified
existingResult = options.checkExisting && _.find(results, function (result) {
if (calleeDataIgnore) {
return Utils._.isEqual(_.omit(result, calleeDataIgnore), calleeData)
}
......@@ -357,11 +362,16 @@ module.exports = (function() {
for (var attrName in row) {
if (row.hasOwnProperty(attrName)) {
// Child if object, and is an child include
child = Object(row[attrName]) === row[attrName] && options.includeMap && options.includeMap[attrName]
child = Object(row[attrName]) === row[attrName] && includeOptions.includeMap && includeOptions.includeMap[attrName]
if (child) {
if (!existingResult.__children) existingResult.__children = {}
if (!existingResult.__children[attrName]) existingResult.__children[attrName] = []
// Make sure nested object is available
if (!existingResult.__children) {
existingResult.__children = {}
}
if (!existingResult.__children[attrName]) {
existingResult.__children[attrName] = []
}
existingResult.__children[attrName].push(row[attrName])
}
......@@ -371,7 +381,7 @@ module.exports = (function() {
results.forEach(function (result) {
_.each(result.__children, function (children, key) {
result[key] = groupJoinData(children, (options.includeMap && options.includeMap[key]))
result[key] = groupJoinData(children, (includeOptions.includeMap && includeOptions.includeMap[key]), options)
})
delete result.__children
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!