Skip to content

Commit

Permalink
findOrCreate does not modify inputs
Browse files Browse the repository at this point in the history
This helps when making spy assertions about what arguments findOrCreate
was called with.
  • Loading branch information
elliotf committed Nov 25, 2013
1 parent d5c66c3 commit 9e0c5b1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,16 +567,21 @@ module.exports = (function() {
DAOFactory.prototype.findOrCreate = function (params, defaults) {
var self = this;

var where = {};
for (var attrname in params) {
where[attrname] = params[attrname]
}

return new Utils.CustomEventEmitter(function (emitter) {
self.find({
where: params
where: where
}).success(function (instance) {
if (instance === null) {
for (var attrname in defaults) {
params[attrname] = defaults[attrname]
where[attrname] = defaults[attrname]
}

self.create(params)
self.create(where)
.success(function (instance) {
emitter.emit('success', instance, true)
})
Expand Down

0 comments on commit 9e0c5b1

Please sign in to comment.