Skip to content

Commit

Permalink
Merge pull request #43 from kurenai-ryu/master
Browse files Browse the repository at this point in the history
Fix named associations endpoints
  • Loading branch information
tommybananas authored Apr 29, 2019
2 parents 94ceca3 + d4c9fa0 commit 673e6f6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
17 changes: 9 additions & 8 deletions lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var Resource = function(options) {
this.attributes = getKeys(this.model.rawAttributes);
}
}

this.actions = options.actions;
this.endpoints = {
plural: options.endpoints[0],
Expand Down Expand Up @@ -227,23 +227,24 @@ function autoAssociate(resource) {

var subResourceName;
if (association.associationType === 'HasOne') {
subResourceName =
subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.singular.toLowerCase();
resource[subResourceName] = hasOneResource(Resource, resource, association);
} else if (association.associationType === 'HasMany') {
subResourceName =
subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.plural.toLowerCase();
resource[subResourceName] = hasManyResource(Resource, resource, association);
} else if (association.associationType === 'BelongsTo') {
subResourceName =
subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.singular.toLowerCase();
resource[subResourceName] = belongsToResource(Resource, resource, association);
} else if (association.associationType === 'BelongsToMany') {
subResourceName =
association.target.options.name.plural.toLowerCase();
resource[subResourceName] = belongsToManyResource(Resource, resource, association);
subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.plural.toLowerCase();
resource[subResourceName] = belongsToManyResource(Resource, resource, association);
}
//console.log("REsource.autoAsociate srn", subResourceName);
});
}

module.exports = Resource;
module.exports = Resource;
4 changes: 2 additions & 2 deletions lib/associations/belongs-to-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var _ = require('lodash');

module.exports = function(Resource, resource, association) {
// access points
var subResourceName =
association.target.options.name.plural.toLowerCase();
var subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.plural.toLowerCase();

// Find reverse association
var associationPaired;
Expand Down
4 changes: 2 additions & 2 deletions lib/associations/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module.exports = function(Resource, resource, association) {
// access points
var subResourceName =
association.target.options.name.singular.toLowerCase();
var subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.singular.toLowerCase();

var associatedResource = new Resource({
app: resource.app,
Expand Down
4 changes: 2 additions & 2 deletions lib/associations/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module.exports = function(Resource, resource, association) {
// access points
var subResourceName =
association.target.options.name.plural.toLowerCase();
var subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.plural.toLowerCase();

var associatedResource = new Resource({
app: resource.app,
Expand Down
4 changes: 2 additions & 2 deletions lib/associations/has-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module.exports = function(Resource, resource, association) {
// access points
var subResourceName =
association.target.options.name.singular.toLowerCase();
var subResourceName = association.as ? association.as.toLowerCase() :
association.target.options.name.singular.toLowerCase();

var associatedResource = new Resource({
app: resource.app,
Expand Down
48 changes: 48 additions & 0 deletions tests/associations/sub-resource-name.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var expect = require('chai').expect,
rest = require('../../lib'),
test = require('../support');

describe('Associations(named sub-resources)', function() {
describe('without using "as" to name an association', function() {
it('should name the sub-resources according to the model name', function(done) {
var Person = test.db.define('person', { name: { type: test.Sequelize.STRING } }, { underscored: true }),
Course = test.db.define('course', { name: { type: test.Sequelize.STRING } }, { underscored: true });
Course.hasMany(Person);
Course.hasOne(Person);
rest.initialize({
app: test.app,
sequelize: test.Sequelize
});
var resource = rest.resource({model: Course,
endpoints: ['/courses', '/courses/:id'],
associations: true});
expect(resource).to.not.have.property('students');
expect(resource).to.not.have.property('teacher');
expect(resource).to.have.property('people');
expect(resource).to.have.property('person');
done();
});
});
describe('using "as" to name an association', function() {
it('should name the sub-resources using the name provided by "as"', function(done) {
var Person = test.db.define('person', { name: { type: test.Sequelize.STRING } }, { underscored: true }),
Course = test.db.define('course', { name: { type: test.Sequelize.STRING } }, { underscored: true });
Course.hasMany(Person, { as: 'students' });
Course.hasOne(Person, { as: 'teacher' });
rest.initialize({
app: test.app,
sequelize: test.Sequelize
});
var resource = rest.resource({model: Course,
endpoints: ['/courses', '/courses/:id'],
associations: true});
expect(resource).to.have.property('students');
expect(resource).to.have.property('teacher');
expect(resource).to.not.have.property('people');
expect(resource).to.not.have.property('person');
done();
});
});
});

0 comments on commit 673e6f6

Please sign in to comment.