diff --git a/lib/model-builder.js b/lib/model-builder.js index a688004d..7d8c1770 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -110,7 +110,17 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett var args = slice.call(arguments); var pluralName = (settings && settings.plural) || inflection.pluralize(className); - + + var pathName = (settings && settings.path) || pluralName; + var normalize = ModelBuilder.normalizePathName === true || (settings && settings.normalize); + + if (this.normalizePathName || (normalize && this.normalizePathName !== false)) { + pathName = inflection.transform(pathName, ['underscore', 'dasherize']); + } else if (typeof ModelBuilder.normalizePathName === 'function' && + this.normalizePathName !== false) { + pathName = ModelBuilder.normalizePathName.apply(this, [pathName].concat(args)); + } + if (!className) { throw new Error('Class name required'); } @@ -188,7 +198,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett hiddenProperty(ModelClass, 'dataSource', modelBuilder); // Keep for back-compatibility hiddenProperty(ModelClass, 'pluralModelName', pluralName); hiddenProperty(ModelClass, 'relations', {}); - hiddenProperty(ModelClass, 'http', { path: '/' + pluralName }); + hiddenProperty(ModelClass, 'http', { path: '/' + pathName }); hiddenProperty(ModelClass, 'base', ModelBaseClass); // inherit ModelBaseClass static methods diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index a6f840c3..0e1244eb 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -548,6 +548,28 @@ describe('DataSource define model', function () { done(); }); + + it('should allow an explicit remoting path', function () { + var ds = new DataSource('memory'); + + var User = ds.define('User', {name: String, bio: String}, { path: 'accounts' }); + User.http.path.should.equal('/accounts'); + }); + + it('should normalize the remoting path - option', function () { + var ds = new DataSource('memory'); + + var User = ds.define('UserAccount', {name: String, bio: String}, { normalize: true }); + User.http.path.should.equal('/user-accounts'); + }); + + it('should normalize the remoting path - modelBuilder', function () { + var ds = new DataSource('memory'); + ds.modelBuilder.normalizePathName = true; + + var User = ds.define('UserAccount', {name: String, bio: String}); + User.http.path.should.equal('/user-accounts'); + }); });