Skip to content
New issue

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

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

Already on GitHub? # to your account

Implemented dependent attribute (double handlebars) #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sogko
Copy link

@sogko sogko commented Jan 26, 2014

Implemented dependent attribute support.

  • Supports both dependent attribute defined as a string or as lazy attribute.
  • Dependent attribute may also contain associations.
  • Dependent attribute can also be passed as arguments to #build and #create.
  • Using double handlebars as template tags.

See: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#dependent-attributes

Definition example:

...
Factory.define('person', Person, {
      name: function(cb) { cb("person " + nameCounter++); }
    , age: 25
    , job: Factory.assoc('job')
    , title: Factory.assoc('job', 'title')
    , job_description: Factory.assoc('job', 'description')
    , job_description_alternate: Factory.assoc('job', 'description_alternate')
    });

Factory.define('job', Job, {
      title: 'Engineer'
    , company: function(cb) { cb('Foobar Inc.'); }
    , description: function(cb) { cb('{{title}} in {{company}}'); } 
        //--> "Engineer in Foobar Inc."
    , description_alternate: '{{title}}, {{company}}' 
        // --> "Engineer, Foobar Inc."
    });

Using it in factory#build example:

Factory.build( 'person',
    { name : 'John Doe, {{title}} - {{age}}'  },
    function(person) {
       ...
       // person.title = 'Engineer'; <-- Note: person.title was defined as an association
       // person.age = 25;
       // person.name = 'John Doe, Engineer - 25';
    }
);

Supports both dependent attribute defined as a string or as lazy
attribute.
Dependent attribute may also contain associations.

Example:

Factory.define('job', Job, {
      title: 'Engineer'
    , company: function(cb) { cb('Foobar Inc.'); }
    , description: function(cb) { cb('{{title}} in {{company}}'); } //
--> "Engineer in Foobar Inc."
    , description_alternate: '{{title}}, {{company}}' // --> "Engineer,
Foobar Inc."
    });
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant