factory-girl
Advanced tools
Comparing version 2.3.0 to 3.0.0
45
index.js
@@ -182,8 +182,20 @@ /* global window, define */ | ||
builder.build(name, attrs, function(err, doc) { | ||
builder._build(name, attrs, function(err, doc, resultingAttrs) { | ||
if (err) return callback(err); | ||
save(name, doc, callback); | ||
save(name, doc, resultingAttrs, callback); | ||
}); | ||
}; | ||
// Shrink last parameter from callback | ||
// for more comfortable work with promises | ||
function wrapCallback(callback) { | ||
if (callback) { | ||
var oldCallback = callback; | ||
return function(error, doc, attrs) { | ||
return oldCallback(error, doc); | ||
} | ||
} | ||
} | ||
builder.build = function(name, attrs, callback) { | ||
@@ -194,3 +206,6 @@ if (typeof attrs === 'function') { | ||
} | ||
return this._build(name, attrs, wrapCallback(callback)); | ||
}; | ||
builder._build = function(name, attrs, callback) { | ||
if (!factories[name]) { | ||
@@ -227,5 +242,5 @@ return callback(new Error("No factory defined for model '" + name + "'")); | ||
factories[name].options.afterBuild.call( | ||
this, doc, builder.options, callback); | ||
builder, doc, attrs, callback); | ||
} else { | ||
callback(null, doc); | ||
callback(null, doc, attrs); | ||
} | ||
@@ -264,6 +279,6 @@ }); | ||
callback = args.callback; | ||
args.callback = function(err, docs) { | ||
args.callback = function(err, docs, resultingAttrsArray) { | ||
if (err) return callback(err); | ||
asyncForEach(docs, function(doc, cb, index) { | ||
save(name, doc, function(err) { | ||
save(name, doc, resultingAttrsArray[index], function(err) { | ||
if (!err) results[index] = doc; | ||
@@ -280,10 +295,14 @@ cb(err); | ||
function buildMany(args) { | ||
var results = []; | ||
var results = [], | ||
resultingAttrsArray = []; | ||
asyncForEach(args.attrsArray, function(attrs, cb, index) { | ||
builder.build(args.name, attrs, function(err, doc) { | ||
if (!err) results[index] = doc; | ||
builder._build(args.name, attrs, function(err, doc, resultingAttrs) { | ||
if (!err) { | ||
results[index] = doc; | ||
resultingAttrsArray[index] = resultingAttrs; | ||
} | ||
cb(err); | ||
}); | ||
}, function(err) { | ||
args.callback(err, results); | ||
args.callback(err, results, resultingAttrsArray); | ||
}); | ||
@@ -319,7 +338,7 @@ } | ||
num: num, | ||
callback: callback | ||
callback: wrapCallback(callback) | ||
}; | ||
} | ||
function save(name, doc, callback) { | ||
function save(name, doc, resultingAttrs, callback) { | ||
var model = factories[name].model; | ||
@@ -330,3 +349,3 @@ factory.adapterFor(name).save(doc, model, function (err) { | ||
if (factories[name].options.afterCreate) { | ||
factories[name].options.afterCreate.call(this, doc, builder.options, callback); | ||
factories[name].options.afterCreate.call(builder, doc, resultingAttrs, callback); | ||
} | ||
@@ -333,0 +352,0 @@ else { |
@@ -6,3 +6,3 @@ { | ||
"author": "Simon Wade", | ||
"version": "2.3.0", | ||
"version": "3.0.0", | ||
"keywords": [ | ||
@@ -9,0 +9,0 @@ "factory", |
@@ -75,10 +75,29 @@ # factory-girl | ||
#### `afterBuild: function(instance, options, callback)` | ||
#### `afterBuild: function(instance, attrs, callback)` | ||
Provides a function that is called after the model is built. | ||
#### `afterCreate: function(instance, options, callback)` | ||
#### `afterCreate: function(instance, attrs, callback)` | ||
Provides a function that is called after a new model instance is saved. | ||
```javascript | ||
factory.define('user', User, { | ||
foo: 'bar' | ||
}, { | ||
afterCreate: function(instance, options, callback) { | ||
generateBazBasedOnID(instance.id, function(error, generatedBaz) { | ||
if(error) { | ||
callback(error, null); | ||
} else { | ||
instance.baz = generatedBaz; | ||
callback(null, instance); | ||
} | ||
}); | ||
} | ||
}); | ||
``` | ||
Other builder options can be accessed, inside hooks, using `this.options`. | ||
## Defining Associations | ||
@@ -85,0 +104,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
48664
10
1006
240