GithubHelp home page GithubHelp logo

Comments (8)

chetanism avatar chetanism commented on June 30, 2024 1

@jonatanklosko, sorry for a long delay!
I have raised #73 as per @aexmachina 's suggestion. The afterBuild callback should now be invoked on create calls as well (once the PR gets merged :)).

from factory-girl.

jonatanklosko avatar jonatanklosko commented on June 30, 2024 1

I've just realised that it's not invoked after #attrs as well.

from factory-girl.

chetanism avatar chetanism commented on June 30, 2024 1

Hi @jonatanklosko, the order of execution for these methods is attrs -> build -> create. As of now, we just have callbacks for afterBuild and afterCreate, hence the callbacks are not executed for attrs call. I'll open another issue to have an afterAttrs callback.

from factory-girl.

chetanism avatar chetanism commented on June 30, 2024

Hi @jonatanklosko,
You can try following to achieve the same effect:

Factory.define('user', User, {
  username: Factory.sequence(n => `sherlock_${n}`),
  name: 'Sherlock Holmes',
  password: 'password',
  passwordConfirmation: null,
}, {
  afterBuild: model => {
    model.passwordConfirmation = model.password;
    return model;
  },
});

Just to add some details, the problem with having it the way you suggested is how to ensure that passwordConfirmation is evaluated after password.

You can use the afterBuild callback to modify the built object anyway you want!

Let us know in case you face any issues.

from factory-girl.

jonatanklosko avatar jonatanklosko commented on June 30, 2024

Hey @chetanism,
I've already tried this and actually it should be

afterBuild: model => {
  model.passwordConfirmation = model.passwordConfirmation || model.password;
  return model;
},

The problem with this solution is that afterBuild is not invoked when you use create.

I can think of a feature, that would allow us to do:

/* ... */
password: "secret",
passwordConfirmation: factory.duplicate('password')
/* ... */

/* Or even more flexible */

/* ... */
password: "secret",
passwordConfirmation: factory.after('password', password => /* do something and return */)
/* ... */

This way you can evaluate all normal attributes and those duplicate/after calls at the end.
What are your thoughts =) ?

from factory-girl.

simonexmachina avatar simonexmachina commented on June 30, 2024

@chetanism I think maybe we should consider calling both afterBuild and afterCreate when creating. I think this makes sense because create does follow build.

from factory-girl.

jonatanklosko avatar jonatanklosko commented on June 30, 2024

Yeah this behavior makes more sense. The PR looks great, thanks! =)

from factory-girl.

simonexmachina avatar simonexmachina commented on June 30, 2024

#73 has been merged, closing this issue now. Please advise if there's any issues.

from factory-girl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.