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

Commit f66efc26 by Mick Hansen

Set a default as on belongs-to if not defined

1 parent b0c7a448
......@@ -3,10 +3,10 @@ var Utils = require("./../utils")
, Helpers = require('./helpers')
module.exports = (function() {
var BelongsTo = function(srcDAO, targetDAO, options) {
var BelongsTo = function(source, target, options) {
this.associationType = 'BelongsTo'
this.source = srcDAO
this.target = targetDAO
this.source = source
this.target = target
this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName)
......@@ -16,9 +16,13 @@ module.exports = (function() {
this.options.useHooks = options.useHooks
if (!this.options.as) {
this.options.as = Utils.singularize(this.target.tableName, this.target.options.language)
}
this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName)
: this.options.as || this.target.tableName
? Utils.combineTableNames(this.target.tableName, this.options.as)
: this.options.as
}
// the id is in the source table
......@@ -40,7 +44,7 @@ module.exports = (function() {
BelongsTo.prototype.injectGetter = function(obj) {
var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName, this.target.options.language)))
, accessor = Utils._.camelize('get_' + this.options.as)
, primaryKeys = Object.keys(self.target.primaryKeys)
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
......@@ -69,7 +73,7 @@ module.exports = (function() {
BelongsTo.prototype.injectSetter = function(obj) {
var self = this
, accessor = Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName, this.target.options.language)))
, accessor = Utils._.camelize('set_' + this.options.as)
obj[accessor] = function(associatedObject, options) {
var primaryKeys = !!associatedObject && !!associatedObject.daoFactory ? Object.keys(associatedObject.daoFactory.primaryKeys) : []
......
......@@ -16,7 +16,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
Group.belongsTo(User, { foreignKey: 'primaryGroupId', as: 'primaryUsers' })
Group.belongsTo(User, { foreignKey: 'secondaryGroupId', as: 'secondaryUsers' })
expect(Object.keys(Group.associations)).to.deep.equal(['Users', 'primaryUsers', 'secondaryUsers'])
expect(Object.keys(Group.associations)).to.deep.equal(['User', 'primaryUsers', 'secondaryUsers'])
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!