ember-data-factory-guy 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -5
- data/bower.json +1 -1
- data/dist/ember-data-factory-guy.js +5 -1
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/ember-data-factory-guy.gemspec +1 -1
- data/package.json +1 -1
- data/src/factory_guy.js +4 -1
- data/src/store.js +1 -0
- data/tests/active_model_adapter_factory_test.js +27 -2
- data/tests/rest_adapter_factory_test.js +13 -2
- data/tests/support/factories/company_factory.js +13 -0
- data/tests/support/factories/project_factory.js +1 -0
- data/tests/support/factories/user_factory.js +34 -1
- data/tests/support/models/company.js +3 -2
- data/tests/support/models/user.js +1 -1
- data/tests/support/test_helper.js +1 -0
- data/tests/test_setup.js +52 -3
- data/vendor/assets/javascripts/ember_data_factory_guy.js +5 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c2abf7589fecda0e42120d5c2e9bf8efc2caef7
|
4
|
+
data.tar.gz: 93fb335e1e15c4ad91be1b1716b7b30c71483f03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b84a2d29390fd35426e6eb4e5fa5b04338999f5e2c859bebc8a76f53a24ad5b1487a18bacb2d0d110fc482b1211fdf6b4d412d91df557913eea1af496ce438a
|
7
|
+
data.tar.gz: 4f707a3a8702fb5ab142bf97c1e5a9dbb94ee709989e929d3b4356ba847b6798c86c571dba925c8412678bc926024df409438078f4034a041148e6d2d710fefc
|
data/README.md
CHANGED
@@ -5,9 +5,14 @@
|
|
5
5
|
ember-data is changing the way they are doing relationships in 1.0.0-beta.10 and above
|
6
6
|
so, if you are using ember-data-1.0.0-beta.8 and earlier, then be sure to use version 0.6.4
|
7
7
|
( or below ) of ember-data-factory-guy.
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
|
9
|
+
- Versions:
|
10
|
+
- 0.6.4 -> ember-data-1.0.0-beta.8 and under
|
11
|
+
- 0.7.1 -> ember-data-1.0.0-beta.10
|
12
|
+
- 0.7.3 -> ember-data-1.0.0-beta.11
|
13
|
+
- 0.7.3 -> ember-data-1.0.0-beta.12
|
14
|
+
|
15
|
+
**For versions ( 0.7.1 -> 0.7.3 ), support for the fixture adapter is currently broken.**
|
11
16
|
|
12
17
|
## Using as Gem
|
13
18
|
|
@@ -22,7 +27,7 @@ gem 'ember-data-factory-guy', group: test
|
|
22
27
|
or for particular version:
|
23
28
|
|
24
29
|
```ruby
|
25
|
-
gem 'ember-data-factory-guy', '0.7.
|
30
|
+
gem 'ember-data-factory-guy', '0.7.3', group: test
|
26
31
|
```
|
27
32
|
|
28
33
|
then:
|
@@ -57,7 +62,7 @@ or for particular version:
|
|
57
62
|
"dependencies": {
|
58
63
|
"foo-dependency": "latest",
|
59
64
|
"other-foo-dependency": "latest",
|
60
|
-
"ember-data-factory-guy": "0.7.
|
65
|
+
"ember-data-factory-guy": "0.7.3"
|
61
66
|
}
|
62
67
|
```
|
63
68
|
|
data/bower.json
CHANGED
@@ -432,8 +432,11 @@ FactoryGuy = {
|
|
432
432
|
if (store.usingFixtureAdapter()) {
|
433
433
|
modelType.FIXTURES = [];
|
434
434
|
}
|
435
|
-
|
435
|
+
Ember.run(function(){
|
436
|
+
store.unloadAll(modelType);
|
437
|
+
})
|
436
438
|
} catch (e) {
|
439
|
+
// console.log(e)
|
437
440
|
}
|
438
441
|
}
|
439
442
|
},
|
@@ -609,6 +612,7 @@ DS.Store.reopen({
|
|
609
612
|
if (relationship.kind == 'belongsTo') {
|
610
613
|
var belongsToRecord = fixture[relationship.key];
|
611
614
|
if (Ember.typeOf(belongsToRecord) == 'object') {
|
615
|
+
store.findEmbeddedAssociationsForRESTAdapter(relationship.type, belongsToRecord);
|
612
616
|
belongsToRecord = store.push(relationship.type, belongsToRecord);
|
613
617
|
fixture[relationship.key] = belongsToRecord;
|
614
618
|
}
|
@@ -1 +1 @@
|
|
1
|
-
Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};function MissingSequenceError(message){this.toString=function(){return message}}ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.merge=function(config){};this.generate=function(name,sequenceFn){if(sequenceFn){if(!sequences[name]){sequences[name]=new Sequence(sequenceFn)}}var sequence=sequences[name];if(!sequence){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequence.next()};this.build=function(name,opts,traitArgs){var traitsObj={};traitArgs.forEach(function(trait){$.extend(traitsObj,traits[trait])});var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,traitsObj,opts);for(attribute in fixture){if(Ember.typeOf(fixture[attribute])=="function"){fixture[attribute]=fixture[attribute].call(this,fixture)}else if(Ember.typeOf(fixture[attribute])=="object"){fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}if(!fixture.id){fixture.id=modelId++}return fixture};this.buildList=function(name,number,traits,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts,traits))}return arr};this.reset=function(){modelId=1;for(name in sequences){sequences[name].reset()}};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseTraits=function(object){if(!object){return}traits=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(Ember.typeOf(sequenceFn)!="function"){throw new Error("Problem with ["+sequenceName+"] sequence definition. Sequences must be functions")}object[sequenceName]=new Sequence(sequenceFn)}sequences=object};var parseConfig=function(config){parseSequences(config.sequences);delete config.sequences;parseTraits(config.traits);delete config.traits;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},generate:function(nameOrFunction){var sortaRandomName=Math.floor((1+Math.random())*65536).toString(16)+Date.now();return function(){if(Em.typeOf(nameOrFunction)=="function"){return this.generate(sortaRandomName,nameOrFunction)}else{return this.generate(nameOrFunction)}}},belongsTo:function(fixtureName,opts){return function(){return FactoryGuy.build(fixtureName,opts)}},association:function(fixtureName,opts){console.log("DEPRECATION Warning: use FactoryGuy.belongsTo instead");return this.belongsTo(fixtureName,opts)},hasMany:function(fixtureName,number,opts){return function(){return FactoryGuy.buildList(fixtureName,number,opts)}},lookupModelForFixtureName:function(name){var definition=this.lookupDefinitionForFixtureName(name);if(definition){return definition.model}},lookupDefinitionForFixtureName:function(name){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(){var args=Array.prototype.slice.call(arguments);var opts={};var name=args.shift();if(!name){throw new Error("Build needs a factory name to build")}if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts,traits)},buildList:function(){var args=Array.prototype.slice.call(arguments);var name=args.shift();var number=args.shift();if(!name||!number){throw new Error("buildList needs a name and a number ( at least ) to build with")}var opts={};if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,traits,opts)},resetModels:function(store){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];definition.reset();try{var modelType=store.modelFor(definition.model);if(store.usingFixtureAdapter()){modelType.FIXTURES=[]}store.unloadAll(modelType)}catch(e){}}},pushFixture:function(modelClass,fixture){if(!modelClass["FIXTURES"]){modelClass["FIXTURES"]=[]}modelClass["FIXTURES"].push(fixture);return fixture},clear:function(opts){if(!opts){this.modelDefinitions={}}}};DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(name,options){var store=this;var modelName=FactoryGuy.lookupModelForFixtureName(name);var fixture=FactoryGuy.build(name,options);var modelType=store.modelFor(modelName);if(this.usingFixtureAdapter()){this.setAssociationsForFixtureAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var store=this;var model;Em.run(function(){store.findEmbeddedAssociationsForRESTAdapter(modelType,fixture);if(fixture.type){modelName=fixture.type.underscore();modelType=store.modelFor(modelName)}model=store.push(modelName,fixture);store.setAssociationsForRESTAdapter(modelType,modelName,model)});return model}},makeList:function(name,number,options){var arr=[];for(var i=0;i<number;i++){arr.push(this.makeFixture(name,options))}return arr},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=fixture.id})}}if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(belongsToRecord){if(typeof belongsToRecord=="object"){FactoryGuy.pushFixture(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord.id}var hasManyName=self.findHasManyRelationshipNameForFixtureAdapter(relationship.type,relationship.parentType);var belongsToFixtures=adapter.fixturesForType(relationship.type);var belongsTofixture=adapter.findFixtureById(belongsToFixtures,fixture[relationship.key]);if(!belongsTofixture[hasManyName]){belongsTofixture[hasManyName]=[]}belongsTofixture[hasManyName].push(fixture.id)}}})},findEmbeddedAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(Ember.typeOf(belongsToRecord)=="object"){belongsToRecord=store.push(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord}}if(relationship.kind=="hasMany"){var hasManyRecords=fixture[relationship.key];if(Ember.typeOf(hasManyRecords)=="array"){if(Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.map(function(object){var record=store.push(relationship.type,object);records.push(record);return record});fixture[relationship.key]=records}}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var children=model.get(name)||[];children.forEach(function(child){var belongsToName=self.findRelationshipName("belongsTo",child.constructor,model);if(belongsToName){child.set(belongsToName,model)}})}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind==kind&&childModel instanceof relationship.type){relationshipName=relationship.key}});return relationshipName},findHasManyRelationshipNameForFixtureAdapter:function(belongToModelType,childModelType){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"&&childModelType==relationship.type){relationshipName=relationship.key}});return relationshipName},pushPayload:function(type,payload){if(this.usingFixtureAdapter()){var model=this.modelFor(modelName);FactoryGuy.pushFixture(model,payload)}else{this._super(type,payload)}}});FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);return this},useFixtureAdapter:function(app){app.ApplicationAdapter=DS.FixtureAdapter;this.getStore().adapterFor("application").simulateRemoteResponse=false},usingActiveModelSerializer:function(type){var store=this.getStore();var type=store.modelFor(type);var serializer=store.serializerFor(type.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(name,opts){return this.getStore().makeFixture(name,opts)},getStore:function(){return this.get("container").lookup("store:main")},pushPayload:function(type,hash){return this.getStore().pushPayload(type,hash)},pushRecord:function(type,hash){return this.getStore().push(type,hash)},stubEndpointForHttpRequest:function(url,json,options){options=options||{};var request={url:url,dataType:"json",responseText:json,type:options.type||"GET",status:options.status||200};if(options.data){request.data=options.data}$.mockjax(request)},buildAjaxHttpResponse:function(name,opts){var fixture=FactoryGuy.build(name,opts);var modelName=FactoryGuy.lookupModelForFixtureName(name);if(this.usingActiveModelSerializer(modelName)){this.toSnakeCase(fixture)}var hash={};hash[modelName]=fixture;return hash},toSnakeCase:function(fixture){for(key in fixture){if(key!=Em.String.decamelize(key)){var value=fixture[key];delete fixture[key];fixture[Em.String.decamelize(key)]=value}}},buildURL:function(type,id){return this.getStore().adapterFor("application").buildURL(type,id)},handleSideloadFind:function(modelName,json,sideload){var id=json.id;var url=this.buildURL(modelName,id);var responseJson={};responseJson[modelName]=json;$.extend(responseJson,sideload);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET",status:status||200})},handleFind:function(name,opts,status){var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson=this.buildAjaxHttpResponse(name,opts);var id=responseJson[modelName].id;var url=this.buildURL(modelName,id);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET",status:status||200});return responseJson},handleCreate:function(name,opts,status){var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson=this.buildAjaxHttpResponse(name,opts);var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{type:"POST",status:status||200});return responseJson},handleUpdate:function(type,id,status){this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:status||200})},handleDelete:function(type,id,status){this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:status||200})},teardown:function(){FactoryGuy.resetModels(this.getStore())}});
|
1
|
+
Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};function MissingSequenceError(message){this.toString=function(){return message}}ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.merge=function(config){};this.generate=function(name,sequenceFn){if(sequenceFn){if(!sequences[name]){sequences[name]=new Sequence(sequenceFn)}}var sequence=sequences[name];if(!sequence){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequence.next()};this.build=function(name,opts,traitArgs){var traitsObj={};traitArgs.forEach(function(trait){$.extend(traitsObj,traits[trait])});var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,traitsObj,opts);for(attribute in fixture){if(Ember.typeOf(fixture[attribute])=="function"){fixture[attribute]=fixture[attribute].call(this,fixture)}else if(Ember.typeOf(fixture[attribute])=="object"){fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}if(!fixture.id){fixture.id=modelId++}return fixture};this.buildList=function(name,number,traits,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts,traits))}return arr};this.reset=function(){modelId=1;for(name in sequences){sequences[name].reset()}};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseTraits=function(object){if(!object){return}traits=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(Ember.typeOf(sequenceFn)!="function"){throw new Error("Problem with ["+sequenceName+"] sequence definition. Sequences must be functions")}object[sequenceName]=new Sequence(sequenceFn)}sequences=object};var parseConfig=function(config){parseSequences(config.sequences);delete config.sequences;parseTraits(config.traits);delete config.traits;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},generate:function(nameOrFunction){var sortaRandomName=Math.floor((1+Math.random())*65536).toString(16)+Date.now();return function(){if(Em.typeOf(nameOrFunction)=="function"){return this.generate(sortaRandomName,nameOrFunction)}else{return this.generate(nameOrFunction)}}},belongsTo:function(fixtureName,opts){return function(){return FactoryGuy.build(fixtureName,opts)}},association:function(fixtureName,opts){console.log("DEPRECATION Warning: use FactoryGuy.belongsTo instead");return this.belongsTo(fixtureName,opts)},hasMany:function(fixtureName,number,opts){return function(){return FactoryGuy.buildList(fixtureName,number,opts)}},lookupModelForFixtureName:function(name){var definition=this.lookupDefinitionForFixtureName(name);if(definition){return definition.model}},lookupDefinitionForFixtureName:function(name){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(){var args=Array.prototype.slice.call(arguments);var opts={};var name=args.shift();if(!name){throw new Error("Build needs a factory name to build")}if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts,traits)},buildList:function(){var args=Array.prototype.slice.call(arguments);var name=args.shift();var number=args.shift();if(!name||!number){throw new Error("buildList needs a name and a number ( at least ) to build with")}var opts={};if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,traits,opts)},resetModels:function(store){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];definition.reset();try{var modelType=store.modelFor(definition.model);if(store.usingFixtureAdapter()){modelType.FIXTURES=[]}Ember.run(function(){store.unloadAll(modelType)})}catch(e){}}},pushFixture:function(modelClass,fixture){if(!modelClass["FIXTURES"]){modelClass["FIXTURES"]=[]}modelClass["FIXTURES"].push(fixture);return fixture},clear:function(opts){if(!opts){this.modelDefinitions={}}}};DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(name,options){var store=this;var modelName=FactoryGuy.lookupModelForFixtureName(name);var fixture=FactoryGuy.build(name,options);var modelType=store.modelFor(modelName);if(this.usingFixtureAdapter()){this.setAssociationsForFixtureAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var store=this;var model;Em.run(function(){store.findEmbeddedAssociationsForRESTAdapter(modelType,fixture);if(fixture.type){modelName=fixture.type.underscore();modelType=store.modelFor(modelName)}model=store.push(modelName,fixture);store.setAssociationsForRESTAdapter(modelType,modelName,model)});return model}},makeList:function(name,number,options){var arr=[];for(var i=0;i<number;i++){arr.push(this.makeFixture(name,options))}return arr},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=fixture.id})}}if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(belongsToRecord){if(typeof belongsToRecord=="object"){FactoryGuy.pushFixture(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord.id}var hasManyName=self.findHasManyRelationshipNameForFixtureAdapter(relationship.type,relationship.parentType);var belongsToFixtures=adapter.fixturesForType(relationship.type);var belongsTofixture=adapter.findFixtureById(belongsToFixtures,fixture[relationship.key]);if(!belongsTofixture[hasManyName]){belongsTofixture[hasManyName]=[]}belongsTofixture[hasManyName].push(fixture.id)}}})},findEmbeddedAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(Ember.typeOf(belongsToRecord)=="object"){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,belongsToRecord);belongsToRecord=store.push(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord}}if(relationship.kind=="hasMany"){var hasManyRecords=fixture[relationship.key];if(Ember.typeOf(hasManyRecords)=="array"){if(Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.map(function(object){var record=store.push(relationship.type,object);records.push(record);return record});fixture[relationship.key]=records}}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var children=model.get(name)||[];children.forEach(function(child){var belongsToName=self.findRelationshipName("belongsTo",child.constructor,model);if(belongsToName){child.set(belongsToName,model)}})}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind==kind&&childModel instanceof relationship.type){relationshipName=relationship.key}});return relationshipName},findHasManyRelationshipNameForFixtureAdapter:function(belongToModelType,childModelType){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"&&childModelType==relationship.type){relationshipName=relationship.key}});return relationshipName},pushPayload:function(type,payload){if(this.usingFixtureAdapter()){var model=this.modelFor(modelName);FactoryGuy.pushFixture(model,payload)}else{this._super(type,payload)}}});FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);return this},useFixtureAdapter:function(app){app.ApplicationAdapter=DS.FixtureAdapter;this.getStore().adapterFor("application").simulateRemoteResponse=false},usingActiveModelSerializer:function(type){var store=this.getStore();var type=store.modelFor(type);var serializer=store.serializerFor(type.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(name,opts){return this.getStore().makeFixture(name,opts)},getStore:function(){return this.get("container").lookup("store:main")},pushPayload:function(type,hash){return this.getStore().pushPayload(type,hash)},pushRecord:function(type,hash){return this.getStore().push(type,hash)},stubEndpointForHttpRequest:function(url,json,options){options=options||{};var request={url:url,dataType:"json",responseText:json,type:options.type||"GET",status:options.status||200};if(options.data){request.data=options.data}$.mockjax(request)},buildAjaxHttpResponse:function(name,opts){var fixture=FactoryGuy.build(name,opts);var modelName=FactoryGuy.lookupModelForFixtureName(name);if(this.usingActiveModelSerializer(modelName)){this.toSnakeCase(fixture)}var hash={};hash[modelName]=fixture;return hash},toSnakeCase:function(fixture){for(key in fixture){if(key!=Em.String.decamelize(key)){var value=fixture[key];delete fixture[key];fixture[Em.String.decamelize(key)]=value}}},buildURL:function(type,id){return this.getStore().adapterFor("application").buildURL(type,id)},handleSideloadFind:function(modelName,json,sideload){var id=json.id;var url=this.buildURL(modelName,id);var responseJson={};responseJson[modelName]=json;$.extend(responseJson,sideload);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET",status:status||200})},handleFind:function(name,opts,status){var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson=this.buildAjaxHttpResponse(name,opts);var id=responseJson[modelName].id;var url=this.buildURL(modelName,id);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET",status:status||200});return responseJson},handleCreate:function(name,opts,status){var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson=this.buildAjaxHttpResponse(name,opts);var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{type:"POST",status:status||200});return responseJson},handleUpdate:function(type,id,status){this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:status||200})},handleDelete:function(type,id,status){this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:status||200})},teardown:function(){FactoryGuy.resetModels(this.getStore())}});
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = "ember-data-factory-guy"
|
4
|
-
s.version = "0.7.
|
4
|
+
s.version = "0.7.3"
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.authors = ["Daniel Sudol", "Alex Opak"]
|
7
7
|
s.email = ["dansudol@yahoo.com", "opak.alexandr@gmail.com"]
|
data/package.json
CHANGED
data/src/factory_guy.js
CHANGED
@@ -262,8 +262,11 @@ FactoryGuy = {
|
|
262
262
|
if (store.usingFixtureAdapter()) {
|
263
263
|
modelType.FIXTURES = [];
|
264
264
|
}
|
265
|
-
|
265
|
+
Ember.run(function(){
|
266
|
+
store.unloadAll(modelType);
|
267
|
+
})
|
266
268
|
} catch (e) {
|
269
|
+
// console.log(e)
|
267
270
|
}
|
268
271
|
}
|
269
272
|
},
|
data/src/store.js
CHANGED
@@ -143,6 +143,7 @@ DS.Store.reopen({
|
|
143
143
|
if (relationship.kind == 'belongsTo') {
|
144
144
|
var belongsToRecord = fixture[relationship.key];
|
145
145
|
if (Ember.typeOf(belongsToRecord) == 'object') {
|
146
|
+
store.findEmbeddedAssociationsForRESTAdapter(relationship.type, belongsToRecord);
|
146
147
|
belongsToRecord = store.push(relationship.type, belongsToRecord);
|
147
148
|
fixture[relationship.key] = belongsToRecord;
|
148
149
|
}
|
@@ -22,8 +22,8 @@ test("#resetModels clears the store of models, and resets the model definition",
|
|
22
22
|
|
23
23
|
FactoryGuy.resetModels(store);
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
equal(store.all('user').get('content.length'),0)
|
26
|
+
equal(store.all('project').get('content.length'),0)
|
27
27
|
|
28
28
|
for (model in FactoryGuy.modelDefinitions) {
|
29
29
|
var definition = FactoryGuy.modelDefinitions[model];
|
@@ -219,6 +219,31 @@ test("belongsTo associations defined as attributes in fixture", function() {
|
|
219
219
|
ok(project.get('user.name') == 'Admin');
|
220
220
|
});
|
221
221
|
|
222
|
+
test("belongsTo with embedded hasMany associations", function() {
|
223
|
+
var project = store.makeFixture('project', 'with_user_having_hats');
|
224
|
+
var user = project.get('user');
|
225
|
+
var hats = user.get('hats');
|
226
|
+
|
227
|
+
ok(user.get('projects.firstObject') == project)
|
228
|
+
ok(hats.get('firstObject.user') == user)
|
229
|
+
});
|
230
|
+
|
231
|
+
|
232
|
+
test("hasMany associations defined as attributes in fixture", function() {
|
233
|
+
var user = store.makeFixture('user_with_projects');
|
234
|
+
equal(user.get('projects.length'), 2)
|
235
|
+
ok(user.get('projects.firstObject.user') == user)
|
236
|
+
ok(user.get('projects.lastObject.user') == user)
|
237
|
+
})
|
238
|
+
|
239
|
+
|
240
|
+
test("hasMany associations defined with traits", function() {
|
241
|
+
var user = store.makeFixture('user', 'with_projects');
|
242
|
+
equal(user.get('projects.length'), 2)
|
243
|
+
ok(user.get('projects.firstObject.user') == user)
|
244
|
+
ok(user.get('projects.lastObject.user') == user)
|
245
|
+
})
|
246
|
+
|
222
247
|
|
223
248
|
module('DS.Store#makeList with ActiveModelAdapter', {
|
224
249
|
setup: function() {
|
@@ -22,8 +22,8 @@ test("#resetModels clears the store of models, and resets the model definition",
|
|
22
22
|
|
23
23
|
FactoryGuy.resetModels(store);
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
equal(store.all('user').get('content.length'), 0)
|
26
|
+
equal(store.all('project').get('content.length'), 0)
|
27
27
|
|
28
28
|
for (model in FactoryGuy.modelDefinitions) {
|
29
29
|
var definition = FactoryGuy.modelDefinitions[model];
|
@@ -214,6 +214,16 @@ test("belongsTo associations defined as attributes in fixture", function() {
|
|
214
214
|
});
|
215
215
|
|
216
216
|
|
217
|
+
test("belongsTo with embedded hasMany associations", function() {
|
218
|
+
var project = store.makeFixture('project', 'with_user_having_hats');
|
219
|
+
var user = project.get('user');
|
220
|
+
var hats = user.get('hats');
|
221
|
+
|
222
|
+
ok(user.get('projects.firstObject') == project)
|
223
|
+
ok(hats.get('firstObject.user') == user)
|
224
|
+
});
|
225
|
+
|
226
|
+
|
217
227
|
test("hasMany associations defined as attributes in fixture", function() {
|
218
228
|
var user = store.makeFixture('user_with_projects');
|
219
229
|
equal(user.get('projects.length'), 2)
|
@@ -230,6 +240,7 @@ test("hasMany associations defined with traits", function() {
|
|
230
240
|
})
|
231
241
|
|
232
242
|
|
243
|
+
|
233
244
|
module('DS.Store#makeList with DS.RESTAdapter', {
|
234
245
|
setup: function() {
|
235
246
|
testHelper = TestHelper.setup(DS.RESTAdapter);
|
@@ -1,5 +1,18 @@
|
|
1
1
|
FactoryGuy.define("company", {
|
2
2
|
default: {
|
3
3
|
name: 'Silly corp'
|
4
|
+
},
|
5
|
+
traits: {
|
6
|
+
with_projects: {
|
7
|
+
projects: FactoryGuy.hasMany('project', 2)
|
8
|
+
}
|
9
|
+
}
|
10
|
+
|
11
|
+
})
|
12
|
+
|
13
|
+
FactoryGuy.define("small_company", {
|
14
|
+
default: {
|
15
|
+
name: 'Small Corp',
|
16
|
+
projects: FactoryGuy.hasMany('project', 2)
|
4
17
|
}
|
5
18
|
})
|
@@ -6,6 +6,7 @@ FactoryGuy.define("project", {
|
|
6
6
|
big: { title: 'Big Project' },
|
7
7
|
with_title_sequence: { title: FactoryGuy.generate('title') },
|
8
8
|
with_user: { user: {} },
|
9
|
+
with_user_having_hats: { user: FactoryGuy.belongsTo('user', 'with_hats') },
|
9
10
|
with_dude: { user: {name: 'Dude'} },
|
10
11
|
with_admin: { user: FactoryGuy.belongsTo('admin') }
|
11
12
|
},
|
@@ -13,6 +13,39 @@ FactoryGuy.define('user', {
|
|
13
13
|
traits: {
|
14
14
|
with_projects: {
|
15
15
|
projects: FactoryGuy.hasMany('project', 2)
|
16
|
+
},
|
17
|
+
with_hats: {
|
18
|
+
hats: FactoryGuy.hasMany('big_hat', 2)
|
16
19
|
}
|
17
20
|
}
|
18
|
-
});
|
21
|
+
});
|
22
|
+
|
23
|
+
|
24
|
+
//Unit = DS.Model.extend({
|
25
|
+
// lesson: DS.belongsTo('lesson')
|
26
|
+
//})
|
27
|
+
//
|
28
|
+
//Lesson = DS.Model.extend({
|
29
|
+
// steps: DS.hasMany('step')
|
30
|
+
//})
|
31
|
+
//
|
32
|
+
//Step = DS.Model.extend({
|
33
|
+
// lesson: DS.belongsTo ('lesson')
|
34
|
+
//})
|
35
|
+
//
|
36
|
+
//
|
37
|
+
//FactoryGuy.define('unit', {
|
38
|
+
// default: {
|
39
|
+
// lesson: FactoryGuy.belongsTo('lesson')
|
40
|
+
// }
|
41
|
+
//})
|
42
|
+
//
|
43
|
+
//FactoryGuy.define('lesson', {
|
44
|
+
// default: {
|
45
|
+
// steps: FactoryGuy.hasMany('step', 2)
|
46
|
+
// }
|
47
|
+
//})
|
48
|
+
//
|
49
|
+
//FactoryGuy.define('step', {
|
50
|
+
// default: {}
|
51
|
+
//})
|
@@ -1,10 +1,11 @@
|
|
1
1
|
Company = DS.Model.extend({
|
2
2
|
name: DS.attr('string'),
|
3
3
|
profile: DS.belongsTo('profile'),
|
4
|
-
users: DS.hasMany('user', {async: true, inverse: 'company'})
|
4
|
+
users: DS.hasMany('user', {async: true, inverse: 'company'}),
|
5
|
+
projects: DS.hasMany('project', {async: true})
|
5
6
|
});
|
6
7
|
|
7
8
|
SmallCompany = Company.extend({
|
8
9
|
owner: DS.belongsTo('user', {async: true}),
|
9
|
-
projects: DS.hasMany('project'
|
10
|
+
projects: DS.hasMany('project')
|
10
11
|
});
|
@@ -1,6 +1,6 @@
|
|
1
1
|
User = DS.Model.extend({
|
2
2
|
name: DS.attr('string'),
|
3
|
-
company: DS.belongsTo('company', {async: true, inverse: 'users'}),
|
3
|
+
company: DS.belongsTo('company', {async: true, inverse: 'users', polymorphic: true}),
|
4
4
|
properties: DS.hasMany('property', {async: true, inverse: 'owners'}),
|
5
5
|
projects: DS.hasMany('project'),
|
6
6
|
hats: DS.hasMany('hat', {polymorphic: true})
|
data/tests/test_setup.js
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
FactoryGuy.define("company", {
|
2
2
|
default: {
|
3
3
|
name: 'Silly corp'
|
4
|
+
},
|
5
|
+
traits: {
|
6
|
+
with_projects: {
|
7
|
+
projects: FactoryGuy.hasMany('project', 2)
|
8
|
+
}
|
9
|
+
}
|
10
|
+
|
11
|
+
})
|
12
|
+
|
13
|
+
FactoryGuy.define("small_company", {
|
14
|
+
default: {
|
15
|
+
name: 'Small Corp',
|
16
|
+
projects: FactoryGuy.hasMany('project', 2)
|
4
17
|
}
|
5
18
|
})
|
6
19
|
FactoryGuy.define("group", {
|
@@ -71,6 +84,7 @@ FactoryGuy.define("project", {
|
|
71
84
|
big: { title: 'Big Project' },
|
72
85
|
with_title_sequence: { title: FactoryGuy.generate('title') },
|
73
86
|
with_user: { user: {} },
|
87
|
+
with_user_having_hats: { user: FactoryGuy.belongsTo('user', 'with_hats') },
|
74
88
|
with_dude: { user: {name: 'Dude'} },
|
75
89
|
with_admin: { user: FactoryGuy.belongsTo('admin') }
|
76
90
|
},
|
@@ -126,18 +140,53 @@ FactoryGuy.define('user', {
|
|
126
140
|
traits: {
|
127
141
|
with_projects: {
|
128
142
|
projects: FactoryGuy.hasMany('project', 2)
|
143
|
+
},
|
144
|
+
with_hats: {
|
145
|
+
hats: FactoryGuy.hasMany('big_hat', 2)
|
129
146
|
}
|
130
147
|
}
|
131
148
|
});
|
149
|
+
|
150
|
+
|
151
|
+
//Unit = DS.Model.extend({
|
152
|
+
// lesson: DS.belongsTo('lesson')
|
153
|
+
//})
|
154
|
+
//
|
155
|
+
//Lesson = DS.Model.extend({
|
156
|
+
// steps: DS.hasMany('step')
|
157
|
+
//})
|
158
|
+
//
|
159
|
+
//Step = DS.Model.extend({
|
160
|
+
// lesson: DS.belongsTo ('lesson')
|
161
|
+
//})
|
162
|
+
//
|
163
|
+
//
|
164
|
+
//FactoryGuy.define('unit', {
|
165
|
+
// default: {
|
166
|
+
// lesson: FactoryGuy.belongsTo('lesson')
|
167
|
+
// }
|
168
|
+
//})
|
169
|
+
//
|
170
|
+
//FactoryGuy.define('lesson', {
|
171
|
+
// default: {
|
172
|
+
// steps: FactoryGuy.hasMany('step', 2)
|
173
|
+
// }
|
174
|
+
//})
|
175
|
+
//
|
176
|
+
//FactoryGuy.define('step', {
|
177
|
+
// default: {}
|
178
|
+
//})
|
179
|
+
|
132
180
|
Company = DS.Model.extend({
|
133
181
|
name: DS.attr('string'),
|
134
182
|
profile: DS.belongsTo('profile'),
|
135
|
-
users: DS.hasMany('user', {async: true, inverse: 'company'})
|
183
|
+
users: DS.hasMany('user', {async: true, inverse: 'company'}),
|
184
|
+
projects: DS.hasMany('project', {async: true})
|
136
185
|
});
|
137
186
|
|
138
187
|
SmallCompany = Company.extend({
|
139
188
|
owner: DS.belongsTo('user', {async: true}),
|
140
|
-
projects: DS.hasMany('project'
|
189
|
+
projects: DS.hasMany('project')
|
141
190
|
});
|
142
191
|
|
143
192
|
Group = DS.Model.extend({
|
@@ -204,7 +253,7 @@ Property = DS.Model.extend({
|
|
204
253
|
});
|
205
254
|
User = DS.Model.extend({
|
206
255
|
name: DS.attr('string'),
|
207
|
-
company: DS.belongsTo('company', {async: true, inverse: 'users'}),
|
256
|
+
company: DS.belongsTo('company', {async: true, inverse: 'users', polymorphic: true}),
|
208
257
|
properties: DS.hasMany('property', {async: true, inverse: 'owners'}),
|
209
258
|
projects: DS.hasMany('project'),
|
210
259
|
hats: DS.hasMany('hat', {polymorphic: true})
|
@@ -432,8 +432,11 @@ FactoryGuy = {
|
|
432
432
|
if (store.usingFixtureAdapter()) {
|
433
433
|
modelType.FIXTURES = [];
|
434
434
|
}
|
435
|
-
|
435
|
+
Ember.run(function(){
|
436
|
+
store.unloadAll(modelType);
|
437
|
+
})
|
436
438
|
} catch (e) {
|
439
|
+
// console.log(e)
|
437
440
|
}
|
438
441
|
}
|
439
442
|
},
|
@@ -609,6 +612,7 @@ DS.Store.reopen({
|
|
609
612
|
if (relationship.kind == 'belongsTo') {
|
610
613
|
var belongsToRecord = fixture[relationship.key];
|
611
614
|
if (Ember.typeOf(belongsToRecord) == 'object') {
|
615
|
+
store.findEmbeddedAssociationsForRESTAdapter(relationship.type, belongsToRecord);
|
612
616
|
belongsToRecord = store.push(relationship.type, belongsToRecord);
|
613
617
|
fixture[relationship.key] = belongsToRecord;
|
614
618
|
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ember-data-factory-guy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Sudol
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-10-
|
12
|
+
date: 2014-10-27 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|