ember-data-factory-guy 0.6.3 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba327d4ab533e2f7963019b5f361b64437e4a44b
4
- data.tar.gz: 14d0e116fc4b25cf21f2c6530d310ae20c0c8bb1
3
+ metadata.gz: 893b813e5c6ef5119e39948266640355ba86f352
4
+ data.tar.gz: 0c6b49d31ecb172835f646f5ca9db9ac87430874
5
5
  SHA512:
6
- metadata.gz: 37b8348c03752999359f5a13b43430efcb460b8f3b6d3e8188da34e38f6d476b2d869eb46f5ac97d545903b7f81c87c23b41b8be69e9b941de6cc77c0af8d265
7
- data.tar.gz: 3260af6bd6998457f22cee06f52005aa069fc0a870e10d4a65462059c12dd198815e3270934b12af4df0fbedd831856e4a98e902c259d51abe2c7d1033fd874d
6
+ metadata.gz: 1ebf5ca9fb86b002caee4626b85ac349e3c7cadd324f5d933eb9aaa2adc32df17fe897cdfd30db878a68a78a9e3d7a9f479f9b1070142cf65b990e3fe3ccf97c
7
+ data.tar.gz: edf981f537126c5dfde50ceea4162c8b84218282120b4301e991e6b9b813c160a15e79e5a4ebab9bfa04b4a4d05fae3cd0b062d7cfeb8ffc24c0a0413df55783
data/README.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Ember Data Factory Guy [![Build Status](https://secure.travis-ci.org/danielspaniel/ember-data-factory-guy.png?branch=master)](http://travis-ci.org/danielspaniel/ember-data-factory-guy)
2
2
 
3
+ *NOTE*
4
+
5
+ ember-data is changing the way they are doing relationships in 1.0.0-beta.10 and above
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
+ ( or below ) of ember-data-factory-guy.
8
+
9
+ ember-data-factory-guy version 0.7.0 and above will work on the newer versions of ember-data ( 1.0.0-beta.10 and above. )
10
+
11
+
3
12
  ## Using as Gem
4
13
 
5
14
  To Use with in Rails project or project with sprockets:
@@ -10,6 +19,12 @@ In Gemfile:
10
19
  gem 'ember-data-factory-guy', group: test
11
20
  ```
12
21
 
22
+ or for particular version:
23
+
24
+ ```ruby
25
+ gem 'ember-data-factory-guy', '0.7.0', group: test
26
+ ```
27
+
13
28
  then:
14
29
 
15
30
  ```
@@ -36,6 +51,16 @@ Add as one of your dependencies in bower.json file:
36
51
  }
37
52
  ```
38
53
 
54
+ or for particular version:
55
+
56
+ ```json
57
+ "dependencies": {
58
+ "foo-dependency": "latest",
59
+ "other-foo-dependency": "latest",
60
+ "ember-data-factory-guy": "0.7.0"
61
+ }
62
+ ```
63
+
39
64
  then:
40
65
  ```
41
66
  $ bower install
data/bower.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-data-factory-guy",
3
- "version": "0.6.3",
3
+ "version": "0.7.0",
4
4
  "authors": [
5
5
  "Daniel Sudol <dansudol@yahoo.com>",
6
6
  "Opak Alex <opak.alexandr@gmail.com>"
@@ -17,9 +17,9 @@
17
17
 
18
18
  "dependencies": {
19
19
  "handlebars": "~1.1.2",
20
- "jquery": "1.10.2",
21
- "ember": "~1.6.0",
22
- "ember-data": "~1.0.0-beta.7",
20
+ "jquery": "2.0.3",
21
+ "ember": "1.7.0",
22
+ "ember-data": "1.0.0-beta.10",
23
23
  "chance": "latest",
24
24
  "qunit": "latest"
25
25
  },
@@ -495,7 +495,7 @@ DS.Store.reopen({
495
495
 
496
496
  var model;
497
497
  Em.run(function () {
498
- store.findEmbeddedBelongsToAssociationsForRESTAdapter(modelType, fixture);
498
+ store.findEmbeddedAssociationsForRESTAdapter(modelType, fixture);
499
499
  if (fixture.type) {
500
500
  // assuming its polymorphic if there is a type attribute
501
501
  // is this too bold an assumption?
@@ -503,7 +503,7 @@ DS.Store.reopen({
503
503
  modelType = store.modelFor(modelName);
504
504
  }
505
505
  model = store.push(modelName, fixture);
506
- store.setAssociationsForRESTAdapter(modelType, modelName, model);
506
+ // store.setAssociationsForRESTAdapter(modelType, modelName, model);
507
507
  });
508
508
  return model;
509
509
  }
@@ -603,7 +603,7 @@ DS.Store.reopen({
603
603
  @param modelType
604
604
  @param fixture
605
605
  */
606
- findEmbeddedBelongsToAssociationsForRESTAdapter: function (modelType, fixture) {
606
+ findEmbeddedAssociationsForRESTAdapter: function (modelType, fixture) {
607
607
  var store = this;
608
608
  Ember.get(modelType, 'relationshipsByName').forEach(function (name, relationship) {
609
609
  if (relationship.kind == 'belongsTo') {
@@ -629,94 +629,6 @@ DS.Store.reopen({
629
629
  })
630
630
  },
631
631
 
632
- /**
633
- For the REST type models:
634
-
635
- For example if a user hasMany projects, then set the user
636
- on each project that the user hasMany of, so that the project
637
- now has the belongsTo user association setup. As in this scenario:
638
-
639
- ```js
640
- var project = store.makeFixture('project');
641
- var user = store.makeFixture('user', {projects: [project]});
642
- ```
643
-
644
- Or if you make a user, then a project with that user, then set the project
645
- in the users list of 'projects' it hasMany of. As in this scenario:
646
-
647
- ```js
648
- var user = store.makeFixture('user');
649
- var project = store.makeFixture('project', {user: user});
650
- ```
651
-
652
- @param {DS.Model} modelType model type like 'User'
653
- @param {String} modelName model name like 'user'
654
- @param {DS.Model} model model to check for needed association assignments
655
- */
656
- setAssociationsForRESTAdapter: function (modelType, modelName, model) {
657
- var self = this;
658
-
659
- Ember.get(modelType, 'relationshipsByName').forEach(function (name, relationship) {
660
- if (relationship.kind == 'hasMany') {
661
- var children = model.get(name) || [];
662
- children.forEach(function (child) {
663
- var belongsToName = self.findRelationshipName(
664
- 'belongsTo',
665
- child.constructor,
666
- model
667
- );
668
- var hasManyName = self.findRelationshipName(
669
- 'hasMany',
670
- child.constructor,
671
- model
672
- );
673
- var inverseName = (relationship.options && relationship.options.inverse)
674
- if (belongsToName) {
675
- child.set(belongsToName || inverseName, model);
676
- } else if (hasManyName) {
677
- relation = child.get(hasManyName || inverseName) || [];
678
- relation.pushObject(model)
679
- }
680
- })
681
- }
682
-
683
- if (relationship.kind == 'belongsTo') {
684
- var belongsToRecord = model.get(name);
685
- if (belongsToRecord) {
686
- var setAssociations = function() {
687
- var hasManyName = self.findRelationshipName(
688
- 'hasMany',
689
- belongsToRecord.constructor,
690
- model
691
- );
692
- if (hasManyName) {
693
- belongsToRecord.get(hasManyName).addObject(model);
694
- return;
695
- }
696
- var oneToOneName = self.findRelationshipName(
697
- 'belongsTo',
698
- belongsToRecord.constructor,
699
- model
700
- );
701
- // Guard against a situation where a model can belong to itself.
702
- // Do not want to set the belongsTo on this case.
703
- if (oneToOneName && !(belongsToRecord.constructor == model.constructor)) {
704
- belongsToRecord.set(oneToOneName, model);
705
- }
706
- }
707
- if (belongsToRecord.then) {
708
- belongsToRecord.then(function(record) {
709
- belongsToRecord = record;
710
- setAssociations();
711
- })
712
- } else {
713
- setAssociations();
714
- }
715
- }
716
- }
717
- })
718
- },
719
-
720
632
  findRelationshipName: function (kind, belongToModelType, childModel) {
721
633
  var relationshipName;
722
634
  Ember.get(belongToModelType, 'relationshipsByName').forEach(
@@ -761,95 +673,6 @@ DS.Store.reopen({
761
673
  }
762
674
  });
763
675
 
764
-
765
- DS.FixtureAdapter.reopen({
766
-
767
- /**
768
- Overriding createRecord to add the record created to the
769
- hashMany records for all of the records that this record belongsTo.
770
-
771
- For example:
772
-
773
- If models are defined like so:
774
-
775
- User = DS.Model.extend({
776
- projects: DS.hasMany('project')
777
- })
778
-
779
- Project = DS.Model.extend({
780
- user: DS.belongsTo('user')
781
- })
782
-
783
- and you create a project record with a user defined:
784
- store.createRecord('project', {user: user})
785
-
786
- this method will take the new project created and add it to the user's 'projects'
787
- hasMany array.
788
-
789
- And a full code example:
790
-
791
- var userJson = store.makeFixture('user');
792
-
793
- store.find('user', userJson.id).then(function(user) {
794
- store.createRecord('project', {user: user}).save()
795
- .then( function(project) {
796
- // user.get('projects.length') == 1;
797
- })
798
- })
799
-
800
- @method createRecord
801
- @param {DS.Store} store
802
- @param {subclass of DS.Model} type
803
- @param {DS.Model} record
804
- @return {Promise} promise
805
- */
806
- createRecord: function (store, type, record) {
807
- var promise = this._super(store, type, record);
808
- promise.then(function () {
809
- Em.RSVP.Promise.resolve(Ember.get(type, 'relationshipNames')).then(function (relationShips){
810
- if (relationShips.belongsTo) {
811
- relationShips.belongsTo.forEach(function (relationship) {
812
- Em.RSVP.Promise.resolve(record.get(relationship)).then(function(belongsToRecord){
813
- if (belongsToRecord) {
814
- var hasManyName = store.findRelationshipName(
815
- 'hasMany',
816
- belongsToRecord.constructor,
817
- record
818
- );
819
- if (hasManyName) {
820
- Ember.RSVP.resolve(belongsToRecord.get(hasManyName)).then (function(relationship){
821
- relationship.addObject(record);
822
- });
823
- }
824
- }
825
- });
826
- });
827
- }
828
- if (relationShips.hasMany) {
829
- relationShips.hasMany.forEach(function (relationship) {
830
- Em.RSVP.Promise.resolve(record.get(relationship)).then(function(belongsToRecord){
831
- if (belongsToRecord && belongsToRecord.get('length') > 0) {
832
- var hasManyName = store.findRelationshipName(
833
- 'hasMany',
834
- belongsToRecord.get('firstObject').constructor,
835
- record
836
- );
837
- belongsToRecord.forEach(function (child){
838
- Em.RSVP.resolve(child.get(hasManyName)).then( function(value) {
839
- value.addObjects(record);
840
- });
841
- });
842
- }
843
- });
844
- })
845
- }
846
- });
847
- });
848
-
849
- return promise;
850
- }
851
- })
852
-
853
676
  FactoryGuyTestMixin = Em.Mixin.create({
854
677
 
855
678
  // Pass in the app root, which typically is App.
@@ -976,11 +799,18 @@ FactoryGuyTestMixin = Em.Mixin.create({
976
799
  },
977
800
 
978
801
 
979
- handleSideLoadFind: function (modelName, json) {
980
- var responseJson = this.buildAjaxHttpResponse(name, opts);
981
- var id = responseJson[modelName].id
802
+ handleSideloadFind: function (modelName, json, sideload) {
803
+ var id = json.id
982
804
  var url = this.buildURL(modelName, id);
805
+ var responseJson = {};
806
+ responseJson[modelName] = json;
807
+ $.extend(responseJson, sideload)
983
808
 
809
+ this.stubEndpointForHttpRequest(
810
+ url,
811
+ responseJson,
812
+ {type: 'GET', status: (status || 200)}
813
+ )
984
814
  },
985
815
 
986
816
 
@@ -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.findEmbeddedBelongsToAssociationsForRESTAdapter(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(name,relationship){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)}}})},findEmbeddedBelongsToAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(name,relationship){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"&&Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.forEach(function(record){var record=store.push(relationship.type,record);records.push(record)});fixture[relationship.key]=records}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(name,relationship){if(relationship.kind=="hasMany"){var children=model.get(name)||[];children.forEach(function(child){var belongsToName=self.findRelationshipName("belongsTo",child.constructor,model);var hasManyName=self.findRelationshipName("hasMany",child.constructor,model);var inverseName=relationship.options&&relationship.options.inverse;if(belongsToName){child.set(belongsToName||inverseName,model)}else if(hasManyName){relation=child.get(hasManyName||inverseName)||[];relation.pushObject(model)}})}if(relationship.kind=="belongsTo"){var belongsToRecord=model.get(name);if(belongsToRecord){var setAssociations=function(){var hasManyName=self.findRelationshipName("hasMany",belongsToRecord.constructor,model);if(hasManyName){belongsToRecord.get(hasManyName).addObject(model);return}var oneToOneName=self.findRelationshipName("belongsTo",belongsToRecord.constructor,model);if(oneToOneName&&!(belongsToRecord.constructor==model.constructor)){belongsToRecord.set(oneToOneName,model)}};if(belongsToRecord.then){belongsToRecord.then(function(record){belongsToRecord=record;setAssociations()})}else{setAssociations()}}}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(name,relationship){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(name,relationship){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)}}});DS.FixtureAdapter.reopen({createRecord:function(store,type,record){var promise=this._super(store,type,record);promise.then(function(){Em.RSVP.Promise.resolve(Ember.get(type,"relationshipNames")).then(function(relationShips){if(relationShips.belongsTo){relationShips.belongsTo.forEach(function(relationship){Em.RSVP.Promise.resolve(record.get(relationship)).then(function(belongsToRecord){if(belongsToRecord){var hasManyName=store.findRelationshipName("hasMany",belongsToRecord.constructor,record);if(hasManyName){Ember.RSVP.resolve(belongsToRecord.get(hasManyName)).then(function(relationship){relationship.addObject(record)})}}})})}if(relationShips.hasMany){relationShips.hasMany.forEach(function(relationship){Em.RSVP.Promise.resolve(record.get(relationship)).then(function(belongsToRecord){if(belongsToRecord&&belongsToRecord.get("length")>0){var hasManyName=store.findRelationshipName("hasMany",belongsToRecord.get("firstObject").constructor,record);belongsToRecord.forEach(function(child){Em.RSVP.resolve(child.get(hasManyName)).then(function(value){value.addObjects(record)})})}})})}})});return promise}});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){var responseJson=this.buildAjaxHttpResponse(name,opts);var id=responseJson[modelName].id;var url=this.buildURL(modelName,id)},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=[]}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)});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(name,relationship){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}console.log("belongsToRecord",belongsToRecord);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(name,relationship){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"&&Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.forEach(function(record){var record=store.push(relationship.type,record);records.push(record)});fixture[relationship.key]=records}}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(name,relationship){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(name,relationship){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())}});
@@ -78,18 +78,19 @@
78
78
  // cache the promise so we can use it
79
79
  // when we come back and don't need to rebuild
80
80
  // the relationship.
81
- set(rel, 'promise', resolver.promise);
82
- return rel;
81
+ // set(rel, 'promise', resolver.promise);
82
+ return relationship;
83
+ // return rel;
83
84
  });
84
85
  }
85
-
86
- var promise = relationship.get('promise').then(function() {
87
- return relationship;
88
- }, null, "DS: Async hasMany records received");
89
-
90
- return DS.PromiseArray.create({
91
- promise: promise
92
- });
86
+ return relationship;
87
+ // var promise = relationship.get('promise').then(function() {
88
+ // return relationship;
89
+ // }, null, "DS: Async hasMany records received");
90
+ //
91
+ // return DS.PromiseArray.create({
92
+ // promise: promise
93
+ // });
93
94
  }
94
95
 
95
96
  return buildRelationship(this, key, options, function(store, data) {
@@ -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.6.3"
4
+ s.version = "0.7.0"
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-data-factory-guy",
3
- "version": "0.6.3",
3
+ "version": "0.7.0",
4
4
  "authors": [
5
5
  "Daniel Sudol <dansudol@yahoo.com>",
6
6
  "Opak Alex <opak.alexandr@gmail.com>"
@@ -124,11 +124,18 @@ FactoryGuyTestMixin = Em.Mixin.create({
124
124
  },
125
125
 
126
126
 
127
- handleSideLoadFind: function (modelName, json) {
128
- var responseJson = this.buildAjaxHttpResponse(name, opts);
129
- var id = responseJson[modelName].id
127
+ handleSideloadFind: function (modelName, json, sideload) {
128
+ var id = json.id
130
129
  var url = this.buildURL(modelName, id);
130
+ var responseJson = {};
131
+ responseJson[modelName] = json;
132
+ $.extend(responseJson, sideload)
131
133
 
134
+ this.stubEndpointForHttpRequest(
135
+ url,
136
+ responseJson,
137
+ {type: 'GET', status: (status || 200)}
138
+ )
132
139
  },
133
140
 
134
141
 
data/src/has_many.js CHANGED
@@ -78,18 +78,19 @@
78
78
  // cache the promise so we can use it
79
79
  // when we come back and don't need to rebuild
80
80
  // the relationship.
81
- set(rel, 'promise', resolver.promise);
82
- return rel;
81
+ // set(rel, 'promise', resolver.promise);
82
+ return relationship;
83
+ // return rel;
83
84
  });
84
85
  }
85
-
86
- var promise = relationship.get('promise').then(function() {
87
- return relationship;
88
- }, null, "DS: Async hasMany records received");
89
-
90
- return DS.PromiseArray.create({
91
- promise: promise
92
- });
86
+ return relationship;
87
+ // var promise = relationship.get('promise').then(function() {
88
+ // return relationship;
89
+ // }, null, "DS: Async hasMany records received");
90
+ //
91
+ // return DS.PromiseArray.create({
92
+ // promise: promise
93
+ // });
93
94
  }
94
95
 
95
96
  return buildRelationship(this, key, options, function(store, data) {
data/src/store.js CHANGED
@@ -29,7 +29,7 @@ DS.Store.reopen({
29
29
 
30
30
  var model;
31
31
  Em.run(function () {
32
- store.findEmbeddedBelongsToAssociationsForRESTAdapter(modelType, fixture);
32
+ store.findEmbeddedAssociationsForRESTAdapter(modelType, fixture);
33
33
  if (fixture.type) {
34
34
  // assuming its polymorphic if there is a type attribute
35
35
  // is this too bold an assumption?
@@ -37,7 +37,7 @@ DS.Store.reopen({
37
37
  modelType = store.modelFor(modelName);
38
38
  }
39
39
  model = store.push(modelName, fixture);
40
- store.setAssociationsForRESTAdapter(modelType, modelName, model);
40
+ // store.setAssociationsForRESTAdapter(modelType, modelName, model);
41
41
  });
42
42
  return model;
43
43
  }
@@ -137,7 +137,7 @@ DS.Store.reopen({
137
137
  @param modelType
138
138
  @param fixture
139
139
  */
140
- findEmbeddedBelongsToAssociationsForRESTAdapter: function (modelType, fixture) {
140
+ findEmbeddedAssociationsForRESTAdapter: function (modelType, fixture) {
141
141
  var store = this;
142
142
  Ember.get(modelType, 'relationshipsByName').forEach(function (name, relationship) {
143
143
  if (relationship.kind == 'belongsTo') {
@@ -163,94 +163,6 @@ DS.Store.reopen({
163
163
  })
164
164
  },
165
165
 
166
- /**
167
- For the REST type models:
168
-
169
- For example if a user hasMany projects, then set the user
170
- on each project that the user hasMany of, so that the project
171
- now has the belongsTo user association setup. As in this scenario:
172
-
173
- ```js
174
- var project = store.makeFixture('project');
175
- var user = store.makeFixture('user', {projects: [project]});
176
- ```
177
-
178
- Or if you make a user, then a project with that user, then set the project
179
- in the users list of 'projects' it hasMany of. As in this scenario:
180
-
181
- ```js
182
- var user = store.makeFixture('user');
183
- var project = store.makeFixture('project', {user: user});
184
- ```
185
-
186
- @param {DS.Model} modelType model type like 'User'
187
- @param {String} modelName model name like 'user'
188
- @param {DS.Model} model model to check for needed association assignments
189
- */
190
- setAssociationsForRESTAdapter: function (modelType, modelName, model) {
191
- var self = this;
192
-
193
- Ember.get(modelType, 'relationshipsByName').forEach(function (name, relationship) {
194
- if (relationship.kind == 'hasMany') {
195
- var children = model.get(name) || [];
196
- children.forEach(function (child) {
197
- var belongsToName = self.findRelationshipName(
198
- 'belongsTo',
199
- child.constructor,
200
- model
201
- );
202
- var hasManyName = self.findRelationshipName(
203
- 'hasMany',
204
- child.constructor,
205
- model
206
- );
207
- var inverseName = (relationship.options && relationship.options.inverse)
208
- if (belongsToName) {
209
- child.set(belongsToName || inverseName, model);
210
- } else if (hasManyName) {
211
- relation = child.get(hasManyName || inverseName) || [];
212
- relation.pushObject(model)
213
- }
214
- })
215
- }
216
-
217
- if (relationship.kind == 'belongsTo') {
218
- var belongsToRecord = model.get(name);
219
- if (belongsToRecord) {
220
- var setAssociations = function() {
221
- var hasManyName = self.findRelationshipName(
222
- 'hasMany',
223
- belongsToRecord.constructor,
224
- model
225
- );
226
- if (hasManyName) {
227
- belongsToRecord.get(hasManyName).addObject(model);
228
- return;
229
- }
230
- var oneToOneName = self.findRelationshipName(
231
- 'belongsTo',
232
- belongsToRecord.constructor,
233
- model
234
- );
235
- // Guard against a situation where a model can belong to itself.
236
- // Do not want to set the belongsTo on this case.
237
- if (oneToOneName && !(belongsToRecord.constructor == model.constructor)) {
238
- belongsToRecord.set(oneToOneName, model);
239
- }
240
- }
241
- if (belongsToRecord.then) {
242
- belongsToRecord.then(function(record) {
243
- belongsToRecord = record;
244
- setAssociations();
245
- })
246
- } else {
247
- setAssociations();
248
- }
249
- }
250
- }
251
- })
252
- },
253
-
254
166
  findRelationshipName: function (kind, belongToModelType, childModel) {
255
167
  var relationshipName;
256
168
  Ember.get(belongToModelType, 'relationshipsByName').forEach(
@@ -294,92 +206,3 @@ DS.Store.reopen({
294
206
  }
295
207
  }
296
208
  });
297
-
298
-
299
- DS.FixtureAdapter.reopen({
300
-
301
- /**
302
- Overriding createRecord to add the record created to the
303
- hashMany records for all of the records that this record belongsTo.
304
-
305
- For example:
306
-
307
- If models are defined like so:
308
-
309
- User = DS.Model.extend({
310
- projects: DS.hasMany('project')
311
- })
312
-
313
- Project = DS.Model.extend({
314
- user: DS.belongsTo('user')
315
- })
316
-
317
- and you create a project record with a user defined:
318
- store.createRecord('project', {user: user})
319
-
320
- this method will take the new project created and add it to the user's 'projects'
321
- hasMany array.
322
-
323
- And a full code example:
324
-
325
- var userJson = store.makeFixture('user');
326
-
327
- store.find('user', userJson.id).then(function(user) {
328
- store.createRecord('project', {user: user}).save()
329
- .then( function(project) {
330
- // user.get('projects.length') == 1;
331
- })
332
- })
333
-
334
- @method createRecord
335
- @param {DS.Store} store
336
- @param {subclass of DS.Model} type
337
- @param {DS.Model} record
338
- @return {Promise} promise
339
- */
340
- createRecord: function (store, type, record) {
341
- var promise = this._super(store, type, record);
342
- promise.then(function () {
343
- Em.RSVP.Promise.resolve(Ember.get(type, 'relationshipNames')).then(function (relationShips){
344
- if (relationShips.belongsTo) {
345
- relationShips.belongsTo.forEach(function (relationship) {
346
- Em.RSVP.Promise.resolve(record.get(relationship)).then(function(belongsToRecord){
347
- if (belongsToRecord) {
348
- var hasManyName = store.findRelationshipName(
349
- 'hasMany',
350
- belongsToRecord.constructor,
351
- record
352
- );
353
- if (hasManyName) {
354
- Ember.RSVP.resolve(belongsToRecord.get(hasManyName)).then (function(relationship){
355
- relationship.addObject(record);
356
- });
357
- }
358
- }
359
- });
360
- });
361
- }
362
- if (relationShips.hasMany) {
363
- relationShips.hasMany.forEach(function (relationship) {
364
- Em.RSVP.Promise.resolve(record.get(relationship)).then(function(belongsToRecord){
365
- if (belongsToRecord && belongsToRecord.get('length') > 0) {
366
- var hasManyName = store.findRelationshipName(
367
- 'hasMany',
368
- belongsToRecord.get('firstObject').constructor,
369
- record
370
- );
371
- belongsToRecord.forEach(function (child){
372
- Em.RSVP.resolve(child.get(hasManyName)).then( function(value) {
373
- value.addObjects(record);
374
- });
375
- });
376
- }
377
- });
378
- })
379
- }
380
- });
381
- });
382
-
383
- return promise;
384
- }
385
- })