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

Sequences #2

Closed
mgraham opened this issue May 5, 2014 · 5 comments
Closed

Sequences #2

mgraham opened this issue May 5, 2014 · 5 comments

Comments

@mgraham
Copy link

mgraham commented May 5, 2014

It would be nice to have FactoryGirl-style sequences.

In the meantime, I'm doing something like this:

FactoryGuy.define('user', {
  default: {
    name: 'Person 1',
    email: 'person_1@example.com'
  },
  sequence: {
    name: function(n)  { return 'Person ' + n },
    email: function(n) { return 'person_' + n + '@example.com' }
  },
});


// makeFixtureList
//
// - assumes a factory set up like this:
//         FactoryGuy.define('user', {
//           default: {
//             name: 'Person',
//             email: 'person@example.com'
//           },
//           sequence: {
//             name: function(n)  { return 'Person ' + n },
//             email: function(n) { return 'person_' + n + '@example.com' }
//           },
//         });
//     
//
//
// makeFixtureList(count, model, sequence_fixture_name = 'sequence')
// e.g.
//      makeFixtureList(10, 'user')   
//      - makes 10 users, using 'sequence' as the name generators, and 'default' to actually create the users
//      - the equvalent of calling:
//            store.makeFixture('user', { name: 'Person 1', email: 'person_1@example.com' })
//            store.makeFixture('user', { name: 'Person 2', email: 'person_2@example.com' })
//            ...
//            store.makeFixture('user', { name: 'Person 10', email: 'person_10@example.com' })
//
var makeFixtureList = function(count, model, sequence_fixture_name) {
  var store = App.__container__.lookup('store:main'));

  if (!sequence_fixture_name) sequence_fixture_name = 'sequence';

  var attributes_generators = FactoryGuy.getModelInfo(model)[sequence_fixture_name];

  for (var i = 1; i <= count; i++) {
    var attributes = {}; 
    for (var key in attributes_generators) {
      var func = attributes_generators[key];
        var text = func(i);
        attributes[key] = text;
    }

    store.makeFixture(model, attributes);
  }

}

@OpakAlex
Copy link
Contributor

OpakAlex commented May 5, 2014

@mgraham, yes, we can do this!
Thanks!

@danielspaniel
Copy link
Collaborator

@mgraham, can you fork the repo, add this feature with tests .. make pull request and I can add it?

@mgraham
Copy link
Author

mgraham commented May 5, 2014

Sure - I will try to do this! It may not be for a week or two, though.

@danielspaniel
Copy link
Collaborator

I beat you to it .. since I had to refactor the heck out of code to make the sequences easy to use, I just did this on a binge yesterday.
The sequences and makeList feature are now in version 0.1.2 or in master

@mgraham
Copy link
Author

mgraham commented May 6, 2014

Fantastic! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants