From abf57ff4977fae20d8b1a15c101623dd3feed85b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 2 Apr 2014 19:32:37 +0200 Subject: [PATCH 1/3] scope: add _targetClass to scope property Store the class of the results returned by a scope find method. This class is different from the "targetClass" when the relations is using a third "through" model (e.g. hasAndBelongsToMany). --- lib/scope.js | 5 +++++ test/relations.test.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/scope.js b/lib/scope.js index da8aa1ad..3c205991 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -1,3 +1,4 @@ +var i8n = require('inflection'); var utils = require('./utils'); var defineCachedRelations = utils.defineCachedRelations; /** @@ -83,6 +84,10 @@ function defineScope(cls, targetClass, name, params, methods) { } }; f._scope = typeof params === 'function' ? params.call(this) : params; + f._targetClass = targetClass.modelName; + if (f._scope.collect) { + f._targetClass = i8n.capitalize(f._scope.collect); + } f.build = build; f.create = create; diff --git a/test/relations.test.js b/test/relations.test.js index 9955ada7..51452156 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -117,6 +117,10 @@ describe('relations', function () { }); } }); + + it('should set targetClass on scope property', function() { + should.equal(Book.prototype.chapters._targetClass, 'Chapter'); + }); }); describe('belongsTo', function () { @@ -248,6 +252,9 @@ describe('relations', function () { }); }); + it('should set targetClass on scope property', function() { + should.equal(Article.prototype.tags._targetClass, 'Tag'); + }); }); }); From 4da4c65faa13211489f4948ca1aa16862a23c87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 2 Apr 2014 19:35:14 +0200 Subject: [PATCH 2/3] Re-enable skipped test. test/relations: hasMany should fetch all scoped instances The test is passing with the current implementation. --- test/relations.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/relations.test.js b/test/relations.test.js index 51452156..7bc3a1b3 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -68,7 +68,7 @@ describe('relations', function () { }); }); - it.skip('should fetch all scoped instances', function (done) { + it('should fetch all scoped instances', function (done) { Book.create(function (err, book) { book.chapters.create({name: 'a'}, function () { book.chapters.create({name: 'z'}, function () { From d08c6714d81a1f0638f2c64794ed7306c7d02d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 2 Apr 2014 19:36:13 +0200 Subject: [PATCH 3/3] scope: improve description of shared methods The description is used by client SDK code-generators like loopback-angularjs. --- lib/scope.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index 3c205991..ac14efc7 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -121,7 +121,7 @@ function defineScope(cls, targetClass, name, params, methods) { fn.shared = true; fn.http = {verb: 'get', path: '/' + name}; fn.accepts = {arg: 'filter', type: 'object'}; - fn.description = 'Fetches ' + name; + fn.description = 'Queries ' + name + ' of this model.'; fn.returns = {arg: name, type: 'array', root: true}; cls['__get__' + name] = fn; @@ -134,7 +134,7 @@ function defineScope(cls, targetClass, name, params, methods) { fn_create.shared = true; fn_create.http = {verb: 'post', path: '/' + name}; fn_create.accepts = {arg: 'data', type: 'object', http: {source: 'body'}}; - fn_create.description = 'Creates ' + name; + fn_create.description = 'Creates a new instance in ' + name + ' of this model.'; fn_create.returns = {arg: 'data', type: 'object', root: true}; cls['__create__' + name] = fn_create; @@ -145,7 +145,7 @@ function defineScope(cls, targetClass, name, params, methods) { }; fn_delete.shared = true; fn_delete.http = {verb: 'delete', path: '/' + name}; - fn_delete.description = 'Deletes ' + name; + fn_delete.description = 'Deletes all ' + name + ' of this model.'; fn_delete.returns = {arg: 'data', type: 'object', root: true}; cls['__delete__' + name] = fn_delete;