From 28a661a81a75d0afb149777dcb816a2cf057d2de Mon Sep 17 00:00:00 2001 From: Khashayar Hajian Date: Wed, 17 Sep 2014 17:28:40 +0200 Subject: [PATCH 1/2] Test improvement, shows _targetClass camelCase bug There is an issue where setting _targetClass on hasAndBelongsToMany relations with a camel-case model name, fails. Signed-off-by: Khashayar Hajian --- test/relations.test.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/relations.test.js b/test/relations.test.js index b99f31ba..48173115 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1604,15 +1604,15 @@ describe('relations', function () { }); describe('hasAndBelongsToMany', function () { - var Article, Tag, ArticleTag; + var Article, TagName, ArticleTag; it('can be declared', function (done) { Article = db.define('Article', {title: String}); - Tag = db.define('Tag', {name: String}); - Article.hasAndBelongsToMany('tags'); - ArticleTag = db.models.ArticleTag; + TagName = db.define('TagName', {name: String}); + Article.hasAndBelongsToMany('tagNames'); + ArticleTag = db.models.ArticleTagName; db.automigrate(function () { Article.destroyAll(function () { - Tag.destroyAll(function () { + TagName.destroyAll(function () { ArticleTag.destroyAll(done) }); }); @@ -1621,11 +1621,11 @@ describe('relations', function () { it('should allow to create instances on scope', function (done) { Article.create(function (e, article) { - article.tags.create({name: 'popular'}, function (e, t) { - t.should.be.an.instanceOf(Tag); + article.tagNames.create({name: 'popular'}, function (e, t) { + t.should.be.an.instanceOf(TagName); ArticleTag.findOne(function (e, at) { should.exist(at); - at.tagId.toString().should.equal(t.id.toString()); + at.tagNameId.toString().should.equal(t.id.toString()); at.articleId.toString().should.equal(article.id.toString()); done(); }); @@ -1635,7 +1635,7 @@ describe('relations', function () { it('should allow to fetch scoped instances', function (done) { Article.findOne(function (e, article) { - article.tags(function (e, tags) { + article.tagNames(function (e, tags) { should.not.exist(e); should.exist(tags); done(); @@ -1645,12 +1645,12 @@ describe('relations', function () { it('should allow to add connection with instance', function (done) { Article.findOne(function (e, article) { - Tag.create({name: 'awesome'}, function (e, tag) { - article.tags.add(tag, function (e, at) { + TagName.create({name: 'awesome'}, function (e, tag) { + article.tagNames.add(tag, function (e, at) { should.not.exist(e); should.exist(at); at.should.be.an.instanceOf(ArticleTag); - at.tagId.should.equal(tag.id); + at.tagNameId.should.equal(tag.id); at.articleId.should.equal(article.id); done(); }); @@ -1660,12 +1660,12 @@ describe('relations', function () { it('should allow to remove connection with instance', function (done) { Article.findOne(function (e, article) { - article.tags(function (e, tags) { + article.tagNames(function (e, tags) { var len = tags.length; tags.should.not.be.empty; - article.tags.remove(tags[0], function (e) { + article.tagNames.remove(tags[0], function (e) { should.not.exist(e); - article.tags(true, function (e, tags) { + article.tagNames(true, function (e, tags) { tags.should.have.lengthOf(len - 1); done(); }); @@ -1675,7 +1675,7 @@ describe('relations', function () { }); it('should set targetClass on scope property', function() { - should.equal(Article.prototype.tags._targetClass, 'Tag'); + should.equal(Article.prototype.tagNames._targetClass, 'TagName'); }); }); From c3df5137128007c661619a30bfdd7cc1a76d97c8 Mon Sep 17 00:00:00 2001 From: Khashayar Hajian Date: Fri, 19 Sep 2014 16:23:39 +0200 Subject: [PATCH 2/2] Fix camel-case issue where relation is 'hasAndBelongsToMany' #304 Signed-off-by: Khashayar Hajian --- lib/scope.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/scope.js b/lib/scope.js index 5124251d..d8ebb7c3 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -162,7 +162,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { f._targetClass = targetModel.modelName; if (f._scope.collect) { - f._targetClass = i8n.capitalize(f._scope.collect); + f._targetClass = i8n.camelize(f._scope.collect); } f.build = build;