Skip to content

Implemented dependent attribute (double handlebars)#13

Open
sogko wants to merge 1 commit intopetejkim:masterfrom
sogko:master
Open

Implemented dependent attribute (double handlebars)#13
sogko wants to merge 1 commit intopetejkim:masterfrom
sogko:master

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."
    });
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.

1 participant