factory-girl
Advanced tools
Comparing version 1.4.1 to 1.5.0
21
index.js
@@ -43,2 +43,23 @@ (function() { | ||
factory.assocMany = function(name, key, num, attrsArray) { | ||
if (arguments.length < 4) { | ||
if (typeof key === 'number') { | ||
attrsArray = num; | ||
num = key; | ||
key = null; | ||
} | ||
} | ||
return function(callback) { | ||
factory.createMany(name, attrsArray, num, function(err, docs) { | ||
if (err) return callback(err); | ||
if (key) { | ||
for (var i = 0; i < docs.length; ++i) { | ||
docs[i] = docs[i][key]; | ||
} | ||
} | ||
callback(null, docs); | ||
}); | ||
}; | ||
}; | ||
factory.adapterFor = function(name) { | ||
@@ -45,0 +66,0 @@ return adapters[name] || defaultAdapter; |
@@ -6,3 +6,3 @@ { | ||
"author": "Simon Wade", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"keywords": [ | ||
@@ -9,0 +9,0 @@ "factory", |
@@ -45,2 +45,6 @@ # factory-girl | ||
factory.define('comment', Comment, { | ||
text: 'hello' | ||
}); | ||
factory.define('post', Post, { | ||
@@ -50,2 +54,4 @@ // create associations using factory.assoc(model, key) | ||
user_id: factory.assoc('user', 'id'), | ||
// create array of associations using factory.assocMany(model, key, num) | ||
comments: factory.assocMany('comment', 'text', 2) | ||
subject: 'Hello World', | ||
@@ -58,3 +64,4 @@ // you can refer to other attributes using `this` | ||
factory.build('post', function(err, post) { | ||
console.log(post.attributes); // => {user_id: 1, subject: 'Hello World', slug: 'hello-world'} | ||
console.log(post.attributes); // => {user_id: 1, comments: [{ text: 'hello' }, { text: 'hello' }], | ||
// subject: 'Hello World', slug: 'hello-world'} | ||
)); | ||
@@ -61,0 +68,0 @@ ``` |
@@ -27,2 +27,5 @@ /* global describe, beforeEach, afterEach */ | ||
Job.prototype = new Model(); | ||
Company = function() {}; | ||
Company.prototype = new Model(); | ||
}); | ||
@@ -50,2 +53,8 @@ | ||
}); | ||
factory.define('company', Company, { | ||
name: 'Fruit Company', | ||
employees: factory.assocMany('person', 3), | ||
managers: factory.assocMany('person', 'name', 2) | ||
}); | ||
}); | ||
@@ -91,2 +100,28 @@ | ||
}); | ||
context('factory containing a multi association', function() { | ||
it('is able to handle that', function(done){ | ||
factory.build('company', function(err, company) { | ||
(company instanceof Company).should.be.true; | ||
company.should.not.have.property('saveCalled'); | ||
company.name.should.eql('Fruit Company'); | ||
company.should.have.property('employees'); | ||
(company.employees instanceof Array).should.be.true; | ||
company.employees.length.should.eql(3); | ||
(company.employees[0] instanceof Person).should.be.true; | ||
company.employees[0].name.should.eql('Person 2'); | ||
(company.employees[1] instanceof Person).should.be.true; | ||
company.employees[1].name.should.eql('Person 3'); | ||
(company.employees[2] instanceof Person).should.be.true; | ||
company.employees[2].name.should.eql('Person 4'); | ||
(company.managers instanceof Array).should.be.true; | ||
company.managers.length.should.eql(2); | ||
(company.managers[0] instanceof Person).should.be.false; | ||
company.managers[0].should.eql('Person 5'); | ||
(company.managers[1] instanceof Person).should.be.false; | ||
company.managers[1].should.eql('Person 6'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -93,0 +128,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120301
889
141