ember-data-factory-guy 0.8.4 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -6
- data/bower.json +1 -1
- data/dist/amd/factory-guy.js +37 -2
- data/dist/ember-data-factory-guy.js +37 -2
- 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 +35 -2
- data/src/store.js +2 -2
- data/tests/factory_guy_test.js +23 -0
- data/vendor/assets/javascripts/ember_data_factory_guy.js +38 -3
- 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: e9608d21a251538039579b12d49d7250e3158766
|
4
|
+
data.tar.gz: f68ab6412e121fb7c52d0045494b2ab33f9afa6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 707333b8c3a9cb12034cdc8140e36323d5d5b1a5fcd4a701efd256c0feb506965db976ef352ea1e9244b056bf568cc21fb34786351ffa9eb650dc98b0a6c6d94
|
7
|
+
data.tar.gz: 7056e2ed647ee8c525771d378e3c9dcbe0f127ce876d33568d4479922776fb943eb9462376ad52f98267c87be6edfaac92750730305985618fb14c6eb84fa02b
|
data/README.md
CHANGED
@@ -9,12 +9,12 @@ of ember-data-factory-guy.
|
|
9
9
|
- Versions:
|
10
10
|
- 0.6.4 -> ember-data-1.0.0-beta.8 and under
|
11
11
|
- 0.7.1.1 -> ember-data-1.0.0-beta.10
|
12
|
-
- 0.8.
|
13
|
-
- 0.8.
|
12
|
+
- 0.8.4 -> ember-data-1.0.0-beta.11
|
13
|
+
- 0.8.4 -> ember-data-1.0.0-beta.12
|
14
14
|
|
15
|
-
**For versions ( 0.7.1 -> 0.8.
|
15
|
+
**For versions ( 0.7.1 -> 0.8.4 ), support for the fixture adapter is currently broken.**
|
16
16
|
|
17
|
-
*Version 0.8.
|
17
|
+
*Version 0.8.4 has many bug fixes and improvements that the earlier versions don't have, so
|
18
18
|
hopefully you can switch to newer version of ember-data and therefore the best
|
19
19
|
ember-data-factory-guy version, but if not, send me bug report and I will try and go back
|
20
20
|
and fix the older version you are using.*
|
@@ -41,7 +41,7 @@ gem 'ember-data-factory-guy', group: test
|
|
41
41
|
or for particular version:
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
gem 'ember-data-factory-guy', '0.8.
|
44
|
+
gem 'ember-data-factory-guy', '0.8.4', group: test
|
45
45
|
```
|
46
46
|
|
47
47
|
then:
|
@@ -76,7 +76,7 @@ or for particular version:
|
|
76
76
|
"dependencies": {
|
77
77
|
"foo-dependency": "latest",
|
78
78
|
"other-foo-dependency": "latest",
|
79
|
-
"ember-data-factory-guy": "0.8.
|
79
|
+
"ember-data-factory-guy": "0.8.4"
|
80
80
|
}
|
81
81
|
```
|
82
82
|
|
data/bower.json
CHANGED
data/dist/amd/factory-guy.js
CHANGED
@@ -441,12 +441,45 @@ var FactoryGuy = {
|
|
441
441
|
@returns {Object} json fixture data
|
442
442
|
*/
|
443
443
|
pushFixture: function (modelClass, fixture) {
|
444
|
+
var index;
|
444
445
|
if (!modelClass.FIXTURES) {
|
445
446
|
modelClass.FIXTURES = [];
|
446
447
|
}
|
447
|
-
|
448
|
+
|
449
|
+
index = this.indexOfFixture(modelClass.FIXTURES, fixture);
|
450
|
+
|
451
|
+
if (index > -1) {
|
452
|
+
modelClass.FIXTURES[index] = fixture;
|
453
|
+
} else {
|
454
|
+
modelClass.FIXTURES.push(fixture);
|
455
|
+
}
|
456
|
+
|
448
457
|
return fixture;
|
449
458
|
},
|
459
|
+
|
460
|
+
/**
|
461
|
+
Used in compliment with pushFixture in order to
|
462
|
+
ensure we don't push duplicate fixtures
|
463
|
+
|
464
|
+
@private
|
465
|
+
@param {Array} fixtures
|
466
|
+
@param {String|Integer} id of fixture to find
|
467
|
+
@returns {Object} fixture
|
468
|
+
*/
|
469
|
+
indexOfFixture: function(fixtures, fixture) {
|
470
|
+
var index = -1,
|
471
|
+
id = fixture.id + '';
|
472
|
+
Ember.A(fixtures).find(function(r, i) {
|
473
|
+
if ('' + Ember.get(r, 'id') === id) {
|
474
|
+
index = i;
|
475
|
+
return true;
|
476
|
+
} else {
|
477
|
+
return false;
|
478
|
+
}
|
479
|
+
});
|
480
|
+
return index;
|
481
|
+
},
|
482
|
+
|
450
483
|
/**
|
451
484
|
Clears all model definitions
|
452
485
|
*/
|
@@ -456,6 +489,7 @@ var FactoryGuy = {
|
|
456
489
|
}
|
457
490
|
}
|
458
491
|
};
|
492
|
+
|
459
493
|
(function () {
|
460
494
|
DS.Store.reopen({
|
461
495
|
/**
|
@@ -694,7 +728,7 @@ var FactoryGuy = {
|
|
694
728
|
*/
|
695
729
|
pushPayload: function (type, payload) {
|
696
730
|
if (this.usingFixtureAdapter()) {
|
697
|
-
var model = this.modelFor(
|
731
|
+
var model = this.modelFor(type);
|
698
732
|
FactoryGuy.pushFixture(model, payload);
|
699
733
|
} else {
|
700
734
|
this._super(type, payload);
|
@@ -702,6 +736,7 @@ var FactoryGuy = {
|
|
702
736
|
}
|
703
737
|
});
|
704
738
|
})();
|
739
|
+
|
705
740
|
var FactoryGuyTestMixin = Em.Mixin.create({
|
706
741
|
// Pass in the app root, which typically is App.
|
707
742
|
setup: function (app) {
|
@@ -436,12 +436,45 @@ var FactoryGuy = {
|
|
436
436
|
@returns {Object} json fixture data
|
437
437
|
*/
|
438
438
|
pushFixture: function (modelClass, fixture) {
|
439
|
+
var index;
|
439
440
|
if (!modelClass.FIXTURES) {
|
440
441
|
modelClass.FIXTURES = [];
|
441
442
|
}
|
442
|
-
|
443
|
+
|
444
|
+
index = this.indexOfFixture(modelClass.FIXTURES, fixture);
|
445
|
+
|
446
|
+
if (index > -1) {
|
447
|
+
modelClass.FIXTURES[index] = fixture;
|
448
|
+
} else {
|
449
|
+
modelClass.FIXTURES.push(fixture);
|
450
|
+
}
|
451
|
+
|
443
452
|
return fixture;
|
444
453
|
},
|
454
|
+
|
455
|
+
/**
|
456
|
+
Used in compliment with pushFixture in order to
|
457
|
+
ensure we don't push duplicate fixtures
|
458
|
+
|
459
|
+
@private
|
460
|
+
@param {Array} fixtures
|
461
|
+
@param {String|Integer} id of fixture to find
|
462
|
+
@returns {Object} fixture
|
463
|
+
*/
|
464
|
+
indexOfFixture: function(fixtures, fixture) {
|
465
|
+
var index = -1,
|
466
|
+
id = fixture.id + '';
|
467
|
+
Ember.A(fixtures).find(function(r, i) {
|
468
|
+
if ('' + Ember.get(r, 'id') === id) {
|
469
|
+
index = i;
|
470
|
+
return true;
|
471
|
+
} else {
|
472
|
+
return false;
|
473
|
+
}
|
474
|
+
});
|
475
|
+
return index;
|
476
|
+
},
|
477
|
+
|
445
478
|
/**
|
446
479
|
Clears all model definitions
|
447
480
|
*/
|
@@ -451,6 +484,7 @@ var FactoryGuy = {
|
|
451
484
|
}
|
452
485
|
}
|
453
486
|
};
|
487
|
+
|
454
488
|
(function () {
|
455
489
|
DS.Store.reopen({
|
456
490
|
/**
|
@@ -689,7 +723,7 @@ var FactoryGuy = {
|
|
689
723
|
*/
|
690
724
|
pushPayload: function (type, payload) {
|
691
725
|
if (this.usingFixtureAdapter()) {
|
692
|
-
var model = this.modelFor(
|
726
|
+
var model = this.modelFor(type);
|
693
727
|
FactoryGuy.pushFixture(model, payload);
|
694
728
|
} else {
|
695
729
|
this._super(type, payload);
|
@@ -697,6 +731,7 @@ var FactoryGuy = {
|
|
697
731
|
}
|
698
732
|
});
|
699
733
|
})();
|
734
|
+
|
700
735
|
var FactoryGuyTestMixin = Em.Mixin.create({
|
701
736
|
// Pass in the app root, which typically is App.
|
702
737
|
setup: function (app) {
|
@@ -1 +1 @@
|
|
1
|
-
var Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};var MissingSequenceError=function(message){this.toString=function(){return message}};if(FactoryGuy!==undefined){FactoryGuy.sequence=Sequence;FactoryGuy.missingSequenceError=MissingSequenceError}var ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;var sequenceName=null;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.nextId=function(){return modelId++};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(var 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=this.nextId()}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(var 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)};if(FactoryGuy!==undefined){FactoryGuy.modelDefiniton=ModelDefinition}var 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(var 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(var 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){console.log("resetModels",e)}}},pushFixture:function(modelClass,fixture){if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}modelClass.FIXTURES.push(fixture);return fixture},clear:function(opts){if(!opts){this.modelDefinitions={}}}};(function(){DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(){var store=this;var fixture=FactoryGuy.build.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(this.usingFixtureAdapter()){this.setAssociationsForFixtureAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{return store.makeModel(modelType,fixture)}},makeModel:function(modelType,fixture){var store=this,modelName=store.modelFor(modelType).typeKey,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(){var arr=[];var number=arguments[1];for(var i=0;i<number;i++){arr.push(this.makeFixture.apply(this,arguments))}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){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,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)}}})})();var 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 modelType=store.modelFor(type);var serializer=store.serializerFor(modelType.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(){var store=this.getStore();return store.makeFixture.apply(store,arguments)},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)},buildURL:function(typeName,id){var type=this.getStore().modelFor(typeName);return this.getStore().adapterFor(type).buildURL(type.typeKey,id)},handleFindMany:function(){var store=this.getStore();store.makeList.apply(store,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};responseJson[modelName]=[];var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET"})},handleCreate:function(modelName,options){var opts=options===undefined?{}:options;var succeed=opts.succeed===undefined?true:opts.succeed;var match=opts.match||{};var returnArgs=opts.returns||{};var url=this.buildURL(modelName);var definition=FactoryGuy.modelDefinitions[modelName];var httpOptions={type:"POST"};if(opts.match){var expectedRequest={};var record=this.getStore().createRecord(modelName,match);expectedRequest[modelName]=record.serialize();httpOptions.data=JSON.stringify(expectedRequest)}var modelType=this.getStore().modelFor(modelName);var responseJson={};if(succeed){if(options){responseJson[modelName]=$.extend({id:definition.nextId()},match,returnArgs);Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}}else{httpOptions.status=500}this.stubEndpointForHttpRequest(url,responseJson,httpOptions)},handleUpdate:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:succeed?200:500})},handleDelete:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:succeed?200:500})},teardown:function(){FactoryGuy.resetModels(this.getStore());$.mockjax.clear()}});if(FactoryGuy!==undefined){FactoryGuy.testMixin=FactoryGuyTestMixin}
|
1
|
+
var Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};var MissingSequenceError=function(message){this.toString=function(){return message}};if(FactoryGuy!==undefined){FactoryGuy.sequence=Sequence;FactoryGuy.missingSequenceError=MissingSequenceError}var ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;var sequenceName=null;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.nextId=function(){return modelId++};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(var 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=this.nextId()}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(var 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)};if(FactoryGuy!==undefined){FactoryGuy.modelDefiniton=ModelDefinition}var 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(var 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(var 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){console.log("resetModels",e)}}},pushFixture:function(modelClass,fixture){var index;if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}index=this.indexOfFixture(modelClass.FIXTURES,fixture);if(index>-1){modelClass.FIXTURES[index]=fixture}else{modelClass.FIXTURES.push(fixture)}return fixture},indexOfFixture:function(fixtures,fixture){var index=-1,id=fixture.id+"";Ember.A(fixtures).find(function(r,i){if(""+Ember.get(r,"id")===id){index=i;return true}else{return false}});return index},clear:function(opts){if(!opts){this.modelDefinitions={}}}};(function(){DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(){var store=this;var fixture=FactoryGuy.build.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(this.usingFixtureAdapter()){this.setAssociationsForFixtureAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{return store.makeModel(modelType,fixture)}},makeModel:function(modelType,fixture){var store=this,modelName=store.modelFor(modelType).typeKey,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(){var arr=[];var number=arguments[1];for(var i=0;i<number;i++){arr.push(this.makeFixture.apply(this,arguments))}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){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,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(type);FactoryGuy.pushFixture(model,payload)}else{this._super(type,payload)}}})})();var 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 modelType=store.modelFor(type);var serializer=store.serializerFor(modelType.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(){var store=this.getStore();return store.makeFixture.apply(store,arguments)},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)},buildURL:function(typeName,id){var type=this.getStore().modelFor(typeName);return this.getStore().adapterFor(type).buildURL(type.typeKey,id)},handleFindMany:function(){var store=this.getStore();store.makeList.apply(store,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};responseJson[modelName]=[];var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET"})},handleCreate:function(modelName,options){var opts=options===undefined?{}:options;var succeed=opts.succeed===undefined?true:opts.succeed;var match=opts.match||{};var returnArgs=opts.returns||{};var url=this.buildURL(modelName);var definition=FactoryGuy.modelDefinitions[modelName];var httpOptions={type:"POST"};if(opts.match){var expectedRequest={};var record=this.getStore().createRecord(modelName,match);expectedRequest[modelName]=record.serialize();httpOptions.data=JSON.stringify(expectedRequest)}var modelType=this.getStore().modelFor(modelName);var responseJson={};if(succeed){if(options){responseJson[modelName]=$.extend({id:definition.nextId()},match,returnArgs);Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}}else{httpOptions.status=500}this.stubEndpointForHttpRequest(url,responseJson,httpOptions)},handleUpdate:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:succeed?200:500})},handleDelete:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:succeed?200:500})},teardown:function(){FactoryGuy.resetModels(this.getStore());$.mockjax.clear()}});if(FactoryGuy!==undefined){FactoryGuy.testMixin=FactoryGuyTestMixin}
|
@@ -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.8.
|
4
|
+
s.version = "0.8.5"
|
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
@@ -268,12 +268,45 @@ var FactoryGuy = {
|
|
268
268
|
@returns {Object} json fixture data
|
269
269
|
*/
|
270
270
|
pushFixture: function (modelClass, fixture) {
|
271
|
+
var index;
|
271
272
|
if (!modelClass.FIXTURES) {
|
272
273
|
modelClass.FIXTURES = [];
|
273
274
|
}
|
274
|
-
|
275
|
+
|
276
|
+
index = this.indexOfFixture(modelClass.FIXTURES, fixture);
|
277
|
+
|
278
|
+
if (index > -1) {
|
279
|
+
modelClass.FIXTURES[index] = fixture;
|
280
|
+
} else {
|
281
|
+
modelClass.FIXTURES.push(fixture);
|
282
|
+
}
|
283
|
+
|
275
284
|
return fixture;
|
276
285
|
},
|
286
|
+
|
287
|
+
/**
|
288
|
+
Used in compliment with pushFixture in order to
|
289
|
+
ensure we don't push duplicate fixtures
|
290
|
+
|
291
|
+
@private
|
292
|
+
@param {Array} fixtures
|
293
|
+
@param {String|Integer} id of fixture to find
|
294
|
+
@returns {Object} fixture
|
295
|
+
*/
|
296
|
+
indexOfFixture: function(fixtures, fixture) {
|
297
|
+
var index = -1,
|
298
|
+
id = fixture.id + '';
|
299
|
+
Ember.A(fixtures).find(function(r, i) {
|
300
|
+
if ('' + Ember.get(r, 'id') === id) {
|
301
|
+
index = i;
|
302
|
+
return true;
|
303
|
+
} else {
|
304
|
+
return false;
|
305
|
+
}
|
306
|
+
});
|
307
|
+
return index;
|
308
|
+
},
|
309
|
+
|
277
310
|
/**
|
278
311
|
Clears all model definitions
|
279
312
|
*/
|
@@ -282,4 +315,4 @@ var FactoryGuy = {
|
|
282
315
|
this.modelDefinitions = {};
|
283
316
|
}
|
284
317
|
}
|
285
|
-
};
|
318
|
+
};
|
data/src/store.js
CHANGED
@@ -236,11 +236,11 @@
|
|
236
236
|
*/
|
237
237
|
pushPayload: function (type, payload) {
|
238
238
|
if (this.usingFixtureAdapter()) {
|
239
|
-
var model = this.modelFor(
|
239
|
+
var model = this.modelFor(type);
|
240
240
|
FactoryGuy.pushFixture(model, payload);
|
241
241
|
} else {
|
242
242
|
this._super(type, payload);
|
243
243
|
}
|
244
244
|
}
|
245
245
|
});
|
246
|
-
})();
|
246
|
+
})();
|
data/tests/factory_guy_test.js
CHANGED
@@ -203,3 +203,26 @@ test("#lookupModelForFixtureName", function() {
|
|
203
203
|
equal(FactoryGuy.lookupModelForFixtureName('fake'), undefined, "return nothing if can't find definition");
|
204
204
|
});
|
205
205
|
|
206
|
+
asyncTest("#pushFixture", function() {
|
207
|
+
var User = store.modelFor('user'),
|
208
|
+
user = store.makeFixture('user'),
|
209
|
+
duplicateUser = FactoryGuy.build('user', { id: user.id, name: 'monkey' }),
|
210
|
+
differentUser = FactoryGuy.build('user'),
|
211
|
+
usersById = {};
|
212
|
+
|
213
|
+
usersById[duplicateUser.id] = duplicateUser;
|
214
|
+
usersById[differentUser.id] = differentUser;
|
215
|
+
|
216
|
+
FactoryGuy.pushFixture(User, duplicateUser);
|
217
|
+
FactoryGuy.pushFixture(User, differentUser);
|
218
|
+
|
219
|
+
store.find('user').then(function(users) {
|
220
|
+
ok(users.get('length') === 2);
|
221
|
+
|
222
|
+
users.forEach(function(user) {
|
223
|
+
equal(user.get('name'), usersById[user.get('id')].name, "Updates model fixture if duplicate id found");
|
224
|
+
});
|
225
|
+
|
226
|
+
start();
|
227
|
+
});
|
228
|
+
});
|
@@ -436,12 +436,45 @@ var FactoryGuy = {
|
|
436
436
|
@returns {Object} json fixture data
|
437
437
|
*/
|
438
438
|
pushFixture: function (modelClass, fixture) {
|
439
|
+
var index;
|
439
440
|
if (!modelClass.FIXTURES) {
|
440
441
|
modelClass.FIXTURES = [];
|
441
442
|
}
|
442
|
-
|
443
|
+
|
444
|
+
index = this.indexOfFixture(modelClass.FIXTURES, fixture);
|
445
|
+
|
446
|
+
if (index > -1) {
|
447
|
+
modelClass.FIXTURES[index] = fixture;
|
448
|
+
} else {
|
449
|
+
modelClass.FIXTURES.push(fixture);
|
450
|
+
}
|
451
|
+
|
443
452
|
return fixture;
|
444
453
|
},
|
454
|
+
|
455
|
+
/**
|
456
|
+
Used in compliment with pushFixture in order to
|
457
|
+
ensure we don't push duplicate fixtures
|
458
|
+
|
459
|
+
@private
|
460
|
+
@param {Array} fixtures
|
461
|
+
@param {String|Integer} id of fixture to find
|
462
|
+
@returns {Object} fixture
|
463
|
+
*/
|
464
|
+
indexOfFixture: function(fixtures, fixture) {
|
465
|
+
var index = -1,
|
466
|
+
id = fixture.id + '';
|
467
|
+
Ember.A(fixtures).find(function(r, i) {
|
468
|
+
if ('' + Ember.get(r, 'id') === id) {
|
469
|
+
index = i;
|
470
|
+
return true;
|
471
|
+
} else {
|
472
|
+
return false;
|
473
|
+
}
|
474
|
+
});
|
475
|
+
return index;
|
476
|
+
},
|
477
|
+
|
445
478
|
/**
|
446
479
|
Clears all model definitions
|
447
480
|
*/
|
@@ -451,6 +484,7 @@ var FactoryGuy = {
|
|
451
484
|
}
|
452
485
|
}
|
453
486
|
};
|
487
|
+
|
454
488
|
(function () {
|
455
489
|
DS.Store.reopen({
|
456
490
|
/**
|
@@ -689,7 +723,7 @@ var FactoryGuy = {
|
|
689
723
|
*/
|
690
724
|
pushPayload: function (type, payload) {
|
691
725
|
if (this.usingFixtureAdapter()) {
|
692
|
-
var model = this.modelFor(
|
726
|
+
var model = this.modelFor(type);
|
693
727
|
FactoryGuy.pushFixture(model, payload);
|
694
728
|
} else {
|
695
729
|
this._super(type, payload);
|
@@ -697,6 +731,7 @@ var FactoryGuy = {
|
|
697
731
|
}
|
698
732
|
});
|
699
733
|
})();
|
734
|
+
|
700
735
|
var FactoryGuyTestMixin = Em.Mixin.create({
|
701
736
|
// Pass in the app root, which typically is App.
|
702
737
|
setup: function (app) {
|
@@ -1026,7 +1061,7 @@ if (FactoryGuy !== undefined) {
|
|
1026
1061
|
// console.log('request.data', requestSettings.data )
|
1027
1062
|
// console.log('handler.data', handler.data )
|
1028
1063
|
// console.log('data equal', isMockDataEqual(handler.data, requestSettings.data) )
|
1029
|
-
if
|
1064
|
+
if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) {
|
1030
1065
|
// They're not identical, do not mock this request
|
1031
1066
|
return null;
|
1032
1067
|
}
|
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.8.
|
4
|
+
version: 0.8.5
|
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-12-
|
12
|
+
date: 2014-12-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|