factory-girl
Advanced tools
Comparing version 3.0.1 to 3.1.0
39
index.js
@@ -43,2 +43,3 @@ /* global window, define */ | ||
factory.withOptions = builderProxy('withOptions'); | ||
factory.attrs = builderProxy('attrs'); | ||
factory.build = builderProxy('build'); | ||
@@ -137,2 +138,3 @@ factory.buildSync = builderProxy('buildSync'); | ||
promisified.withOptions = promisifiedBuilderProxy('withOptions'); | ||
promisified.attrs = promisifiedBuilderProxy('attrs'); | ||
promisified.build = promisifiedBuilderProxy('build'); | ||
@@ -164,2 +166,3 @@ promisified.buildMany = promisifiedBuilderProxy('buildMany'); | ||
builder.promisify = function(promisify) { | ||
builder.attrs = promisify(builder.attrs); | ||
builder.build = promisify(builder.build); | ||
@@ -203,3 +206,3 @@ builder.create = promisify(builder.create); | ||
builder.build = function(name, attrs, callback) { | ||
builder.attrs = function(name, attrs, callback) { | ||
if (typeof attrs === 'function') { | ||
@@ -209,10 +212,6 @@ callback = attrs; | ||
} | ||
return builder._build(name, attrs, wrapCallback(callback)); | ||
}; | ||
builder._build = function(name, attrs, callback) { | ||
if (!factories[name]) { | ||
return callback(new Error("No factory defined for model '" + name + "'")); | ||
} | ||
var model = factories[name].model; | ||
attrs = merge(copy(factories[name].attributes), attrs); | ||
@@ -240,10 +239,32 @@ | ||
if (err) return callback(err); | ||
callback(null, attrs); | ||
}); | ||
}; | ||
builder.build = function(name, attrs, callback) { | ||
if (typeof attrs === 'function') { | ||
callback = attrs; | ||
attrs = {}; | ||
} | ||
return builder._build(name, attrs, wrapCallback(callback)); | ||
}; | ||
builder._build = function(name, attrs, callback) { | ||
if (!factories[name]) { | ||
return callback(new Error("No factory defined for model '" + name + "'")); | ||
} | ||
var model = factories[name].model; | ||
builder.attrs(name, attrs, function(err, resultingAttrs) { | ||
if (err) return callback(err); | ||
var adapter = factory.adapterFor(name), | ||
doc = adapter.build(model, attrs); | ||
doc = adapter.build(model, resultingAttrs); | ||
if (factories[name].options.afterBuild) { | ||
factories[name].options.afterBuild.call( | ||
builder, doc, attrs, callback); | ||
builder, doc, resultingAttrs, callback); | ||
} else { | ||
callback(null, doc, attrs); | ||
callback(null, doc, resultingAttrs); | ||
} | ||
@@ -250,0 +271,0 @@ }); |
@@ -6,3 +6,3 @@ { | ||
"author": "Simon Wade", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"keywords": [ | ||
@@ -9,0 +9,0 @@ "factory", |
@@ -87,3 +87,3 @@ # factory-girl | ||
}, { | ||
afterCreate: function(instance, options, callback) { | ||
afterCreate: function(instance, attrs, callback) { | ||
generateBazBasedOnID(instance.id, function(error, generatedBaz) { | ||
@@ -138,2 +138,18 @@ if(error) { | ||
### Factory#attrs | ||
Generates and returns attrs. | ||
```javascript | ||
factory.attrs('post', function(err, postAttrs) { | ||
// postAttrs is a post attributes | ||
console.log(postAttrs); | ||
// => {title: 'Hello', authorEmail: 'user1@demo.com'} | ||
}); | ||
factory.attrs('post', {title: 'Foo', content: 'Bar'}, function(err, postAttrs) { | ||
// build post attrs and override title and content | ||
}); | ||
``` | ||
### Factory#build | ||
@@ -140,0 +156,0 @@ |
@@ -80,2 +80,41 @@ /* global describe, beforeEach, afterEach */ | ||
describe('#attrs', function() { | ||
it('correctly generates attrs', function(done) { | ||
factory.attrs('job', function(err, jobAttrs) { | ||
jobAttrs.should.eql({ | ||
title: 'Engineer', | ||
company: 'Foobar Inc.', | ||
duties: { | ||
cleaning: false, | ||
writing: true, | ||
computing: true | ||
} | ||
}); | ||
done(); | ||
}); | ||
}); | ||
it('correctly overrides attrs', function(done) { | ||
var overrides = { title: 'Developer' }; | ||
factory.attrs('job', overrides, function(err, jobAttrs) { | ||
jobAttrs.title.should.eql(overrides.title); | ||
jobAttrs.company.should.eql('Foobar Inc.'); | ||
done(); | ||
}); | ||
}); | ||
it('correctly handles nested attributes', function(done) { | ||
factory.attrs('company', function(err, companyAttrs) { | ||
companyAttrs.employees.should.be.instanceof(Array); | ||
companyAttrs.employees.length.should.eql(3); | ||
companyAttrs.managers.should.be.instanceof(Array); | ||
companyAttrs.managers.length.should.eql(2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#build', function() { | ||
@@ -82,0 +121,0 @@ it('builds, but does not save the object', function(done) { |
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
51107
1055
270