Skip to content

Commit

Permalink
Merge pull request #66 from cristinawithout/relationship-typekey
Browse files Browse the repository at this point in the history
Use  typeKey for relationship instead of attribute name for build fixture relationship
  • Loading branch information
danielspaniel committed Feb 25, 2015
2 parents b34e19c + 1c7c2b5 commit 276df5a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/factory_guy.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ var FactoryGuy = {
@param {String} attribute attribute you want to check
@returns {Boolean} true if the attribute is a relationship, false if not
*/
isAttributeRelationship: function(typeName, attribute) {
getAttributeRelationship: function(typeName, attribute) {
if (!this.store) {
Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures")
// The legacy value was true.
return true;
}
var model = this.store.modelFor(typeName);
return !!model.typeForRelationship(attribute);
var relationship = model.typeForRelationship(attribute);
return !!relationship ? relationship : null;
},
/**
Used in model definitions to declare use of a sequence. For example:
Expand Down
5 changes: 3 additions & 2 deletions src/model_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ var ModelDefinition = function (model, config) {
// If it's an object and it's a model association attribute, build the json
// for the association and replace the attribute with that json
if (FactoryGuy.getStore()) {
if (FactoryGuy.isAttributeRelationship(this.model, attribute)) {
fixture[attribute] = FactoryGuy.build(attribute, fixture[attribute]);
var relationship = FactoryGuy.getAttributeRelationship(this.model, attribute);
if (relationship) {
fixture[attribute] = FactoryGuy.build(relationship.typeKey, fixture[attribute]);
}
} else {
// For legacy reasons, if the store is not set in FactoryGuy, keep
Expand Down
8 changes: 4 additions & 4 deletions tests/factory_guy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ test("#buildList creates list of fixtures", function() {
});


test("#isAttributeRelationship", function() {
test("#getAttributeRelationship", function() {
FactoryGuy.setStore(store);
var typeName = 'user'
equal(FactoryGuy.isAttributeRelationship(typeName,'company'),true);
equal(FactoryGuy.isAttributeRelationship(typeName,'hats'),true);
equal(FactoryGuy.isAttributeRelationship(typeName,'name'),false);
equal(FactoryGuy.getAttributeRelationship(typeName,'company').typeKey,'company');
equal(FactoryGuy.getAttributeRelationship(typeName,'hats').typeKey,'hat');
equal(FactoryGuy.getAttributeRelationship(typeName,'name'),null);
});


Expand Down

0 comments on commit 276df5a

Please sign in to comment.