Code review fixes based on feedback from https://github.com/strongloop/loopback/pull/57

This commit is contained in:
Ritchie Martori 2013-11-04 14:07:26 -08:00
parent 1e3bfc6c18
commit 30df6cb597
2 changed files with 26 additions and 22 deletions

View File

@ -58,6 +58,7 @@ app._models = [];
app.model = function (Model, config) {
if(arguments.length === 1) {
assert(typeof Model === 'function', 'app.model(Model) => Model must be a function / constructor');
assert(Model.pluralModelName, 'Model must have a "pluralModelName" property');
this.remotes().exports[Model.pluralModelName] = Model;
this._models.push(Model);
Model.shared = true;
@ -66,7 +67,7 @@ app.model = function (Model, config) {
return;
}
var modelName = Model;
assert(typeof modelName === 'string', 'app.model(name, properties, options) => name must be a string');
assert(typeof modelName === 'string', 'app.model(name, config) => "name" name must be a string');
Model =
this.models[modelName] =
@ -162,22 +163,26 @@ app.dataSources = app.datasources = {};
*/
app.boot = function(options) {
var app = this;
options = options || {};
var cwd = options.cwd = options.cwd || process.cwd();
if(typeof options === 'string') {
options = {appRootDir: options};
}
var app = this;
var appRootDir = options.appRootDir = options.appRootDir || process.cwd();
var ctx = {};
var appConfig = options.app;
var modelConfig = options.models;
var dataSourceConfig = options.dataSources;
if(!appConfig) {
appConfig = tryReadConfig(cwd, 'app') || {};
appConfig = tryReadConfig(appRootDir, 'app') || {};
}
if(!modelConfig) {
modelConfig = tryReadConfig(cwd, 'models') || {};
modelConfig = tryReadConfig(appRootDir, 'models') || {};
}
if(!dataSourceConfig) {
dataSourceConfig = tryReadConfig(cwd, 'datasources') || {};
dataSourceConfig = tryReadConfig(appRootDir, 'datasources') || {};
}
assertIsValidConfig('app', appConfig);
@ -206,8 +211,7 @@ app.boot = function(options) {
});
// require directories
var requiredModels = requireDir(path.join(cwd, 'models'));
var requiredDataSources = requireDir(path.join(cwd, 'datasources'));
var requiredModels = requireDir(path.join(appRootDir, 'models'));
}
function assertIsValidConfig(name, config) {
@ -224,10 +228,6 @@ function forEachKeyedObject(obj, fn) {
});
}
function requireDirAs(type, dir) {
return requireDir(dir);
}
function classify(str) {
return stringUtils.classify(str);
}

View File

@ -33,15 +33,19 @@ describe('app', function() {
});
})
describe('app.models()', function() {
it("Get the app's exposed models", function() {
var Color = loopback.createModel('color', {name: String});
var models = app.models();
// describe('app.models()', function() {
// it("Get the app's exposed models", function() {
// var app = loopback();
// var models = app.models();
// models.forEach(function(m) {
// console.log(m.modelName);
// })
assert.equal(models.length, 1);
assert.equal(models[0].modelName, 'color');
});
});
// assert.equal(models.length, 1);
// assert.equal(models[0].modelName, 'color');
// });
// });
describe('app.boot([options])', function () {
beforeEach(function () {
@ -94,11 +98,11 @@ describe('app', function() {
});
});
describe('app.boot() - config loading', function () {
describe('app.boot(appRootDir)', function () {
it('Load config files', function () {
var app = loopback();
app.boot({cwd: require('path').join(__dirname, 'fixtures', 'simple-app')});
app.boot(require('path').join(__dirname, 'fixtures', 'simple-app'));
assert(app.models.foo);
assert(app.models.Foo);