Refactor codes into same if condition

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
Clark Wang 2014-09-03 12:31:09 +08:00
parent 902885c3f8
commit 26547f8461
2 changed files with 19 additions and 19 deletions

View File

@ -636,9 +636,10 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
definition.applyScope(this, filter);
if (params.through) {
// find corresponding belongsTo relations from through model as include
var include = [];
if (params.through) {
Object.keys(params.through.relations).forEach(function (rn) {
var relation = params.through.relations[rn];
@ -653,15 +654,15 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
}
}
});
}
if (params.through && params.polymorphic && params.invert) {
if (params.polymorphic && params.invert) {
filter.where[discriminator] = modelTo.modelName; // overwrite
filter.collect = params.polymorphic;
filter.include = filter.collect;
} else if (params.through) {
} else {
filter.collect = i8n.camelize(modelTo.modelName, true);
filter.include = include;
filter.include = include.join();
}
}
return filter;

View File

@ -263,7 +263,7 @@ describe('relations', function () {
should.exist(physician);
var scope = physician.patients._scope;
scope.should.have.property('collect', 'patient');
scope.should.have.property('include').eql(['patient']);
scope.should.have.property('include', 'patient');
done(err);
});
});
@ -474,7 +474,6 @@ describe('relations', function () {
});
describe('hasMany through with custom relation name', function () {
var Physician, Patient, Appointment, Address;
@ -507,7 +506,7 @@ describe('relations', function () {
should.exist(physician);
var scope = physician.xxx._scope;
scope.should.have.property('collect', 'patient');
scope.should.have.property('include').eql(['bar']);
scope.should.have.property('include', 'bar');
done(err);
});
});
@ -560,7 +559,7 @@ describe('relations', function () {
should.exist(physician);
var scope = physician.xxx._scope;
scope.should.have.property('collect', 'patient');
scope.should.have.property('include').eql(['bar']);
scope.should.have.property('include', 'bar');
done(err);
});
});
@ -594,7 +593,7 @@ describe('relations', function () {
should.exist(physician);
var scope = physician.xxx._scope;
scope.should.have.property('collect', 'patient');
scope.should.have.property('include').eql(['bar', 'car']);
scope.should.have.property('include', 'bar,car');
done(err);
});
});
@ -623,16 +622,16 @@ describe('relations', function () {
});
});
it('should have scope that includes', function (done) {
it('should have scope that includes corresponding relation name', function (done) {
User.create(function (err, user) {
should.not.exist(err);
should.exist(user);
var scope1 = user.followers._scope;
scope1.should.have.property('collect', 'user');
scope1.should.have.property('include').eql(['follower']);
scope1.should.have.property('include', 'follower');
var scope2 = user.following._scope;
scope2.should.have.property('collect', 'user');
scope2.should.have.property('include').eql(['followee']);
scope2.should.have.property('include', 'followee');
done();
});
});