Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buildOptions support to buildSync #82

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ Be aware that `assoc()` is an async function, so it can't be used with `buildSyn

```javascript
var doc = factory.buildSync('post', {title: 'Foo'});

// or with buildOptions
var doc = factory.buildSync('post', { title: 'Foo' }, { veryLong: true });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code formatting is inconsistent with the line above.

```

### Factory#cleanup
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,16 @@
});
};

builder.buildSync = function (name, attrs) {
builder.buildSync = function (name, attrs, buildOptions) {
if (!factories[name]) {
throw new Error("No factory defined for model '" + name + "'");
}
var model = factories[name].model;
attrs = merge(copy(factories[name].attributes), attrs);
var attributes = factories[name].attributes;
if (typeof attributes === 'function') {
attributes = attributes.call(null, buildOptions || {});
}
attrs = merge(copy(attributes), attrs);
var names = keys(attrs);
for (var i = 0; i < names.length; i++) {
var key = names[i], fn = attrs[key];
Expand Down
6 changes: 6 additions & 0 deletions test/factory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,12 @@ describe('factory', function () {
});
});

it('allows the use of buildOptions', function() {
var user = factory.buildSync('user', {}, { facebookUser: true });
(user instanceof User).should.be.true;
user.facebook.id.should.exist;
});

it('allows synchronous function properties', function () {
var post = factory.buildSync('blogpost');
(post instanceof BlogPost).should.be.true;
Expand Down