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

Commit 5cbd2452 by Overlook Motel

refactor validateIncludedElement to utilise altered getAssociation

1 parent 2ed685be
Showing with 12 additions and 27 deletions
...@@ -1349,21 +1349,22 @@ module.exports = (function() { ...@@ -1349,21 +1349,22 @@ module.exports = (function() {
var validateIncludedElement = function(include, parent) { var validateIncludedElement = function(include, parent) {
if (include instanceof DAOFactory) { if (include instanceof DAOFactory) {
include = { daoFactory: include, as: include.tableName } include = { daoFactory: include }
} }
if (typeof parent === "undefined") { if (typeof parent === "undefined") {
parent = this parent = this
} }
if (typeof include === 'object') { if (typeof include !== 'object') {
throw new Error('Include unexpected. Element has to be either an instance of DAOFactory or an object.')
}
if (include.hasOwnProperty('model')) { if (include.hasOwnProperty('model')) {
include.daoFactory = include.model include.daoFactory = include.model
delete include.model delete include.model
} } else if (!include.hasOwnProperty('daoFactory')) {
throw new Error('Include malformed. Expected attributes: daoFactory, as!')
if (!include.hasOwnProperty('as')) {
include.as = include.daoFactory.tableName
} }
if (include.hasOwnProperty('attributes')) { if (include.hasOwnProperty('attributes')) {
...@@ -1384,18 +1385,11 @@ module.exports = (function() { ...@@ -1384,18 +1385,11 @@ module.exports = (function() {
// pseudo include just needed the attribute logic, return // pseudo include just needed the attribute logic, return
if (include._pseudo) return include if (include._pseudo) return include
if (include.hasOwnProperty('daoFactory') && (include.hasOwnProperty('as'))) {
var usesAlias = (include.as !== include.daoFactory.tableName)
, association = (usesAlias ? parent.getAssociationByAlias(include.as) : parent.getAssociation(include.daoFactory))
// If single (1:1) association, we singularize the alias, so it will match the automatically generated alias of belongsTo/HasOne
if (association && !usesAlias && association.isSingleAssociation) {
include.as = Utils.singularize(include.daoFactory.tableName, include.daoFactory.options.language)
}
// check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include // check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include
if (association && (!association.options.as || (association.options.as === include.as))) { var association = parent.getAssociation(include.daoFactory, include.as)
if (association) {
include.association = association include.association = association
include.as = association.as
// If through, we create a pseudo child include, to ease our parsing later on // If through, we create a pseudo child include, to ease our parsing later on
if (Object(include.association.through) === include.association.through) { if (Object(include.association.through) === include.association.through) {
...@@ -1415,10 +1409,7 @@ module.exports = (function() { ...@@ -1415,10 +1409,7 @@ module.exports = (function() {
} }
if (include.required === undefined) { if (include.required === undefined) {
include.required = false include.required = !!include.where
if (include.where) {
include.required = true
}
} }
// Validate child includes // Validate child includes
...@@ -1430,7 +1421,7 @@ module.exports = (function() { ...@@ -1430,7 +1421,7 @@ module.exports = (function() {
} else { } else {
var msg = include.daoFactory.name var msg = include.daoFactory.name
if (usesAlias) { if (include.as) {
msg += " (" + include.as + ")" msg += " (" + include.as + ")"
} }
...@@ -1438,12 +1429,6 @@ module.exports = (function() { ...@@ -1438,12 +1429,6 @@ module.exports = (function() {
throw new Error(msg) throw new Error(msg)
} }
} else {
throw new Error('Include malformed. Expected attributes: daoFactory, as!')
}
} else {
throw new Error('Include unexpected. Element has to be either an instance of DAOFactory or an object.')
}
} }
var replaceReferencesWithTableNames = function(attributes) { var replaceReferencesWithTableNames = function(attributes) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!