Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/scope.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var i8n = require('inflection');
var utils = require('./utils');
var defineCachedRelations = utils.defineCachedRelations;
/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -116,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;
Expand All @@ -129,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;
Expand All @@ -140,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;
Expand Down
9 changes: 8 additions & 1 deletion test/relations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -248,6 +252,9 @@ describe('relations', function () {
});
});

it('should set targetClass on scope property', function() {
should.equal(Article.prototype.tags._targetClass, 'Tag');
});
});

});