From 6827b715bc0246e7d33d99d8f9629da914fbdd77 Mon Sep 17 00:00:00 2001 From: Jaka Hudoklin Date: Fri, 22 Aug 2014 15:39:53 +0200 Subject: [PATCH 1/2] Enable remoting for embedsOne relation Signed-off-by: Jaka Hudoklin --- lib/models/model.js | 9 ++++-- test/relations.integration.js | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/lib/models/model.js b/lib/models/model.js index 6694ac55..a06e247f 100644 --- a/lib/models/model.js +++ b/lib/models/model.js @@ -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' || diff --git a/test/relations.integration.js b/test/relations.integration.js index c0e2d28c..ceacfe8e 100644 --- a/test/relations.integration.js +++ b/test/relations.integration.js @@ -492,6 +492,65 @@ describe('relations - integration', function () { }); }); }); + + describe('embedsOne', function() { + + before(function defineGroupAndImageModels() { + var group = app.model( + 'group', + { properties: { name: 'string' }, + dataSource: 'db', + plural: 'groups' + } + ); + var image = app.model( + 'image', + { properties: { url: 'string' }, dataSource: 'db' } + ); + group.embedsOne(image, { as: 'cover' }); + }); + + before(function createImage(done) { + var test = this; + app.models.group.create({ name: 'Group 1' }, + function(err, list) { + if (err) return done(err); + test.group = group; + group.image.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.imageItem).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() { From b5b0ecd31971d2cc446f96347f0043cbd5681f11 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Fri, 22 Aug 2014 17:05:11 +0200 Subject: [PATCH 2/2] Fix tests --- test/relations.integration.js | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test/relations.integration.js b/test/relations.integration.js index ceacfe8e..4f8677d6 100644 --- a/test/relations.integration.js +++ b/test/relations.integration.js @@ -494,8 +494,8 @@ describe('relations - integration', function () { }); describe('embedsOne', function() { - - before(function defineGroupAndImageModels() { + + before(function defineGroupAndPosterModels() { var group = app.model( 'group', { properties: { name: 'string' }, @@ -503,53 +503,53 @@ describe('relations - integration', function () { plural: 'groups' } ); - var image = app.model( - 'image', + var poster = app.model( + 'poster', { properties: { url: 'string' }, dataSource: 'db' } ); - group.embedsOne(image, { as: 'cover' }); + group.embedsOne(poster, { as: 'cover' }); }); - + before(function createImage(done) { var test = this; app.models.group.create({ name: 'Group 1' }, - function(err, list) { + function(err, group) { if (err) return done(err); test.group = group; - group.image.build({ url: 'http://image.url' }); + 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.imageItem).to.be.eql([ + 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' }, - ]); + expect(res.body).to.be.eql( + { url: 'http://image.url' } + ); done(); }); }); - + }); describe('embedsMany', function() { @@ -1018,7 +1018,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',