From 26547f84619d63b3d6dcfb114731d4f5cbcae528 Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Wed, 3 Sep 2014 12:31:09 +0800 Subject: [PATCH] Refactor codes into same if condition Signed-off-by: Clark Wang --- lib/relation-definition.js | 23 ++++++++++++----------- test/relations.test.js | 15 +++++++-------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 953a033f..d2850498 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -636,9 +636,10 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) { definition.applyScope(this, filter); - // find corresponding belongsTo relations from through model as include - var include = []; if (params.through) { + + // find corresponding belongsTo relations from through model as include + var include = []; Object.keys(params.through.relations).forEach(function (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; }, scopeMethods, definition.options); diff --git a/test/relations.test.js b/test/relations.test.js index 72933329..942c1609 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -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); }); }); @@ -473,7 +473,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(); }); });