Skip to content

Commit

Permalink
Better normalization of passed-in association names
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jul 1, 2013
1 parent c798215 commit 15c506d
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var util = require('util') // Native Node util module
, Query
, query; // Lazy-load query; it depends on model/index

var _foreignKeyCreators = [];

utils.mixin(model, new (function () {

this.ModelBase = function () {};
Expand Down Expand Up @@ -935,6 +937,13 @@ utils.mixin(model, new (function () {
return this[name];
};

this.createForeignKeys = function () {
var creator;
while((creator = _foreignKeyCreators.pop())) {
creator();
}
};

})());

model.ModelDefinitionBase = function (name) {
Expand Down Expand Up @@ -993,8 +1002,8 @@ model.ModelDefinitionBase = function (name) {
, modelName = opts.model || name;

// Normalize inflection; we don't care which they use
assnName = utils.inflection.singularize(assnName);
modelName = utils.inflection.singularize(modelName);
assnName = utils.string.getInflection(assnName, 'constructor', 'singular');
modelName = utils.string.getInflection(modelName, 'constructor', 'singular');

assn[assnName] = {
model: modelName
Expand Down Expand Up @@ -1046,7 +1055,7 @@ model.ModelDefinitionBase = function (name) {
// Should probably listen for an event that signals
// base models are set up
if (!opts.through) {
setTimeout(createForeignKey(assnName), 0);
_foreignKeyCreators.push(createForeignKey(assnName));
}
};
});
Expand Down

0 comments on commit 15c506d

Please sign in to comment.