Skip to content

Commit

Permalink
feat(ModelApi): makes identity do the pluralizing if plural is not de…
Browse files Browse the repository at this point in the history
…fined
  • Loading branch information
iobaixas committed May 7, 2015
1 parent 591db81 commit b94b349
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/module/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -229,17 +225,24 @@ 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.
* }
* });
* ```
*
* @return {boolean} If true, return plural name
* @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;
},

/**
Expand Down
15 changes: 15 additions & 0 deletions test/model-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit b94b349

Please sign in to comment.