Skip to content

Commit

Permalink
refactor a few things to support associations still ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickhansen committed Nov 22, 2013
1 parent 79c364d commit a46121c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/associations/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ module.exports = (function() {
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.target, this.source, this.options)
Utils._.defaults(this.source.rawAttributes, newAttributes)

// Sync attributes to DAO proto each time a new assoc is added
this.source.DAO.prototype.attributes = Object.keys(this.source.DAO.prototype.rawAttributes)
// Sync attributes and setters/getters to DAO prototype
this.source.refreshAttributes()

return this
}
Expand Down
6 changes: 3 additions & 3 deletions lib/associations/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ module.exports = (function() {
Utils._.defaults(this.target.rawAttributes, newAttributes)
}

// Sync attributes to DAO proto each time a new assoc is added
this.target.DAO.prototype.attributes = Object.keys(this.target.DAO.prototype.rawAttributes);
this.source.DAO.prototype.attributes = Object.keys(this.source.DAO.prototype.rawAttributes);
// Sync attributes and setters/getters to DAO prototype
this.target.refreshAttributes()
this.source.refreshAttributes()

return this
}
Expand Down
5 changes: 2 additions & 3 deletions lib/associations/has-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ module.exports = (function() {
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options)
Utils._.defaults(this.target.rawAttributes, newAttributes)

// Sync attributes to DAO proto each time a new assoc is added
this.target.DAO.prototype.attributes = Object.keys(this.target.DAO.prototype.rawAttributes);

// Sync attributes and setters/getters to DAO prototype
this.target.refreshAttributes()
return this
}

Expand Down
61 changes: 35 additions & 26 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,35 @@ module.exports = (function() {
})
}

var attributeManipulation = {};
this.refreshAttributes();

this.DAO.prototype.booleanValues = []
this.DAO.prototype.defaultValues = {}
this.DAO.prototype.validators = {}

Utils._.each(this.rawAttributes, function (definition, name) {
if (((definition === DataTypes.BOOLEAN) || (definition.type === DataTypes.BOOLEAN))) {
self.DAO.prototype.booleanValues.push(name);
}
if (definition.hasOwnProperty('defaultValue')) {
self.DAO.prototype.defaultValues[name] = Utils._.partial(
Utils.toDefaultValue, definition.defaultValue)
}

if (definition.hasOwnProperty('validate')) {
self.DAO.prototype.validators[name] = definition.validate;
}
})

this.DAO.prototype.__factory = this
this.DAO.prototype.hasDefaultValues = !Utils._.isEmpty(this.DAO.prototype.defaultValues)

return this
}

DAOFactory.prototype.refreshAttributes = function() {
var self = this
, attributeManipulation = {};

Utils._.each(['get', 'set'], function(type) {
var opt = type + 'terMethods'
Expand Down Expand Up @@ -173,36 +201,17 @@ module.exports = (function() {
})

Utils._.each(funcs, function(fct, name) {
if (!attributeManipulation[name]) attributeManipulation[name] = {}
if (!attributeManipulation[name]) {
attributeManipulation[name] = {
configurable: true
}
}
attributeManipulation[name][type] = fct
})
})

Object.defineProperties(this.DAO.prototype, attributeManipulation)

this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes);
this.DAO.prototype.booleanValues = []
this.DAO.prototype.defaultValues = {}
this.DAO.prototype.validators = {}

Utils._.each(this.rawAttributes, function (definition, name) {
if (((definition === DataTypes.BOOLEAN) || (definition.type === DataTypes.BOOLEAN))) {
self.DAO.prototype.booleanValues.push(name);
}
if (definition.hasOwnProperty('defaultValue')) {
self.DAO.prototype.defaultValues[name] = Utils._.partial(
Utils.toDefaultValue, definition.defaultValue)
}

if (definition.hasOwnProperty('validate')) {
self.DAO.prototype.validators[name] = definition.validate;
}
})

this.DAO.prototype.__factory = this
this.DAO.prototype.hasDefaultValues = !Utils._.isEmpty(this.DAO.prototype.defaultValues)

return this
this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes)
}

DAOFactory.prototype.sync = function(options) {
Expand Down
2 changes: 1 addition & 1 deletion test/dao-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.find({ limit: 10 }).success(function(user) {
// it returns an object instead of an array
expect(Array.isArray(user)).to.not.be.ok
expect(user.hasOwnProperty('username')).to.be.ok
expect(user.dataValues.hasOwnProperty('username')).to.be.ok
done()
})
})
Expand Down

0 comments on commit a46121c

Please sign in to comment.