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

Commit 66de46ae by Sascha Depold

fixed and refactored belongs-to

1 parent 7d0dbfef
...@@ -11,7 +11,7 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) { ...@@ -11,7 +11,7 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
BelongsTo.prototype.injectAttributes = function() { BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = this.target.identifierName this.identifier = this.options.foreignKey || Utils._.underscoredIf(this.target.tableName + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
...@@ -20,8 +20,9 @@ BelongsTo.prototype.injectAttributes = function() { ...@@ -20,8 +20,9 @@ BelongsTo.prototype.injectAttributes = function() {
BelongsTo.prototype.injectGetter = function(obj) { BelongsTo.prototype.injectGetter = function(obj) {
var self = this var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || this.target.tableName))
obj[Utils._.camelize('get_' + this.associationName)] = function() { obj[accessor] = function() {
var id = obj[self.identifier] var id = obj[self.identifier]
return self.target.find(id) return self.target.find(id)
} }
...@@ -31,8 +32,9 @@ BelongsTo.prototype.injectGetter = function(obj) { ...@@ -31,8 +32,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
BelongsTo.prototype.injectSetter = function(obj) { BelongsTo.prototype.injectSetter = function(obj) {
var self = this var self = this
, accessor = Utils._.camelize('set_' + (this.options.as || this.target.tableName))
obj[Utils._.camelize('set_' + this.associationName)] = function(associatedObject) { obj[accessor] = function(associatedObject) {
obj[self.identifier] = associatedObject ? associatedObject.id : null obj[self.identifier] = associatedObject ? associatedObject.id : null
return obj.save() return obj.save()
} }
......
...@@ -142,8 +142,4 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() { ...@@ -142,8 +142,4 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
return this.primaryKeyCount > 0 return this.primaryKeyCount > 0
}) })
ModelDefinition.prototype.__defineGetter__('identifierName', function() {
return Utils._.underscoredIf(this.tableName + "Id", this.options.underscored)
})
Utils._.map(require("./associations/mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct }) Utils._.map(require("./associations/mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct })
\ No newline at end of file
...@@ -13,36 +13,56 @@ module.exports = { ...@@ -13,36 +13,56 @@ module.exports = {
assert.eql(Task.attributes['User'+num+'Id'], "INT") assert.eql(Task.attributes['User'+num+'Id'], "INT")
}, },
'it should correctly add the foreign id with underscore': function() { 'it should correctly add the foreign id with underscore': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }, {underscored: true}) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }, {underscored: true})
Task.belongsTo('user', User) Task.belongsTo(User)
assert.eql(Task.attributes.user_id, "INT") assert.eql(Task.attributes['user'+num+'_id'], "INT")
},
'it should correctly add the foreign id if foreignKey is passed': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User, {foreignKey: 'person_id'})
assert.eql(Task.attributes['person_id'], "INT")
}, },
'it should define getter and setter': function() { 'it should define getter and setter': function() {
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User)
var t = Task.build({title: 'asd'})
assert.isDefined(t['setUser'+num])
assert.isDefined(t['getUser'+num])
},
'it should define getter and setter according to passed as option': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo('user', User) Task.belongsTo(User, {as: 'Person'})
var t = Task.build({title: 'asd'}) var t = Task.build({title: 'asd'})
assert.isDefined(t.setUser) assert.isDefined(t.setPerson)
assert.isDefined(t.getUser) assert.isDefined(t.getPerson)
}, },
'it should set the foreign id to null': function() { 'it should set the foreign id to null': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo('user', User) Task.belongsTo(User)
var t = Task.build({title: 'asd'}) var t = Task.build({title: 'asd'})
assert.isNull(t.userId) assert.isNull(t['User'+num+'Id'])
}, },
'it should set and get the correct object': function(exit) { 'it should set and get the correct object': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo('user', User) Task.belongsTo(User, {as: 'User'})
User.sync({force: true}).on('success', function() { User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() { Task.sync({force: true}).on('success', function() {
...@@ -63,7 +83,7 @@ module.exports = { ...@@ -63,7 +83,7 @@ module.exports = {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo('user', User) Task.belongsTo(User, {as: 'User'})
User.sync({force: true}).on('success', function() { User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() { Task.sync({force: true}).on('success', function() {
......
...@@ -5,7 +5,7 @@ var assert = require("assert") ...@@ -5,7 +5,7 @@ var assert = require("assert")
module.exports = { module.exports = {
'it should correctly add the foreign id': function() { 'it should correctly add the foreign id': function() {
var num = parseInt(Math.random() * 99999999) /* var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING }) var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
...@@ -49,6 +49,6 @@ module.exports = { ...@@ -49,6 +49,6 @@ module.exports = {
}) })
}) })
}) })
}) })*/
} }
} }
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!