Merge pull request #96 from strongloop/feature/remember-scope-target-class
Remember scope target class, improved description of scope methods
This commit is contained in:
commit
f368c96612
11
lib/scope.js
11
lib/scope.js
|
@ -1,3 +1,4 @@
|
||||||
|
var i8n = require('inflection');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var defineCachedRelations = utils.defineCachedRelations;
|
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._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.build = build;
|
||||||
f.create = create;
|
f.create = create;
|
||||||
|
@ -116,7 +121,7 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
fn.shared = true;
|
fn.shared = true;
|
||||||
fn.http = {verb: 'get', path: '/' + name};
|
fn.http = {verb: 'get', path: '/' + name};
|
||||||
fn.accepts = {arg: 'filter', type: 'object'};
|
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};
|
fn.returns = {arg: name, type: 'array', root: true};
|
||||||
|
|
||||||
cls['__get__' + name] = fn;
|
cls['__get__' + name] = fn;
|
||||||
|
@ -129,7 +134,7 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
fn_create.shared = true;
|
fn_create.shared = true;
|
||||||
fn_create.http = {verb: 'post', path: '/' + name};
|
fn_create.http = {verb: 'post', path: '/' + name};
|
||||||
fn_create.accepts = {arg: 'data', type: 'object', http: {source: 'body'}};
|
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};
|
fn_create.returns = {arg: 'data', type: 'object', root: true};
|
||||||
|
|
||||||
cls['__create__' + name] = fn_create;
|
cls['__create__' + name] = fn_create;
|
||||||
|
@ -140,7 +145,7 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
};
|
};
|
||||||
fn_delete.shared = true;
|
fn_delete.shared = true;
|
||||||
fn_delete.http = {verb: 'delete', path: '/' + name};
|
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};
|
fn_delete.returns = {arg: 'data', type: 'object', root: true};
|
||||||
|
|
||||||
cls['__delete__' + name] = fn_delete;
|
cls['__delete__' + name] = fn_delete;
|
||||||
|
|
|
@ -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.create(function (err, book) {
|
||||||
book.chapters.create({name: 'a'}, function () {
|
book.chapters.create({name: 'a'}, function () {
|
||||||
book.chapters.create({name: 'z'}, function () {
|
book.chapters.create({name: 'z'}, function () {
|
||||||
|
@ -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 () {
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue