Merge pull request #504 from fabien/feature/embeds-one-remoting

Enable remoting for embedsOne relation
This commit is contained in:
Raymond Feng 2014-08-29 09:30:49 -07:00
commit 9b97014b52
2 changed files with 66 additions and 4 deletions

View File

@ -192,9 +192,12 @@ Model.setup = function () {
for (var relationName in relations) {
var relation = relations[relationName];
if (relation.type === 'belongsTo') {
ModelCtor.belongsToRemoting(relationName, relation, define)
} else if (relation.type === 'hasOne') {
ModelCtor.hasOneRemoting(relationName, relation, define)
ModelCtor.belongsToRemoting(relationName, relation, define);
} else if (
relation.type === 'hasOne' ||
relation.type === 'embedsOne'
) {
ModelCtor.hasOneRemoting(relationName, relation, define);
} else if (
relation.type === 'hasMany' ||
relation.type === 'embedsMany' ||

View File

@ -534,6 +534,65 @@ describe('relations - integration', function () {
});
});
});
describe('embedsOne', function() {
before(function defineGroupAndPosterModels() {
var group = app.model(
'group',
{ properties: { name: 'string' },
dataSource: 'db',
plural: 'groups'
}
);
var poster = app.model(
'poster',
{ properties: { url: 'string' }, dataSource: 'db' }
);
group.embedsOne(poster, { as: 'cover' });
});
before(function createImage(done) {
var test = this;
app.models.group.create({ name: 'Group 1' },
function(err, group) {
if (err) return done(err);
test.group = group;
group.cover.build({ url: 'http://image.url' });
group.save(done);
});
});
after(function(done) {
this.app.models.group.destroyAll(done);
});
it('includes the embedded models', function(done) {
var url = '/api/groups/' + this.group.id;
this.get(url)
.expect(200, function(err, res) {
expect(res.body.name).to.be.equal('Group 1');
expect(res.body.poster).to.be.eql(
{ url: 'http://image.url' }
);
done();
});
});
it('returns the embedded model', function(done) {
var url = '/api/groups/' + this.group.id + '/cover';
this.get(url)
.expect(200, function(err, res) {
expect(res.body).to.be.eql(
{ url: 'http://image.url' }
);
done();
});
});
});
describe('embedsMany', function() {
@ -1001,7 +1060,7 @@ describe('relations - integration', function () {
describe('nested relations', function() {
before(function defineProductAndCategoryModels() {
before(function defineModels() {
var Book = app.model(
'Book',
{ properties: { name: 'string' }, dataSource: 'db',