ember-data-factory-guy 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,7 +30,7 @@ FactoryGuy = Ember.Object.reopenClass({
30
30
  FactoryGuy.build('user') or FactoryGuy.build('bob')
31
31
 
32
32
  @param model the model to define
33
- @param config your default and specific fixtures
33
+ @param config your default and named fixtures
34
34
  */
35
35
  define: function (model, config) {
36
36
  var info = this.getModelInfo(model);
@@ -100,7 +100,9 @@ FactoryGuy = Ember.Object.reopenClass({
100
100
  var modelAttributes = modelInfo[name] || {};
101
101
  var defaultModelAttributes = modelInfo.default;
102
102
  var fixture = $.extend({}, defaultModelAttributes, modelAttributes, opts);
103
- fixture.id = this.generateId(model);
103
+ if(!fixture.id){
104
+ fixture.id = this.generateId(model);
105
+ }
104
106
  return fixture;
105
107
  },
106
108
 
@@ -118,10 +120,7 @@ FactoryGuy = Ember.Object.reopenClass({
118
120
  }
119
121
  } else {
120
122
  for (model in typeMaps) {
121
- if (typeMaps[model].type.typeKey != 'user') {
122
- // console.log(typeMaps[model].type.typeKey)
123
- store.unloadAll(typeMaps[model].type);
124
- }
123
+ store.unloadAll(typeMaps[model].type);
125
124
  }
126
125
  }
127
126
  this.modelIds = {}
@@ -142,6 +141,7 @@ FactoryGuy = Ember.Object.reopenClass({
142
141
  return fixture;
143
142
  }
144
143
  })
144
+
145
145
  DS.Store.reopen({
146
146
 
147
147
  usingFixtureAdapter: function() {
@@ -266,10 +266,10 @@ DS.FixtureAdapter.reopen({
266
266
  createRecord: function(store, type, record) {
267
267
  var promise = this._super(store, type, record);
268
268
 
269
- promise.then( function() {
270
- var hasManyName = Ember.String.pluralize(type.typeKey);
271
- var relationShips = Ember.get(type, 'relationshipNames');
272
- if (relationShips.belongsTo) {
269
+ // promise.then( function() {
270
+ // var hasManyName = Ember.String.pluralize(type.typeKey);
271
+ // var relationShips = Ember.get(type, 'relationshipNames');
272
+ // if (relationShips.belongsTo) {
273
273
  // console.log('record',record+'', type.typeKey, hasManyName);
274
274
  // relationShips.belongsTo.forEach(function (relationship) {
275
275
  // console.log(relationship, record.get(relationship)+'')
@@ -277,8 +277,8 @@ DS.FixtureAdapter.reopen({
277
277
  // console.log(relationshipForType)
278
278
  // belongsToRecord.get(hasManyName).addObject(record);
279
279
  // })
280
- }
281
- })
280
+ // }
281
+ // })
282
282
  return promise;
283
283
  }
284
284
 
@@ -359,7 +359,7 @@ FactoryGuyHelperMixin = Em.Mixin.create({
359
359
 
360
360
  handleUpdate: function (root, id) {
361
361
  this.stubEndpointForHttpRequest(
362
- "/" + Em.String.pluralize(root) + "/" + id, '{}', {type: 'PUT'}
362
+ "/" + Em.String.pluralize(root) + "/" + id, {}, {type: 'PUT'}
363
363
  )
364
364
  },
365
365
 
@@ -1 +1 @@
1
- FactoryGuy=Ember.Object.reopenClass({fixtureStore:{},fixtureLookup:{},modelIds:{},define:function(model,config){var info=this.getModelInfo(model);for(key in config){var value=config[key];info[key]=value;if(key!="default"){this.fixtureLookup[key]=model}}this.modelIds[model]=0},getModelInfo:function(model){if(!this.fixtureStore[model]){this.fixtureStore[model]={}}return this.fixtureStore[model]},lookupModelForName:function(name){var model=this.fixtureLookup[name];if(!model){if(this.fixtureStore[name]){model=name}}return model},generateId:function(model){var lastId=this.modelIds[model]||0;this.modelIds[model]=lastId+1;return this.modelIds[model]},build:function(name,opts){var model=this.lookupModelForName(name);if(!model){throw new Error("can't find that factory named ["+name+"]")}var modelInfo=this.fixtureStore[model];var modelAttributes=modelInfo[name]||{};var defaultModelAttributes=modelInfo.default;var fixture=$.extend({},defaultModelAttributes,modelAttributes,opts);fixture.id=this.generateId(model);return fixture},resetModels:function(store){var typeMaps=store.typeMaps;if(store.usingFixtureAdapter()){for(typeKey in this.fixtureStore){var modelType=store.modelFor(typeKey);modelType.FIXTURES=[];store.unloadAll(modelType)}}else{for(model in typeMaps){if(typeMaps[model].type.typeKey!="user"){store.unloadAll(typeMaps[model].type)}}}this.modelIds={}},pushFixture:function(modelClass,fixture){if(!modelClass["FIXTURES"]){modelClass["FIXTURES"]=[]}modelClass["FIXTURES"].push(fixture);return fixture}});DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(name,options){var modelName=FactoryGuy.lookupModelForName(name);var fixture=FactoryGuy.build(name,options);var modelType=this.modelFor(modelName);if(this.usingFixtureAdapter()){this.setBelongsToFixturesAssociation(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var self=this;var model;Em.run(function(){model=self.push(modelName,fixture);self.setBelongsToRestAssociation(modelType,modelName,model)});return model}},setBelongsToFixturesAssociation:function(modelType,modelName,parentFixture){var store=this;var adapter=this.adapterFor("application");var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(relationship){var hasManyModel=store.modelFor(Em.String.singularize(relationship));if(parentFixture[relationship]){parentFixture[relationship].forEach(function(id){var hasManyfixtures=adapter.fixturesForType(hasManyModel);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=parentFixture.id})}})}},setBelongsToRestAssociation:function(modelType,modelName,parent){var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(name){var children=parent.get(name);if(children.get("length")>0){children.forEach(function(child){child.set(modelName,parent)})}})}},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(){var hasManyName=Ember.String.pluralize(type.typeKey);var relationShips=Ember.get(type,"relationshipNames");if(relationShips.belongsTo){}});return promise}});FactoryGuyHelperMixin=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},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)},handleCreate:function(name,opts){var model=FactoryGuy.lookupModelForName(name);this.stubEndpointForHttpRequest("/"+Em.String.pluralize(model),this.buildAjaxResponse(name,opts),{type:"POST"})},buildAjaxResponse:function(name,opts){var fixture=FactoryGuy.build(name,opts);var model=FactoryGuy.lookupModelForName(name);var hash={};hash[model]=fixture;return hash},handleUpdate:function(root,id){this.stubEndpointForHttpRequest("/"+Em.String.pluralize(root)+"/"+id,"{}",{type:"PUT"})},handleDelete:function(root,id){this.stubEndpointForHttpRequest("/"+Em.String.pluralize(root)+"/"+id,{},{type:"DELETE"})},teardown:function(){FactoryGuy.resetModels(this.getStore())}});
1
+ FactoryGuy=Ember.Object.reopenClass({fixtureStore:{},fixtureLookup:{},modelIds:{},define:function(model,config){var info=this.getModelInfo(model);for(key in config){var value=config[key];info[key]=value;if(key!="default"){this.fixtureLookup[key]=model}}this.modelIds[model]=0},getModelInfo:function(model){if(!this.fixtureStore[model]){this.fixtureStore[model]={}}return this.fixtureStore[model]},lookupModelForName:function(name){var model=this.fixtureLookup[name];if(!model){if(this.fixtureStore[name]){model=name}}return model},generateId:function(model){var lastId=this.modelIds[model]||0;this.modelIds[model]=lastId+1;return this.modelIds[model]},build:function(name,opts){var model=this.lookupModelForName(name);if(!model){throw new Error("can't find that factory named ["+name+"]")}var modelInfo=this.fixtureStore[model];var modelAttributes=modelInfo[name]||{};var defaultModelAttributes=modelInfo.default;var fixture=$.extend({},defaultModelAttributes,modelAttributes,opts);if(!fixture.id){fixture.id=this.generateId(model)}return fixture},resetModels:function(store){var typeMaps=store.typeMaps;if(store.usingFixtureAdapter()){for(typeKey in this.fixtureStore){var modelType=store.modelFor(typeKey);modelType.FIXTURES=[];store.unloadAll(modelType)}}else{for(model in typeMaps){store.unloadAll(typeMaps[model].type)}}this.modelIds={}},pushFixture:function(modelClass,fixture){if(!modelClass["FIXTURES"]){modelClass["FIXTURES"]=[]}modelClass["FIXTURES"].push(fixture);return fixture}});DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(name,options){var modelName=FactoryGuy.lookupModelForName(name);var fixture=FactoryGuy.build(name,options);var modelType=this.modelFor(modelName);if(this.usingFixtureAdapter()){this.setBelongsToFixturesAssociation(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var self=this;var model;Em.run(function(){model=self.push(modelName,fixture);self.setBelongsToRestAssociation(modelType,modelName,model)});return model}},setBelongsToFixturesAssociation:function(modelType,modelName,parentFixture){var store=this;var adapter=this.adapterFor("application");var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(relationship){var hasManyModel=store.modelFor(Em.String.singularize(relationship));if(parentFixture[relationship]){parentFixture[relationship].forEach(function(id){var hasManyfixtures=adapter.fixturesForType(hasManyModel);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=parentFixture.id})}})}},setBelongsToRestAssociation:function(modelType,modelName,parent){var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(name){var children=parent.get(name);if(children.get("length")>0){children.forEach(function(child){child.set(modelName,parent)})}})}},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);return promise}});FactoryGuyHelperMixin=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},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)},handleCreate:function(name,opts){var model=FactoryGuy.lookupModelForName(name);this.stubEndpointForHttpRequest("/"+Em.String.pluralize(model),this.buildAjaxResponse(name,opts),{type:"POST"})},buildAjaxResponse:function(name,opts){var fixture=FactoryGuy.build(name,opts);var model=FactoryGuy.lookupModelForName(name);var hash={};hash[model]=fixture;return hash},handleUpdate:function(root,id){this.stubEndpointForHttpRequest("/"+Em.String.pluralize(root)+"/"+id,{},{type:"PUT"})},handleDelete:function(root,id){this.stubEndpointForHttpRequest("/"+Em.String.pluralize(root)+"/"+id,{},{type:"DELETE"})},teardown:function(){FactoryGuy.resetModels(this.getStore())}});
@@ -1,13 +1,13 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = "ember-data-factory-guy"
4
- s.version = "0.1"
4
+ s.version = "0.1.1"
5
5
  s.platform = Gem::Platform::RUBY
6
- s.authors = ["daniel sudol", "alex opak"]
7
- s.email = ["dansudol@yahoo.com"]
8
- s.homepage = "http://rubygems.org/gems/ember-data-factory-guy"
9
- s.summary = "Create Fixtures for Ember Data"
10
- s.description = "Create Fixtures for Ember Data"
6
+ s.authors = ["Daniel Sudol", "Alex Opak"]
7
+ s.email = ["dansudol@yahoo.com", "opak.alexandr@gmail.com"]
8
+ s.homepage = "https://github.com/danielspaniel/ember-data-factory-guy"
9
+ s.summary = "Easily create Fixtures for Ember Data"
10
+ s.description = "Easily create Fixtures for Ember Data"
11
11
  s.license = "MIT"
12
12
 
13
13
  s.required_rubygems_version = ">= 1.3.6"
data/src/factory_guy.js CHANGED
@@ -30,7 +30,7 @@ FactoryGuy = Ember.Object.reopenClass({
30
30
  FactoryGuy.build('user') or FactoryGuy.build('bob')
31
31
 
32
32
  @param model the model to define
33
- @param config your default and specific fixtures
33
+ @param config your default and named fixtures
34
34
  */
35
35
  define: function (model, config) {
36
36
  var info = this.getModelInfo(model);
@@ -100,7 +100,9 @@ FactoryGuy = Ember.Object.reopenClass({
100
100
  var modelAttributes = modelInfo[name] || {};
101
101
  var defaultModelAttributes = modelInfo.default;
102
102
  var fixture = $.extend({}, defaultModelAttributes, modelAttributes, opts);
103
- fixture.id = this.generateId(model);
103
+ if(!fixture.id){
104
+ fixture.id = this.generateId(model);
105
+ }
104
106
  return fixture;
105
107
  },
106
108
 
@@ -118,10 +120,7 @@ FactoryGuy = Ember.Object.reopenClass({
118
120
  }
119
121
  } else {
120
122
  for (model in typeMaps) {
121
- if (typeMaps[model].type.typeKey != 'user') {
122
- // console.log(typeMaps[model].type.typeKey)
123
- store.unloadAll(typeMaps[model].type);
124
- }
123
+ store.unloadAll(typeMaps[model].type);
125
124
  }
126
125
  }
127
126
  this.modelIds = {}
@@ -141,4 +140,4 @@ FactoryGuy = Ember.Object.reopenClass({
141
140
  modelClass['FIXTURES'].push(fixture);
142
141
  return fixture;
143
142
  }
144
- })
143
+ })
data/src/has_many.js CHANGED
@@ -61,7 +61,7 @@
61
61
 
62
62
  return Ember.computed('data', function(key) {
63
63
  var adapter = this.store.adapterFor('application');
64
- if (adapter.toString().match('Fixture')) {
64
+ if (adapter instanceof DS.FixtureAdapter) {
65
65
  var relationship = this._relationships[key],
66
66
  promiseLabel = "DS: Async hasMany " + this + " : " + key;
67
67
 
data/src/store.js CHANGED
@@ -122,10 +122,10 @@ DS.FixtureAdapter.reopen({
122
122
  createRecord: function(store, type, record) {
123
123
  var promise = this._super(store, type, record);
124
124
 
125
- promise.then( function() {
126
- var hasManyName = Ember.String.pluralize(type.typeKey);
127
- var relationShips = Ember.get(type, 'relationshipNames');
128
- if (relationShips.belongsTo) {
125
+ // promise.then( function() {
126
+ // var hasManyName = Ember.String.pluralize(type.typeKey);
127
+ // var relationShips = Ember.get(type, 'relationshipNames');
128
+ // if (relationShips.belongsTo) {
129
129
  // console.log('record',record+'', type.typeKey, hasManyName);
130
130
  // relationShips.belongsTo.forEach(function (relationship) {
131
131
  // console.log(relationship, record.get(relationship)+'')
@@ -133,8 +133,8 @@ DS.FixtureAdapter.reopen({
133
133
  // console.log(relationshipForType)
134
134
  // belongsToRecord.get(hasManyName).addObject(record);
135
135
  // })
136
- }
137
- })
136
+ // }
137
+ // })
138
138
  return promise;
139
139
  }
140
140
 
@@ -118,17 +118,17 @@ asyncTest("#makeFixture sets hasMany associations on fixtures", function() {
118
118
  })
119
119
  })
120
120
 
121
- asyncTest("#createRecord adds belongsTo associations to hasMany array", function() {
122
- var user = store.makeFixture('user');
123
-
124
- store.find('user', user.id).then(function(user){
125
-
126
- var projectJson = {title:'project', user: user};
127
-
128
- store.createRecord('project', projectJson).save()
129
- .then( function() {
130
- equal(user.get('projects.length'), 1);
131
- start();
132
- });
133
- })
134
- })
121
+ //asyncTest("#createRecord adds belongsTo associations to hasMany array", function() {
122
+ // var user = store.makeFixture('user');
123
+ //
124
+ // store.find('user', user.id).then(function(user){
125
+ //
126
+ // var projectJson = {title:'project', user: user};
127
+ //
128
+ // store.createRecord('project', projectJson).save()
129
+ // .then( function() {
130
+ // equal(user.get('projects.length'), 1);
131
+ // start();
132
+ // });
133
+ // })
134
+ //})
@@ -30,7 +30,7 @@ FactoryGuy = Ember.Object.reopenClass({
30
30
  FactoryGuy.build('user') or FactoryGuy.build('bob')
31
31
 
32
32
  @param model the model to define
33
- @param config your default and specific fixtures
33
+ @param config your default and named fixtures
34
34
  */
35
35
  define: function (model, config) {
36
36
  var info = this.getModelInfo(model);
@@ -100,7 +100,9 @@ FactoryGuy = Ember.Object.reopenClass({
100
100
  var modelAttributes = modelInfo[name] || {};
101
101
  var defaultModelAttributes = modelInfo.default;
102
102
  var fixture = $.extend({}, defaultModelAttributes, modelAttributes, opts);
103
- fixture.id = this.generateId(model);
103
+ if(!fixture.id){
104
+ fixture.id = this.generateId(model);
105
+ }
104
106
  return fixture;
105
107
  },
106
108
 
@@ -118,10 +120,7 @@ FactoryGuy = Ember.Object.reopenClass({
118
120
  }
119
121
  } else {
120
122
  for (model in typeMaps) {
121
- if (typeMaps[model].type.typeKey != 'user') {
122
- // console.log(typeMaps[model].type.typeKey)
123
- store.unloadAll(typeMaps[model].type);
124
- }
123
+ store.unloadAll(typeMaps[model].type);
125
124
  }
126
125
  }
127
126
  this.modelIds = {}
@@ -142,6 +141,7 @@ FactoryGuy = Ember.Object.reopenClass({
142
141
  return fixture;
143
142
  }
144
143
  })
144
+
145
145
  DS.Store.reopen({
146
146
 
147
147
  usingFixtureAdapter: function() {
@@ -266,10 +266,10 @@ DS.FixtureAdapter.reopen({
266
266
  createRecord: function(store, type, record) {
267
267
  var promise = this._super(store, type, record);
268
268
 
269
- promise.then( function() {
270
- var hasManyName = Ember.String.pluralize(type.typeKey);
271
- var relationShips = Ember.get(type, 'relationshipNames');
272
- if (relationShips.belongsTo) {
269
+ // promise.then( function() {
270
+ // var hasManyName = Ember.String.pluralize(type.typeKey);
271
+ // var relationShips = Ember.get(type, 'relationshipNames');
272
+ // if (relationShips.belongsTo) {
273
273
  // console.log('record',record+'', type.typeKey, hasManyName);
274
274
  // relationShips.belongsTo.forEach(function (relationship) {
275
275
  // console.log(relationship, record.get(relationship)+'')
@@ -277,8 +277,8 @@ DS.FixtureAdapter.reopen({
277
277
  // console.log(relationshipForType)
278
278
  // belongsToRecord.get(hasManyName).addObject(record);
279
279
  // })
280
- }
281
- })
280
+ // }
281
+ // })
282
282
  return promise;
283
283
  }
284
284
 
@@ -359,7 +359,7 @@ FactoryGuyHelperMixin = Em.Mixin.create({
359
359
 
360
360
  handleUpdate: function (root, id) {
361
361
  this.stubEndpointForHttpRequest(
362
- "/" + Em.String.pluralize(root) + "/" + id, '{}', {type: 'PUT'}
362
+ "/" + Em.String.pluralize(root) + "/" + id, {}, {type: 'PUT'}
363
363
  )
364
364
  },
365
365
 
@@ -61,7 +61,7 @@
61
61
 
62
62
  return Ember.computed('data', function(key) {
63
63
  var adapter = this.store.adapterFor('application');
64
- if (adapter.toString().match('Fixture')) {
64
+ if (adapter instanceof DS.FixtureAdapter) {
65
65
  var relationship = this._relationships[key],
66
66
  promiseLabel = "DS: Async hasMany " + this + " : " + key;
67
67
 
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ember-data-factory-guy
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - daniel sudol
9
- - alex opak
8
+ - Daniel Sudol
9
+ - Alex Opak
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-04-23 00:00:00.000000000 +03:00
13
+ date: 2014-04-24 00:00:00.000000000 +03:00
14
14
  default_executable:
15
15
  dependencies: []
16
- description: Create Fixtures for Ember Data
16
+ description: Easily create Fixtures for Ember Data
17
17
  email:
18
18
  - dansudol@yahoo.com
19
+ - opak.alexandr@gmail.com
19
20
  executables: []
20
21
  extensions: []
21
22
  extra_rdoc_files: []
@@ -48,7 +49,7 @@ files:
48
49
  - vendor/assets/javascripts/ember_data_factory_guy.js
49
50
  - vendor/assets/javascripts/factory_guy_has_many.js
50
51
  has_rdoc: true
51
- homepage: http://rubygems.org/gems/ember-data-factory-guy
52
+ homepage: https://github.com/danielspaniel/ember-data-factory-guy
52
53
  licenses:
53
54
  - MIT
54
55
  post_install_message:
@@ -72,5 +73,5 @@ rubyforge_project: ember-data-factory-guy
72
73
  rubygems_version: 1.6.2
73
74
  signing_key:
74
75
  specification_version: 3
75
- summary: Create Fixtures for Ember Data
76
+ summary: Easily create Fixtures for Ember Data
76
77
  test_files: []