ember-data-factory-guy 0.8.7 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  User = DS.Model.extend({
2
2
  name: DS.attr('string'),
3
+ info: DS.attr('object'),
3
4
  company: DS.belongsTo('company', {async: true, inverse: 'users', polymorphic: true}),
4
5
  properties: DS.hasMany('property', {async: true, inverse: 'owners'}),
5
6
  projects: DS.hasMany('project'),
@@ -1,3 +1,13 @@
1
+ ObjectTransform = DS.Transform.extend({
2
+ serialize: function(obj) {
3
+ return JSON.parse(obj);
4
+ },
5
+ deserialize: function(obj) {
6
+ return JSON.stringify(obj);
7
+ }
8
+ });
9
+
10
+
1
11
  TestHelper = Ember.Object.createWithMixins(FactoryGuy.testMixin,{
2
12
 
3
13
  /**
@@ -34,7 +44,10 @@ TestHelper = Ember.Object.createWithMixins(FactoryGuy.testMixin,{
34
44
  if (adapter instanceof DS.FixtureAdapter) {
35
45
  adapter.simulateRemoteResponse = false;
36
46
  }
47
+ // bypassing the parent TestHelper setup because there is no application
48
+ // here in testing, just a container
37
49
  this.set('container', env.container);
50
+ FactoryGuy.setStore(this.getStore());
38
51
  return this;
39
52
  }
40
53
  });
@@ -69,6 +82,7 @@ TestHelper = Ember.Object.createWithMixins(FactoryGuy.testMixin,{
69
82
  container.register('serializer:-default', serializer);
70
83
  container.register('transform:string', DS.StringTransform);
71
84
  container.register('transform:date', DS.DateTransform);
85
+ container.register('transform:object', ObjectTransform);
72
86
  container.injection('serializer', 'store', 'store:main');
73
87
 
74
88
  env.store = container.lookup('store:main');
data/tests/test_setup.js CHANGED
@@ -1,3 +1,107 @@
1
+ Company = DS.Model.extend({
2
+ type: DS.attr('string', {defaultValue: 'Company'}),
3
+ name: DS.attr('string'),
4
+ profile: DS.belongsTo('profile'),
5
+ users: DS.hasMany('user', {async: true, inverse: 'company'}),
6
+ projects: DS.hasMany('project', {async: true})
7
+ });
8
+
9
+ SmallCompany = Company.extend({
10
+ type: DS.attr('string', {defaultValue: 'SmallCompany'}),
11
+ owner: DS.belongsTo('user', {async: true}),
12
+ projects: DS.hasMany('project')
13
+ });
14
+
15
+ Group = DS.Model.extend({
16
+ type: DS.attr('string', {defaultValue: 'Group'}),
17
+ name: DS.attr('string'),
18
+ versions: DS.hasMany('group'),
19
+ profiles: DS.hasMany('profile')
20
+ })
21
+
22
+ BigGroup = Group.extend({
23
+ type: DS.attr('string', {defaultValue: 'BigGroup'}),
24
+ group: DS.belongsTo('group')
25
+ })
26
+
27
+ SmallGroup = Group.extend({
28
+ type: DS.attr('string', {defaultValue: 'SmallGroup'}),
29
+ group: DS.belongsTo('group')
30
+ })
31
+
32
+
33
+ Hat = DS.Model.extend({
34
+ type: DS.attr('string'),
35
+ user: DS.belongsTo('user'),
36
+ outfit: DS.belongsTo('outfit'),
37
+ hat: DS.belongsTo('hat', {inverse: 'hats', polymorphic: true}),
38
+ hats: DS.hasMany('hat', {inverse: 'hat', polymorphic: true}),
39
+ fluffy_materials: DS.hasMany('fluffy_materials')
40
+ });
41
+
42
+ BigHat = Hat.extend({
43
+ materials: DS.hasMany('soft_material')
44
+ });
45
+ SmallHat = Hat.extend();
46
+
47
+
48
+
49
+ SoftMaterial = DS.Model.extend({
50
+ name: DS.attr('string'),
51
+ hat: DS.belongsTo('big_hat')
52
+ })
53
+
54
+ FluffyMaterial = DS.Model.extend({
55
+ name: DS.attr('string'),
56
+ hat: DS.belongsTo('hat', {polymorphic: true})
57
+ })
58
+
59
+ Outfit = DS.Model.extend({
60
+ name: DS.attr('string'),
61
+ hats: DS.hasMany('hat', {polymorphic: true})
62
+ })
63
+
64
+ Person = DS.Model.extend({
65
+ type: DS.attr('string'),
66
+ name: DS.attr('string')
67
+ })
68
+
69
+ Profile = DS.Model.extend({
70
+ created_at: DS.attr('date'),
71
+ description: DS.attr('string'),
72
+ camelCaseDescription: DS.attr('string'),
73
+ snake_case_description: DS.attr('string'),
74
+ company: DS.belongsTo('company'),
75
+ group: DS.belongsTo('group', {polymorphic: true})
76
+ });
77
+
78
+ Project = DS.Model.extend({
79
+ title: DS.attr('string'),
80
+ user: DS.belongsTo('user'),
81
+ parent: DS.belongsTo('project', {inverse: 'children'}),
82
+ children: DS.hasMany('project', {inverse: 'parent'})
83
+ });
84
+
85
+
86
+
87
+
88
+
89
+
90
+ Property = DS.Model.extend({
91
+ name: DS.attr('string'),
92
+ company: DS.belongsTo('company', {async: true}),
93
+ owners: DS.hasMany('user', {async: true, inverse: 'properties'})
94
+ });
95
+ User = DS.Model.extend({
96
+ name: DS.attr('string'),
97
+ info: DS.attr('object'),
98
+ company: DS.belongsTo('company', {async: true, inverse: 'users', polymorphic: true}),
99
+ properties: DS.hasMany('property', {async: true, inverse: 'owners'}),
100
+ projects: DS.hasMany('project'),
101
+ hats: DS.hasMany('hat', {polymorphic: true})
102
+ });
103
+
104
+
1
105
  FactoryGuy.define("company", {
2
106
  default: {
3
107
  name: 'Silly corp'
@@ -171,106 +275,3 @@ FactoryGuy.define('user', {
171
275
  }
172
276
  });
173
277
 
174
-
175
- Company = DS.Model.extend({
176
- type: DS.attr('string', {defaultValue: 'Company'}),
177
- name: DS.attr('string'),
178
- profile: DS.belongsTo('profile'),
179
- users: DS.hasMany('user', {async: true, inverse: 'company'}),
180
- projects: DS.hasMany('project', {async: true})
181
- });
182
-
183
- SmallCompany = Company.extend({
184
- type: DS.attr('string', {defaultValue: 'SmallCompany'}),
185
- owner: DS.belongsTo('user', {async: true}),
186
- projects: DS.hasMany('project')
187
- });
188
-
189
- Group = DS.Model.extend({
190
- type: DS.attr('string', {defaultValue: 'Group'}),
191
- name: DS.attr('string'),
192
- versions: DS.hasMany('group'),
193
- profiles: DS.hasMany('profile')
194
- })
195
-
196
- BigGroup = Group.extend({
197
- type: DS.attr('string', {defaultValue: 'BigGroup'}),
198
- group: DS.belongsTo('group')
199
- })
200
-
201
- SmallGroup = Group.extend({
202
- type: DS.attr('string', {defaultValue: 'SmallGroup'}),
203
- group: DS.belongsTo('group')
204
- })
205
-
206
-
207
- Hat = DS.Model.extend({
208
- type: DS.attr('string'),
209
- user: DS.belongsTo('user'),
210
- outfit: DS.belongsTo('outfit'),
211
- hat: DS.belongsTo('hat', {inverse: 'hats', polymorphic: true}),
212
- hats: DS.hasMany('hat', {inverse: 'hat', polymorphic: true}),
213
- fluffy_materials: DS.hasMany('fluffy_materials')
214
- });
215
-
216
- BigHat = Hat.extend({
217
- materials: DS.hasMany('soft_material')
218
- });
219
- SmallHat = Hat.extend();
220
-
221
-
222
-
223
- SoftMaterial = DS.Model.extend({
224
- name: DS.attr('string'),
225
- hat: DS.belongsTo('big_hat')
226
- })
227
-
228
- FluffyMaterial = DS.Model.extend({
229
- name: DS.attr('string'),
230
- hat: DS.belongsTo('hat', {polymorphic: true})
231
- })
232
-
233
- Outfit = DS.Model.extend({
234
- name: DS.attr('string'),
235
- hats: DS.hasMany('hat', {polymorphic: true})
236
- })
237
-
238
- Person = DS.Model.extend({
239
- type: DS.attr('string'),
240
- name: DS.attr('string')
241
- })
242
-
243
- Profile = DS.Model.extend({
244
- created_at: DS.attr('date'),
245
- description: DS.attr('string'),
246
- camelCaseDescription: DS.attr('string'),
247
- snake_case_description: DS.attr('string'),
248
- company: DS.belongsTo('company'),
249
- group: DS.belongsTo('group', {polymorphic: true})
250
- });
251
-
252
- Project = DS.Model.extend({
253
- title: DS.attr('string'),
254
- user: DS.belongsTo('user'),
255
- parent: DS.belongsTo('project', {inverse: 'children'}),
256
- children: DS.hasMany('project', {inverse: 'parent'})
257
- });
258
-
259
-
260
-
261
-
262
-
263
-
264
- Property = DS.Model.extend({
265
- name: DS.attr('string'),
266
- company: DS.belongsTo('company', {async: true}),
267
- owners: DS.hasMany('user', {async: true, inverse: 'properties'})
268
- });
269
- User = DS.Model.extend({
270
- name: DS.attr('string'),
271
- company: DS.belongsTo('company', {async: true, inverse: 'users', polymorphic: true}),
272
- properties: DS.hasMany('property', {async: true, inverse: 'owners'}),
273
- projects: DS.hasMany('project'),
274
- hats: DS.hasMany('hat', {polymorphic: true})
275
- });
276
-
@@ -90,9 +90,19 @@ var ModelDefinition = function (model, config) {
90
90
  // function might be a sequence of a named association
91
91
  fixture[attribute] = fixture[attribute].call(this, fixture);
92
92
  } else if (Ember.typeOf(fixture[attribute]) == 'object') {
93
- // if it's an object it's for a model association, so build the json
93
+ // If it's an object and it's a model association attribute, build the json
94
94
  // for the association and replace the attribute with that json
95
- fixture[attribute] = FactoryGuy.build(attribute, fixture[attribute]);
95
+ if (FactoryGuy.getStore()) {
96
+ if (FactoryGuy.isAttributeRelationship(this.model, attribute)) {
97
+ fixture[attribute] = FactoryGuy.build(attribute, fixture[attribute]);
98
+ }
99
+ } else {
100
+ // For legacy reasons, if the store is not set in FactoryGuy, keep
101
+ // this code the way it is ( though it will cause failures when the object is actually
102
+ // a custom attribute and not a relationship ), while users start setting the store
103
+ // in FactoryGuy, or using testHelper.make instead of store.makeFixture
104
+ fixture[attribute] = FactoryGuy.build(attribute, fixture[attribute]);
105
+ }
96
106
  }
97
107
  }
98
108
  // set the id, unless it was already set in opts
@@ -213,6 +223,44 @@ var FactoryGuy = {
213
223
  this.modelDefinitions[model] = new ModelDefinition(model, config);
214
224
  }
215
225
  },
226
+ /**
227
+ Setting the store so FactoryGuy can do some model introspection.
228
+ */
229
+ setStore: function(store) {
230
+ Ember.assert("FactoryGuy#setStore needs a valid store instance.You passed in ["+store+"]",store instanceof DS.Store)
231
+ this.store = store;
232
+ },
233
+ getStore: function() {
234
+ return this.store;
235
+ },
236
+ /**
237
+ Checks a model's attribute to determine if it's a relationship.
238
+
239
+ @param {String} typeName model type name like 'user' for User model class
240
+ @param {String} attribute attribute you want to check
241
+ @returns {Boolean} true if the attribute is a relationship, false if not
242
+ */
243
+ isAttributeRelationship: function(typeName, attribute) {
244
+ if (!this.store) {
245
+ console.log("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures")
246
+ // The legacy value was true.
247
+ return true;
248
+ }
249
+ var model = this.store.modelFor(typeName);
250
+ return !!model.typeForRelationship(attribute);
251
+ },
252
+ /**
253
+ Make new fixture and save to store. Proxy to store#makeFixture method
254
+
255
+ @param {String} name fixture name
256
+ @param {String} trait optional trait names ( one or more )
257
+ @param {Object} opts optional fixture options that will override default fixture values
258
+ @returns {Object|DS.Model} json or record depending on the adapter type
259
+ */
260
+ make: function() {
261
+ Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures", this.store);
262
+ return this.store.makeFixture.apply(this.store,arguments);
263
+ },
216
264
  /**
217
265
  Used in model definitions to declare use of a sequence. For example:
218
266
 
@@ -736,6 +784,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
736
784
  // Pass in the app root, which typically is App.
737
785
  setup: function (app) {
738
786
  this.set('container', app.__container__);
787
+ FactoryGuy.setStore(this.getStore());
739
788
  return this;
740
789
  },
741
790
  useFixtureAdapter: function (app) {
@@ -763,8 +812,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
763
812
  return this.getStore().find(type, id);
764
813
  },
765
814
  /**
766
- Proxy to store's makeFixture method
767
-
815
+ Make new fixture and save to store. Proxy to store#makeFixture method
768
816
  */
769
817
  make: function () {
770
818
  var store = this.getStore();
@@ -773,12 +821,6 @@ var FactoryGuyTestMixin = Em.Mixin.create({
773
821
  getStore: function () {
774
822
  return this.get('container').lookup('store:main');
775
823
  },
776
- pushPayload: function (type, hash) {
777
- return this.getStore().pushPayload(type, hash);
778
- },
779
- pushRecord: function (type, hash) {
780
- return this.getStore().push(type, hash);
781
- },
782
824
  /**
783
825
  Using mockjax to stub an http request.
784
826
 
@@ -855,7 +897,10 @@ var FactoryGuyTestMixin = Em.Mixin.create({
855
897
  so you don't need to include them in the returns hash as well.
856
898
 
857
899
  2) If you don't use match options for exact match, there will be no id returned to the model.
858
-
900
+ The reason being, that this method purposely returns an empty hash response with a 'don't match'
901
+ style handleCreate, because if the responseJson returns a non empty data hash ( with even only
902
+ the id), this essentially empty hash of attributes will override ( and nullify ) all the attributes
903
+ that set when you created the record.
859
904
  3) If you match on a belongsTo association, you don't have to include that in the
860
905
  returns hash.
861
906
 
@@ -908,8 +953,25 @@ var FactoryGuyTestMixin = Em.Mixin.create({
908
953
  @param {Boolean} succeed optional flag to indicate if the request
909
954
  should succeed ( default is true )
910
955
  */
911
- handleUpdate: function (type, id, succeed) {
912
- succeed = succeed === undefined ? true : succeed;
956
+ handleUpdate: function () {
957
+ var args = Array.prototype.slice.call(arguments)
958
+ Ember.assert("To handleUpdate pass in a model instance or a type and an id", args.length>0)
959
+ var succeed = true;
960
+ if (typeof args[args.length-1] == 'boolean') {
961
+ args.pop()
962
+ succeed = false;
963
+ }
964
+ Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0)
965
+ var type, id;
966
+ if (args[0] instanceof DS.Model) {
967
+ var model = args[0];
968
+ type = model.constructor.typeKey;
969
+ id = model.id;
970
+ } else if (typeof args[0] == "string" && typeof parseInt(args[1]) == "number") {
971
+ type = args[0];
972
+ id = args[1];
973
+ }
974
+ Ember.assert("To handleUpdate pass in a model instance or a type and an id",type && id)
913
975
  this.stubEndpointForHttpRequest(this.buildURL(type, id), {}, {
914
976
  type: 'PUT',
915
977
  status: succeed ? 200 : 500
@@ -1056,12 +1118,10 @@ if (FactoryGuy !== undefined) {
1056
1118
  return null;
1057
1119
  }
1058
1120
  }
1121
+
1059
1122
  // Inspect the data submitted in the request (either POST body or GET query string)
1060
1123
  if ( handler.data ) {
1061
- // console.log('request.data', requestSettings.data )
1062
- // console.log('handler.data', handler.data )
1063
- // console.log('data equal', isMockDataEqual(handler.data, requestSettings.data) )
1064
- if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) {
1124
+ if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) {
1065
1125
  // They're not identical, do not mock this request
1066
1126
  return null;
1067
1127
  }
@@ -1072,6 +1132,7 @@ if (FactoryGuy !== undefined) {
1072
1132
  // The request type doesn't match (GET vs. POST)
1073
1133
  return null;
1074
1134
  }
1135
+
1075
1136
  return handler;
1076
1137
  }
1077
1138
 
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.7
4
+ version: 0.9.0
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-10 00:00:00.000000000 Z
12
+ date: 2014-12-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Easily create Fixtures for Ember Data
15
15
  email:
@@ -19,8 +19,8 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
- - ".gitignore"
23
- - ".travis.yml"
22
+ - .gitignore
23
+ - .travis.yml
24
24
  - Gruntfile.js
25
25
  - LICENSE
26
26
  - README.md
@@ -80,17 +80,17 @@ require_paths:
80
80
  - lib
81
81
  required_ruby_version: !ruby/object:Gem::Requirement
82
82
  requirements:
83
- - - ">="
83
+ - - '>='
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ">="
88
+ - - '>='
89
89
  - !ruby/object:Gem::Version
90
90
  version: 1.3.6
91
91
  requirements: []
92
92
  rubyforge_project: ember-data-factory-guy
93
- rubygems_version: 2.2.2
93
+ rubygems_version: 2.0.14
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Easily create Fixtures for Ember Data