diff --git a/src/module/factory.js b/src/module/factory.js index 718f037..ed0d538 100644 --- a/src/module/factory.js +++ b/src/module/factory.js @@ -32,14 +32,10 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils', // make sure the resource name and plural name are available if posible: - if(!config.name && _baseUrl) { + if(_baseUrl) { config.name = inflector.singularize(_baseUrl.replace(NAME_RGX, '$2')); } - if(!config.plural && config.name) { - config.plural = inflector.pluralize(config.name); - } - var Collection = Utils.buildArrayType(), List = Utils.buildArrayType(), Dummy = function(_asCollection) { @@ -229,9 +225,11 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils', * it should be manually set by writing the name and plural properties: * * ```javascript - * restmod.model(null, { - * __name__: 'resource', - * __plural__: 'resourciness' // set only if inflector cant properly gess the name. + * restmod.model().mix{ + * $config: { + * name: 'resource', + * plural: 'resourciness' // set only if inflector cant properly gess the name. + * } * }); * ``` * @@ -239,7 +237,12 @@ RMModule.factory('RMModelFactory', ['$injector', '$log', 'inflector', 'RMUtils', * @return {string} The base url. */ identity: function(_plural) { - return _plural ? config.plural : config.name; + if(!_plural) return config.name; + if(_plural) { + if(config.plural) return config.plural; + if(config.name) return inflector.pluralize(config.name); + } + return null; }, /** diff --git a/test/model-spec.js b/test/model-spec.js index 4deb994..534dbcc 100644 --- a/test/model-spec.js +++ b/test/model-spec.js @@ -76,6 +76,21 @@ describe('Restmod model class:', function() { }); }); + describe('identity', function() { + + it('should infer the model identity from the url', function() { + expect(Bike.identity()).toEqual('bike'); + }); + + it('should infer the model plural identity from the name', function() { + expect(Bike.identity(true)).toEqual('bikes'); + }); + + it('should use the specified plural identity if given', function() { + expect(Bike.mix({ $config: { plural: 'cletas' } }).identity(true)).toEqual('cletas'); + }); + }); + describe('dummy', function() { it('should create a new dummy resource with common api infrastructure', function() {