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

Inject Faker into the Mirage.Factory attribute's closure #153

Merged
merged 5 commits into from
Jun 3, 2015

Conversation

mupkoo
Copy link
Contributor

@mupkoo mupkoo commented Jun 2, 2015

Inject Faker into the Mirage.Factory attribute's closure, so you can do something like

export default Mirage.Factory.extend({
  name: () => { return this.faker.name.firstName() }
});

Let me know what do you think and I will add it do the documentation.

@samselikoff
Copy link
Collaborator

Thanks for the PR! I'm wondering if we can make it even simpler, something like

import Mirage, {faker} from 'ember-cli-mirage';

export default Mirage.Factory.extend({
  name: faker.name.firstName
});

@samselikoff
Copy link
Collaborator

I've also thought it would be nice to have some way to specify a list of data to pull from, is this possible with Faker? For example, I like seeding my dev database with actual ted events so I can see what the UI will actually look like. I've done something like this before

export default Mirage.Factory.extend({
  title: i => {
    var list = [
      'TED2011',
      'TED Women 2011',
      'TED Global 2012',
      'TED 2013',
      'TED 2014',
      'TED Active 2014'
    ];
    return list[i % list.length];
  }
});

but it would be really nice to be able to do

export default Mirage.Factory.extend({
  title: faker.list(
    'TED2011',
    'TED Women 2011',
    'TED Global 2012',
    'TED 2013',
    'TED 2014',
    'TED Active 2014'
  )
});

or something like that.

@mupkoo
Copy link
Contributor Author

mupkoo commented Jun 2, 2015

Importing faker looks and feels better. The latest version exports fakers, so you can do it like you mentioned.

import Mirage, {faker} from 'ember-cli-mirage';

export default Mirage.Factory.extend({
  name: faker.name.firstName
});

But the project is using an old version of Babel, so I had to add two import lines in the dummy test app

import Mirage from 'ember-cli-mirage';
import { faker } from 'ember-cli-mirage';

As for the list helper, I created a helper method which gets exported as well. You can now do something like that and it will select a value from the list you have provided (the example will work with Babel 5)

import Mirage, { cycle } from 'ember-cli-mirage';

export default Mirage.Factory.extend({
   title: cycle('TED2011', 'TED2012')
});

If you think it's better to name it list or/and merge it into faker, let me know.

@mupkoo
Copy link
Contributor Author

mupkoo commented Jun 2, 2015

I had a change of hearts and decided to integrate list helpers into faker, but I created two helpers - random and cycle. One selects a random value from a list, the other one cycles them.

import Mirage, { faker } from 'ember-cli-mirage';

export default Mirage.Factory.extend({
   name: faker.list.random('Jane', 'John'),
   age: faker.list.cycle(30, 40, 50)
});

@samselikoff
Copy link
Collaborator

love this, exactly what I was thinking! thanks for your help!

samselikoff added a commit that referenced this pull request Jun 3, 2015
Inject Faker into the Mirage.Factory attribute's closure
@samselikoff samselikoff merged commit c88229c into miragejs:master Jun 3, 2015
@seanpdoyle
Copy link
Contributor

This looks great!

@mupkoo
Copy link
Contributor Author

mupkoo commented Jun 3, 2015

Thank you @seanpdoyle

@samselikoff do you want me to create a PR with an update to the docs explaining about faker?

@samselikoff
Copy link
Collaborator

@mupkoo that would be great! Since it's a backwards-compatible new feature we'll release it in 0.1.3, but let's not worry about making a new version of the docs for 0.1.2. We should add a note that says Added in 0.1.3 somehow. You can look at the other <aside> tags, or make your own banner for this.

@samselikoff
Copy link
Collaborator

And we should also make Latest say Latest (0.1.x)

@mupkoo
Copy link
Contributor Author

mupkoo commented Jun 3, 2015

Ok. Can you tell me where I can find the documentation?

@samselikoff
Copy link
Collaborator

yep gh-pages branch

@seawatts
Copy link
Contributor

seawatts commented Jun 9, 2015

@mupkoo Any updates on the docs?

@mupkoo
Copy link
Contributor Author

mupkoo commented Jun 9, 2015

@seawatts I haven't had any free time those days, but I think I will manage to create a PR this week

@seawatts
Copy link
Contributor

seawatts commented Jun 9, 2015

@mupkoo 👍 Thanks!

@samselikoff
Copy link
Collaborator

@mupkoo @seawatts I'm taking care of this, almost had it finished last night but will wrap up today. I want everything to be under a v0.1.x docset with new features indicated with a banner.

Should have something pushed today or tomorrow.

btw, <3 this PR, been using it a lot and it's awesome!

@samselikoff
Copy link
Collaborator

@seawatts
Copy link
Contributor

Woot 👍
On Jun 10, 2015 9:16 AM, "Sam Selikoff" notifications@github.com wrote:

faker docs added:
http://www.ember-cli-mirage.com/docs/v0.1.x/factories/#using-fakerjs


Reply to this email directly or view it on GitHub
#153 (comment)
.

@mupkoo
Copy link
Contributor Author

mupkoo commented Jun 10, 2015

Awesome! 👍 :)

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

Successfully merging this pull request may close these issues.

None yet

4 participants