Refactor codes into same if condition
Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
parent
902885c3f8
commit
26547f8461
|
@ -636,9 +636,10 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
|
||||||
|
|
||||||
definition.applyScope(this, filter);
|
definition.applyScope(this, filter);
|
||||||
|
|
||||||
// find corresponding belongsTo relations from through model as include
|
|
||||||
var include = [];
|
|
||||||
if (params.through) {
|
if (params.through) {
|
||||||
|
|
||||||
|
// find corresponding belongsTo relations from through model as include
|
||||||
|
var include = [];
|
||||||
Object.keys(params.through.relations).forEach(function (rn) {
|
Object.keys(params.through.relations).forEach(function (rn) {
|
||||||
var relation = params.through.relations[rn];
|
var relation = params.through.relations[rn];
|
||||||
|
|
||||||
|
@ -653,17 +654,17 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (params.polymorphic && params.invert) {
|
||||||
|
filter.where[discriminator] = modelTo.modelName; // overwrite
|
||||||
|
filter.collect = params.polymorphic;
|
||||||
|
filter.include = filter.collect;
|
||||||
|
} else {
|
||||||
|
filter.collect = i8n.camelize(modelTo.modelName, true);
|
||||||
|
filter.include = include.join();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.through && params.polymorphic && params.invert) {
|
|
||||||
filter.where[discriminator] = modelTo.modelName; // overwrite
|
|
||||||
filter.collect = params.polymorphic;
|
|
||||||
filter.include = filter.collect;
|
|
||||||
} else if (params.through) {
|
|
||||||
filter.collect = i8n.camelize(modelTo.modelName, true);
|
|
||||||
filter.include = include;
|
|
||||||
}
|
|
||||||
|
|
||||||
return filter;
|
return filter;
|
||||||
}, scopeMethods, definition.options);
|
}, scopeMethods, definition.options);
|
||||||
|
|
||||||
|
|
|
@ -263,7 +263,7 @@ describe('relations', function () {
|
||||||
should.exist(physician);
|
should.exist(physician);
|
||||||
var scope = physician.patients._scope;
|
var scope = physician.patients._scope;
|
||||||
scope.should.have.property('collect', 'patient');
|
scope.should.have.property('collect', 'patient');
|
||||||
scope.should.have.property('include').eql(['patient']);
|
scope.should.have.property('include', 'patient');
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -473,7 +473,6 @@ describe('relations', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('hasMany through with custom relation name', function () {
|
describe('hasMany through with custom relation name', function () {
|
||||||
var Physician, Patient, Appointment, Address;
|
var Physician, Patient, Appointment, Address;
|
||||||
|
@ -507,7 +506,7 @@ describe('relations', function () {
|
||||||
should.exist(physician);
|
should.exist(physician);
|
||||||
var scope = physician.xxx._scope;
|
var scope = physician.xxx._scope;
|
||||||
scope.should.have.property('collect', 'patient');
|
scope.should.have.property('collect', 'patient');
|
||||||
scope.should.have.property('include').eql(['bar']);
|
scope.should.have.property('include', 'bar');
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -560,7 +559,7 @@ describe('relations', function () {
|
||||||
should.exist(physician);
|
should.exist(physician);
|
||||||
var scope = physician.xxx._scope;
|
var scope = physician.xxx._scope;
|
||||||
scope.should.have.property('collect', 'patient');
|
scope.should.have.property('collect', 'patient');
|
||||||
scope.should.have.property('include').eql(['bar']);
|
scope.should.have.property('include', 'bar');
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -594,7 +593,7 @@ describe('relations', function () {
|
||||||
should.exist(physician);
|
should.exist(physician);
|
||||||
var scope = physician.xxx._scope;
|
var scope = physician.xxx._scope;
|
||||||
scope.should.have.property('collect', 'patient');
|
scope.should.have.property('collect', 'patient');
|
||||||
scope.should.have.property('include').eql(['bar', 'car']);
|
scope.should.have.property('include', 'bar,car');
|
||||||
done(err);
|
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) {
|
User.create(function (err, user) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(user);
|
should.exist(user);
|
||||||
var scope1 = user.followers._scope;
|
var scope1 = user.followers._scope;
|
||||||
scope1.should.have.property('collect', 'user');
|
scope1.should.have.property('collect', 'user');
|
||||||
scope1.should.have.property('include').eql(['follower']);
|
scope1.should.have.property('include', 'follower');
|
||||||
var scope2 = user.following._scope;
|
var scope2 = user.following._scope;
|
||||||
scope2.should.have.property('collect', 'user');
|
scope2.should.have.property('collect', 'user');
|
||||||
scope2.should.have.property('include').eql(['followee']);
|
scope2.should.have.property('include', 'followee');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue