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

Factory sequences, traits, lazy #27

Closed
samselikoff opened this issue Feb 25, 2015 · 6 comments
Closed

Factory sequences, traits, lazy #27

samselikoff opened this issue Feb 25, 2015 · 6 comments

Comments

@samselikoff
Copy link
Collaborator

The API for sequences is changing to be consistent with upcoming traits, dynamic attrs and associations.

// factories/post.js
import Mirage from 'ember-cli-mirage';

export default Factory.extend({
  title: 'ABC',
  date: new Date(),

  likes: Mirage.seq(i => i),

  approved: Mirage.trait({
    approved_at: new Date(),
  }),

  views: Mirage.lazy(() => {
    return Math.random() * 1000;
  }),

  link: Mirage.lazy('title', 'date', (title, date) => {
    return moment(date).format('MM/DD') + '/' + title;
  }),

});


// tests/acceptance/index-test.js
...
server.create('user');
server.create('user', 'approved');
@samselikoff samselikoff changed the title Factory traits Factory squences and traits Mar 19, 2015
@samselikoff samselikoff changed the title Factory squences and traits Factory sequences, traits, lazy Mar 19, 2015
@samselikoff samselikoff modified the milestone: 0.1.0 Apr 6, 2015
@samselikoff samselikoff removed this from the 0.2.0 milestone Oct 30, 2015
@jamesarosen
Copy link

One pattern we use a lot is a belongs-to relationship that defaults to the first one in the DB:

foo_id: Mirage.lazy((db) => {
  return db.foos.firstOrCreate().id;
})

@mellatone
Copy link

Would this support something like

Factory.extend({
  first: function(){
    return faker.name();
  },
  last: function(){
    return faker.name();
  },
  full: function(){
    Return this.first + " " + this.last;
  }
})

(Take it as pseudo code.. I am on a mobile phone!)

@samselikoff
Copy link
Collaborator Author

@jamesarosen interesting... I think an afterCreate block would be a nice place for this?

@mellatone yes, actually this is possible as soon as this PR lands, which is backwards compatible so there's nothing blocking it.

@samselikoff
Copy link
Collaborator Author

image

@samselikoff
Copy link
Collaborator Author

#269 closes dynamic

@samselikoff
Copy link
Collaborator Author

samselikoff commented Aug 30, 2016

closing as sequences/lazy are done. Follow #852 to track traits

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