ember-data-factory-guy 0.7.8 → 0.7.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -30
- data/bower.json +6 -7
- data/dist/ember-data-factory-guy.js +118 -201
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/dist/factory_guy_has_many.js +33 -54
- data/ember-data-factory-guy.gemspec +1 -1
- data/package.json +1 -1
- data/src/factory_guy.js +39 -46
- data/src/factory_guy_test_mixin.js +25 -61
- data/src/has_many.js +33 -54
- data/src/model_definition.js +27 -39
- data/src/sequence.js +6 -7
- data/src/store.js +22 -47
- data/tests/factory_guy_test_mixin_test.js +35 -20
- data/tests/support/models/company.js +2 -0
- data/tests/support/models/group.js +5 -1
- data/tests/support/models/profile.js +2 -1
- data/tests/test_setup.js +9 -2
- data/vendor/assets/javascripts/ember_data_factory_guy.js +118 -201
- data/vendor/assets/javascripts/factory_guy_has_many.js +33 -54
- 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: b03aa99469ce10e4dd64863b8d36b5daba14be94
|
4
|
+
data.tar.gz: 7e9e167093d1792f6a146a51e35affc3667ded9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d3b281787780405eb930f2ad26405e7d05fbcced53622efb915a8f6a109dd40d713eb8d7586ecb1bdc947daeb30df95874cf53d05beb58765456db941a612f4
|
7
|
+
data.tar.gz: 3f239d6ac31b065f73be62f22a264288250dfe66efd942195183551876b7951072f4dc6ec1eeb8ce883332a871220e9d5748a65b62192432d51f59344a11cc11
|
data/README.md
CHANGED
@@ -606,47 +606,43 @@ and this is already bundled for you when you use the ember-data-factory-guy libr
|
|
606
606
|
```
|
607
607
|
|
608
608
|
##### handleCreate
|
609
|
-
- naive handling of createRecord
|
610
|
-
- real life handling createRecord
|
611
|
-
|
612
|
-
###### Naive handling of createRecord
|
613
|
-
*success case is the default*
|
614
609
|
|
610
|
+
*success case is the default*
|
611
|
+
|
612
|
+
*Realistically, you will have code in a view action or controller action that will
|
613
|
+
create the record, and setup any associations.
|
614
|
+
Therefore, all you need to pass in to handleCreate are the attributes on the model
|
615
|
+
that are being tested ( excluding associations .. you don't need those)*
|
616
|
+
|
615
617
|
```javascript
|
616
|
-
// set up the profile you want to create here
|
617
|
-
testHelper.handleCreate('profile', 'with_company', {description: "Moo"})
|
618
618
|
|
619
|
-
//
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
619
|
+
// most actions that create a record look something like this:
|
620
|
+
action: {
|
621
|
+
addProject: function (user) {
|
622
|
+
var name = this.$('.add-project input').val();
|
623
|
+
var store = this.get('controller.store');
|
624
|
+
store.createRecord('project', {name: name, user: user}).save();
|
625
|
+
}
|
626
|
+
}
|
624
627
|
|
625
628
|
```
|
626
629
|
|
627
|
-
|
628
|
-
|
629
|
-
```javascript
|
630
|
-
// set the succeed flag to 'false'
|
631
|
-
testHelper.handleCreate('profile', false);
|
630
|
+
In this case, you are are creating a 'project' record with a specific name, and belonging
|
631
|
+
to a particular user. To mock this createRecord call do this:
|
632
632
|
|
633
|
-
|
633
|
+
```javascript
|
634
|
+
// note that you don't include the user association
|
635
|
+
testHelper.handleCreate('project', {name: "Moo"})
|
634
636
|
|
635
637
|
```
|
636
638
|
|
639
|
+
*mocking a failed create*
|
637
640
|
|
638
|
-
###### Real Life handling of createRecord
|
639
|
-
|
640
|
-
**Usually
|
641
641
|
```javascript
|
642
|
-
// set
|
643
|
-
testHelper.handleCreate('profile',
|
642
|
+
// set the succeed flag to 'false'
|
643
|
+
testHelper.handleCreate('profile', null, false);
|
644
644
|
|
645
|
-
|
646
|
-
// is tailoring the profile for you
|
647
|
-
store.createRecord('profile').save().then(function(profile) {
|
648
|
-
profile.toJSON() //=> {id:1, description: "Moo", company: "1"}
|
649
|
-
});
|
645
|
+
store.createRecord('profile').save() //=> fails
|
650
646
|
|
651
647
|
```
|
652
648
|
|
@@ -733,8 +729,10 @@ test("Creates new project", function() {
|
|
733
729
|
|
734
730
|
click('.add-div div:contains(New Project)')
|
735
731
|
fillIn('.add-project input', newProjectName)
|
736
|
-
|
737
|
-
//
|
732
|
+
|
733
|
+
// Remember, you only need to pass in attributes of the model you expect
|
734
|
+
// to create in your application. You do not need to include belongsTo records,
|
735
|
+
// as in this case the 'user' belongsTo association
|
738
736
|
viewHelper.handleCreate('project', {name: newProjectName})
|
739
737
|
|
740
738
|
/**
|
data/bower.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ember-data-factory-guy",
|
3
|
-
"version": "0.7.
|
3
|
+
"version": "0.7.9",
|
4
4
|
"authors": [
|
5
5
|
"Daniel Sudol <dansudol@yahoo.com>",
|
6
6
|
"Opak Alex <opak.alexandr@gmail.com>"
|
@@ -15,12 +15,7 @@
|
|
15
15
|
],
|
16
16
|
"license": "MIT",
|
17
17
|
"dependencies": {
|
18
|
-
"handlebars": "~1.1.2",
|
19
|
-
"jquery": "2.0.3",
|
20
|
-
"ember": "latest",
|
21
18
|
"ember-data": "1.0.0-beta.11",
|
22
|
-
"chance": "latest",
|
23
|
-
"qunit": "latest",
|
24
19
|
"jquery-mockjax": "latest"
|
25
20
|
},
|
26
21
|
"ignore": [
|
@@ -36,7 +31,11 @@
|
|
36
31
|
"tests"
|
37
32
|
],
|
38
33
|
"devDependencies": {
|
34
|
+
"chance": "latest",
|
35
|
+
"qunit": "latest",
|
39
36
|
"sinon": "http://sinonjs.org/releases/sinon-1.10.1.js",
|
40
|
-
"sinon-qunit": "~2.0.0"
|
37
|
+
"sinon-qunit": "~2.0.0",
|
38
|
+
"jquery": "2.0.3",
|
39
|
+
"ember": "latest"
|
41
40
|
}
|
42
41
|
}
|
@@ -1,19 +1,17 @@
|
|
1
1
|
Sequence = function (fn) {
|
2
2
|
var index = 1;
|
3
|
-
|
4
3
|
this.next = function () {
|
5
4
|
return fn.call(this, index++);
|
6
|
-
}
|
7
|
-
|
5
|
+
};
|
8
6
|
this.reset = function () {
|
9
7
|
index = 1;
|
10
|
-
}
|
8
|
+
};
|
11
9
|
};
|
12
|
-
|
13
10
|
function MissingSequenceError(message) {
|
14
|
-
this.toString = function() {
|
15
|
-
|
16
|
-
|
11
|
+
this.toString = function () {
|
12
|
+
return message;
|
13
|
+
};
|
14
|
+
}
|
17
15
|
/**
|
18
16
|
A ModelDefinition encapsulates a model's definition
|
19
17
|
|
@@ -27,8 +25,8 @@ ModelDefinition = function (model, config) {
|
|
27
25
|
var defaultAttributes = {};
|
28
26
|
var namedModels = {};
|
29
27
|
var modelId = 1;
|
28
|
+
var sequenceName = null;
|
30
29
|
this.model = model;
|
31
|
-
|
32
30
|
/**
|
33
31
|
@param {String} name model name like 'user' or named type like 'admin'
|
34
32
|
@returns {Boolean} true if name is this definitions model or this definition
|
@@ -36,12 +34,11 @@ ModelDefinition = function (model, config) {
|
|
36
34
|
*/
|
37
35
|
this.matchesName = function (name) {
|
38
36
|
return model == name || namedModels[name];
|
39
|
-
}
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
}
|
44
|
-
|
37
|
+
};
|
38
|
+
// Increment id
|
39
|
+
this.nextId = function () {
|
40
|
+
return modelId++;
|
41
|
+
};
|
45
42
|
/**
|
46
43
|
Call the next method on the named sequence function. If the name
|
47
44
|
is a function, create the sequence with that function
|
@@ -60,11 +57,10 @@ ModelDefinition = function (model, config) {
|
|
60
57
|
}
|
61
58
|
var sequence = sequences[name];
|
62
59
|
if (!sequence) {
|
63
|
-
throw new MissingSequenceError(
|
60
|
+
throw new MissingSequenceError('Can not find that sequence named [' + sequenceName + '] in \'' + model + '\' definition');
|
64
61
|
}
|
65
62
|
return sequence.next();
|
66
|
-
}
|
67
|
-
|
63
|
+
};
|
68
64
|
/**
|
69
65
|
Build a fixture by name
|
70
66
|
|
@@ -74,31 +70,30 @@ ModelDefinition = function (model, config) {
|
|
74
70
|
@returns {Object} json
|
75
71
|
*/
|
76
72
|
this.build = function (name, opts, traitArgs) {
|
77
|
-
var traitsObj = {}
|
78
|
-
traitArgs.forEach(function(trait) {
|
73
|
+
var traitsObj = {};
|
74
|
+
traitArgs.forEach(function (trait) {
|
79
75
|
$.extend(traitsObj, traits[trait]);
|
80
|
-
})
|
76
|
+
});
|
81
77
|
var modelAttributes = namedModels[name] || {};
|
82
78
|
// merge default, modelAttributes, traits and opts to get the rough fixture
|
83
79
|
var fixture = $.extend({}, defaultAttributes, modelAttributes, traitsObj, opts);
|
84
80
|
// deal with attributes that are functions or objects
|
85
|
-
for (attribute in fixture) {
|
81
|
+
for (var attribute in fixture) {
|
86
82
|
if (Ember.typeOf(fixture[attribute]) == 'function') {
|
87
83
|
// function might be a sequence of a named association
|
88
84
|
fixture[attribute] = fixture[attribute].call(this, fixture);
|
89
85
|
} else if (Ember.typeOf(fixture[attribute]) == 'object') {
|
90
86
|
// if it's an object it's for a model association, so build the json
|
91
87
|
// for the association and replace the attribute with that json
|
92
|
-
fixture[attribute] = FactoryGuy.build(attribute, fixture[attribute])
|
88
|
+
fixture[attribute] = FactoryGuy.build(attribute, fixture[attribute]);
|
93
89
|
}
|
94
90
|
}
|
95
91
|
// set the id, unless it was already set in opts
|
96
92
|
if (!fixture.id) {
|
97
|
-
fixture.id =
|
93
|
+
fixture.id = this.nextId();
|
98
94
|
}
|
99
95
|
return fixture;
|
100
|
-
}
|
101
|
-
|
96
|
+
};
|
102
97
|
/**
|
103
98
|
Build a list of fixtures
|
104
99
|
|
@@ -114,63 +109,53 @@ ModelDefinition = function (model, config) {
|
|
114
109
|
arr.push(this.build(name, opts, traits));
|
115
110
|
}
|
116
111
|
return arr;
|
117
|
-
}
|
118
|
-
|
112
|
+
};
|
119
113
|
// Set the modelId back to 1, and reset the sequences
|
120
114
|
this.reset = function () {
|
121
115
|
modelId = 1;
|
122
|
-
for (name in sequences) {
|
116
|
+
for (var name in sequences) {
|
123
117
|
sequences[name].reset();
|
124
118
|
}
|
125
|
-
}
|
126
|
-
|
119
|
+
};
|
127
120
|
var parseDefault = function (object) {
|
128
121
|
if (!object) {
|
129
|
-
return
|
122
|
+
return;
|
130
123
|
}
|
131
124
|
defaultAttributes = object;
|
132
|
-
}
|
133
|
-
|
125
|
+
};
|
134
126
|
var parseTraits = function (object) {
|
135
127
|
if (!object) {
|
136
|
-
return
|
128
|
+
return;
|
137
129
|
}
|
138
130
|
traits = object;
|
139
|
-
}
|
140
|
-
|
131
|
+
};
|
141
132
|
var parseSequences = function (object) {
|
142
133
|
if (!object) {
|
143
|
-
return
|
134
|
+
return;
|
144
135
|
}
|
145
136
|
for (sequenceName in object) {
|
146
137
|
var sequenceFn = object[sequenceName];
|
147
138
|
if (Ember.typeOf(sequenceFn) != 'function') {
|
148
|
-
throw new Error('Problem with [' + sequenceName + '] sequence definition. Sequences must be functions')
|
139
|
+
throw new Error('Problem with [' + sequenceName + '] sequence definition. Sequences must be functions');
|
149
140
|
}
|
150
141
|
object[sequenceName] = new Sequence(sequenceFn);
|
151
142
|
}
|
152
143
|
sequences = object;
|
153
|
-
}
|
154
|
-
|
144
|
+
};
|
155
145
|
var parseConfig = function (config) {
|
156
146
|
parseSequences(config.sequences);
|
157
147
|
delete config.sequences;
|
158
|
-
|
159
148
|
parseTraits(config.traits);
|
160
149
|
delete config.traits;
|
161
|
-
|
162
150
|
parseDefault(config.default);
|
163
151
|
delete config.default;
|
164
|
-
|
165
152
|
namedModels = config;
|
166
|
-
}
|
167
|
-
|
153
|
+
};
|
168
154
|
// initialize
|
169
155
|
parseConfig(config);
|
170
|
-
}
|
156
|
+
};
|
171
157
|
FactoryGuy = {
|
172
158
|
modelDefinitions: {},
|
173
|
-
|
174
159
|
/**
|
175
160
|
```javascript
|
176
161
|
|
@@ -216,7 +201,6 @@ FactoryGuy = {
|
|
216
201
|
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
217
202
|
}
|
218
203
|
},
|
219
|
-
|
220
204
|
/**
|
221
205
|
Used in model definitions to declare use of a sequence. For example:
|
222
206
|
|
@@ -241,16 +225,15 @@ FactoryGuy = {
|
|
241
225
|
definition containing the sequence
|
242
226
|
*/
|
243
227
|
generate: function (nameOrFunction) {
|
244
|
-
var sortaRandomName = Math.floor((1 + Math.random()) *
|
228
|
+
var sortaRandomName = Math.floor((1 + Math.random()) * 65536).toString(16) + Date.now();
|
245
229
|
return function () {
|
246
|
-
if (Em.typeOf(nameOrFunction) ==
|
230
|
+
if (Em.typeOf(nameOrFunction) == 'function') {
|
247
231
|
return this.generate(sortaRandomName, nameOrFunction);
|
248
232
|
} else {
|
249
233
|
return this.generate(nameOrFunction);
|
250
234
|
}
|
251
|
-
}
|
235
|
+
};
|
252
236
|
},
|
253
|
-
|
254
237
|
/**
|
255
238
|
Used in model definitions to define a belongsTo association attribute.
|
256
239
|
For example:
|
@@ -282,14 +265,12 @@ FactoryGuy = {
|
|
282
265
|
belongsTo: function (fixtureName, opts) {
|
283
266
|
return function () {
|
284
267
|
return FactoryGuy.build(fixtureName, opts);
|
285
|
-
}
|
268
|
+
};
|
286
269
|
},
|
287
|
-
|
288
|
-
|
289
|
-
console.log('DEPRECATION Warning: use FactoryGuy.belongsTo instead')
|
270
|
+
association: function (fixtureName, opts) {
|
271
|
+
console.log('DEPRECATION Warning: use FactoryGuy.belongsTo instead');
|
290
272
|
return this.belongsTo(fixtureName, opts);
|
291
273
|
},
|
292
|
-
|
293
274
|
/**
|
294
275
|
Used in model definitions to define a hasMany association attribute.
|
295
276
|
For example:
|
@@ -321,9 +302,8 @@ FactoryGuy = {
|
|
321
302
|
hasMany: function (fixtureName, number, opts) {
|
322
303
|
return function () {
|
323
304
|
return FactoryGuy.buildList(fixtureName, number, opts);
|
324
|
-
}
|
305
|
+
};
|
325
306
|
},
|
326
|
-
|
327
307
|
/**
|
328
308
|
Given a fixture name like 'person' or 'dude' determine what model this name
|
329
309
|
refers to. In this case it's 'person' for each one.
|
@@ -334,9 +314,10 @@ FactoryGuy = {
|
|
334
314
|
*/
|
335
315
|
lookupModelForFixtureName: function (name) {
|
336
316
|
var definition = this.lookupDefinitionForFixtureName(name);
|
337
|
-
if (definition) {
|
317
|
+
if (definition) {
|
318
|
+
return definition.model;
|
319
|
+
}
|
338
320
|
},
|
339
|
-
|
340
321
|
/**
|
341
322
|
|
342
323
|
@param {String} name a fixture name could be model name like 'person'
|
@@ -344,15 +325,13 @@ FactoryGuy = {
|
|
344
325
|
@returns {ModelDefinition} ModelDefinition associated with model or undefined if not found
|
345
326
|
*/
|
346
327
|
lookupDefinitionForFixtureName: function (name) {
|
347
|
-
for (model in this.modelDefinitions) {
|
328
|
+
for (var model in this.modelDefinitions) {
|
348
329
|
var definition = this.modelDefinitions[model];
|
349
330
|
if (definition.matchesName(name)) {
|
350
331
|
return definition;
|
351
332
|
}
|
352
333
|
}
|
353
334
|
},
|
354
|
-
|
355
|
-
|
356
335
|
/**
|
357
336
|
Build fixtures for model or specific fixture name. For example:
|
358
337
|
|
@@ -369,23 +348,22 @@ FactoryGuy = {
|
|
369
348
|
*/
|
370
349
|
build: function () {
|
371
350
|
var args = Array.prototype.slice.call(arguments);
|
372
|
-
var opts = {}
|
351
|
+
var opts = {};
|
373
352
|
var name = args.shift();
|
374
353
|
if (!name) {
|
375
|
-
throw new Error(
|
354
|
+
throw new Error('Build needs a factory name to build');
|
376
355
|
}
|
377
|
-
if (Ember.typeOf(args[args.length-1]) == 'object') {
|
378
|
-
opts
|
356
|
+
if (Ember.typeOf(args[args.length - 1]) == 'object') {
|
357
|
+
opts = args.pop();
|
379
358
|
}
|
380
|
-
var traits = args;
|
381
|
-
|
359
|
+
var traits = args;
|
360
|
+
// whatever is left are traits
|
382
361
|
var definition = this.lookupDefinitionForFixtureName(name);
|
383
362
|
if (!definition) {
|
384
|
-
throw new Error(
|
363
|
+
throw new Error('Can\'t find that factory named [' + name + ']');
|
385
364
|
}
|
386
365
|
return definition.build(name, opts, traits);
|
387
366
|
},
|
388
|
-
|
389
367
|
/**
|
390
368
|
Build list of fixtures for model or specific fixture name. For example:
|
391
369
|
|
@@ -403,21 +381,28 @@ FactoryGuy = {
|
|
403
381
|
var name = args.shift();
|
404
382
|
var number = args.shift();
|
405
383
|
if (!name || !number) {
|
406
|
-
throw new Error(
|
384
|
+
throw new Error('buildList needs a name and a number ( at least ) to build with');
|
407
385
|
}
|
408
|
-
var opts = {}
|
409
|
-
if (Ember.typeOf(args[args.length-1]) == 'object') {
|
410
|
-
opts
|
386
|
+
var opts = {};
|
387
|
+
if (Ember.typeOf(args[args.length - 1]) == 'object') {
|
388
|
+
opts = args.pop();
|
411
389
|
}
|
412
|
-
var traits = args;
|
413
|
-
|
390
|
+
var traits = args;
|
391
|
+
// whatever is left are traits
|
414
392
|
var definition = this.lookupDefinitionForFixtureName(name);
|
415
393
|
if (!definition) {
|
416
|
-
throw new Error(
|
394
|
+
throw new Error('Can\'t find that factory named [' + name + ']');
|
417
395
|
}
|
418
396
|
return definition.buildList(name, number, traits, opts);
|
419
397
|
},
|
420
|
-
|
398
|
+
/**
|
399
|
+
Unload all models of the given type from the store in the next Ember runloop
|
400
|
+
*/
|
401
|
+
unloadModel: function (modelType) {
|
402
|
+
Ember.run(function () {
|
403
|
+
store.unloadAll(modelType);
|
404
|
+
});
|
405
|
+
},
|
421
406
|
/**
|
422
407
|
TODO: This is kind of problematic right now .. needs work
|
423
408
|
|
@@ -425,7 +410,7 @@ FactoryGuy = {
|
|
425
410
|
Reset the id sequence for the models back to zero.
|
426
411
|
*/
|
427
412
|
resetModels: function (store) {
|
428
|
-
for (model in this.modelDefinitions) {
|
413
|
+
for (var model in this.modelDefinitions) {
|
429
414
|
var definition = this.modelDefinitions[model];
|
430
415
|
definition.reset();
|
431
416
|
try {
|
@@ -433,16 +418,11 @@ FactoryGuy = {
|
|
433
418
|
if (store.usingFixtureAdapter()) {
|
434
419
|
modelType.FIXTURES = [];
|
435
420
|
}
|
436
|
-
|
437
|
-
store.unloadAll(modelType);
|
438
|
-
})
|
421
|
+
this.unloadModel(modelType);
|
439
422
|
} catch (e) {
|
440
|
-
// console.log(e)
|
441
423
|
}
|
442
424
|
}
|
443
425
|
},
|
444
|
-
|
445
|
-
|
446
426
|
/**
|
447
427
|
Push fixture to model's FIXTURES array.
|
448
428
|
Used when store's adapter is a DS.FixtureAdapter.
|
@@ -452,13 +432,12 @@ FactoryGuy = {
|
|
452
432
|
@returns {Object} json fixture data
|
453
433
|
*/
|
454
434
|
pushFixture: function (modelClass, fixture) {
|
455
|
-
if (!modelClass
|
456
|
-
modelClass
|
435
|
+
if (!modelClass.FIXTURES) {
|
436
|
+
modelClass.FIXTURES = [];
|
457
437
|
}
|
458
|
-
modelClass
|
438
|
+
modelClass.FIXTURES.push(fixture);
|
459
439
|
return fixture;
|
460
440
|
},
|
461
|
-
|
462
441
|
/**
|
463
442
|
Clears all model definitions
|
464
443
|
*/
|
@@ -467,7 +446,7 @@ FactoryGuy = {
|
|
467
446
|
this.modelDefinitions = {};
|
468
447
|
}
|
469
448
|
}
|
470
|
-
}
|
449
|
+
};
|
471
450
|
DS.Store.reopen({
|
472
451
|
/**
|
473
452
|
@returns {Boolean} true if store's adapter is DS.FixtureAdapter
|
@@ -476,7 +455,6 @@ DS.Store.reopen({
|
|
476
455
|
var adapter = this.adapterFor('application');
|
477
456
|
return adapter instanceof DS.FixtureAdapter;
|
478
457
|
},
|
479
|
-
|
480
458
|
/**
|
481
459
|
Make new fixture and save to store. If the store is using FixtureAdapter,
|
482
460
|
will push to FIXTURE array, otherwise will use push method on adapter to load
|
@@ -489,23 +467,19 @@ DS.Store.reopen({
|
|
489
467
|
*/
|
490
468
|
makeFixture: function () {
|
491
469
|
var store = this;
|
492
|
-
var fixture = FactoryGuy.build.apply(FactoryGuy,arguments);
|
470
|
+
var fixture = FactoryGuy.build.apply(FactoryGuy, arguments);
|
493
471
|
var name = arguments[0];
|
494
472
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
495
473
|
var modelType = store.modelFor(modelName);
|
496
|
-
|
497
474
|
if (this.usingFixtureAdapter()) {
|
498
475
|
this.setAssociationsForFixtureAdapter(modelType, modelName, fixture);
|
499
476
|
return FactoryGuy.pushFixture(modelType, fixture);
|
500
477
|
} else {
|
501
|
-
return store.makeModel(modelType, fixture)
|
478
|
+
return store.makeModel(modelType, fixture);
|
502
479
|
}
|
503
480
|
},
|
504
|
-
|
505
481
|
makeModel: function (modelType, fixture) {
|
506
|
-
var store = this,
|
507
|
-
modelName = store.modelFor(modelType).typeKey,
|
508
|
-
model;
|
482
|
+
var store = this, modelName = store.modelFor(modelType).typeKey, model;
|
509
483
|
Em.run(function () {
|
510
484
|
store.findEmbeddedAssociationsForRESTAdapter(modelType, fixture);
|
511
485
|
if (fixture.type) {
|
@@ -519,7 +493,6 @@ DS.Store.reopen({
|
|
519
493
|
});
|
520
494
|
return model;
|
521
495
|
},
|
522
|
-
|
523
496
|
/**
|
524
497
|
Make a list of Fixtures
|
525
498
|
|
@@ -532,11 +505,10 @@ DS.Store.reopen({
|
|
532
505
|
var arr = [];
|
533
506
|
var number = arguments[1];
|
534
507
|
for (var i = 0; i < number; i++) {
|
535
|
-
arr.push(this.makeFixture.apply(this,arguments));
|
508
|
+
arr.push(this.makeFixture.apply(this, arguments));
|
536
509
|
}
|
537
510
|
return arr;
|
538
511
|
},
|
539
|
-
|
540
512
|
/**
|
541
513
|
Set the hasMany and belongsTo associations for FixtureAdapter.
|
542
514
|
|
@@ -582,10 +554,9 @@ DS.Store.reopen({
|
|
582
554
|
var hasManyfixtures = adapter.fixturesForType(relationship.type);
|
583
555
|
var fixture = adapter.findFixtureById(hasManyfixtures, id);
|
584
556
|
fixture[modelName] = fixture.id;
|
585
|
-
})
|
557
|
+
});
|
586
558
|
}
|
587
559
|
}
|
588
|
-
|
589
560
|
if (relationship.kind == 'belongsTo') {
|
590
561
|
var belongsToRecord = fixture[relationship.key];
|
591
562
|
if (belongsToRecord) {
|
@@ -597,14 +568,13 @@ DS.Store.reopen({
|
|
597
568
|
var belongsToFixtures = adapter.fixturesForType(relationship.type);
|
598
569
|
var belongsTofixture = adapter.findFixtureById(belongsToFixtures, fixture[relationship.key]);
|
599
570
|
if (!belongsTofixture[hasManyName]) {
|
600
|
-
belongsTofixture[hasManyName] = []
|
571
|
+
belongsTofixture[hasManyName] = [];
|
601
572
|
}
|
602
573
|
belongsTofixture[hasManyName].push(fixture.id);
|
603
574
|
}
|
604
575
|
}
|
605
|
-
})
|
576
|
+
});
|
606
577
|
},
|
607
|
-
|
608
578
|
/**
|
609
579
|
Before pushing the fixture to the store, do some preprocessing. Descend into the tree
|
610
580
|
of object data, and convert child objects to record instances recursively.
|
@@ -631,20 +601,19 @@ DS.Store.reopen({
|
|
631
601
|
var hasManyRecords = fixture[relationship.key];
|
632
602
|
if (Ember.typeOf(hasManyRecords) == 'array') {
|
633
603
|
if (Ember.typeOf(hasManyRecords[0]) == 'object') {
|
634
|
-
var records = Em.A()
|
604
|
+
var records = Em.A();
|
635
605
|
hasManyRecords.map(function (object) {
|
636
606
|
store.findEmbeddedAssociationsForRESTAdapter(relationship.type, object);
|
637
607
|
var record = store.push(relationship.type, object);
|
638
608
|
records.push(record);
|
639
609
|
return record;
|
640
|
-
})
|
610
|
+
});
|
641
611
|
fixture[relationship.key] = records;
|
642
612
|
}
|
643
613
|
}
|
644
614
|
}
|
645
|
-
})
|
615
|
+
});
|
646
616
|
},
|
647
|
-
|
648
617
|
/**
|
649
618
|
For the REST type models:
|
650
619
|
|
@@ -680,47 +649,32 @@ DS.Store.reopen({
|
|
680
649
|
if (relationship.kind == 'hasMany') {
|
681
650
|
var children = model.get(name) || [];
|
682
651
|
children.forEach(function (child) {
|
683
|
-
var belongsToName = self.findRelationshipName(
|
684
|
-
'belongsTo',
|
685
|
-
child.constructor,
|
686
|
-
model
|
687
|
-
);
|
652
|
+
var belongsToName = self.findRelationshipName('belongsTo', child.constructor, model);
|
688
653
|
if (belongsToName) {
|
689
654
|
child.set(belongsToName, model);
|
690
655
|
}
|
691
|
-
})
|
656
|
+
});
|
692
657
|
}
|
693
|
-
})
|
658
|
+
});
|
694
659
|
},
|
695
|
-
|
696
|
-
|
697
660
|
findRelationshipName: function (kind, belongToModelType, childModel) {
|
698
661
|
var relationshipName;
|
699
|
-
Ember.get(belongToModelType, 'relationshipsByName').forEach(
|
700
|
-
|
701
|
-
|
702
|
-
childModel instanceof relationship.type) {
|
703
|
-
relationshipName = relationship.key;
|
704
|
-
}
|
662
|
+
Ember.get(belongToModelType, 'relationshipsByName').forEach(function (relationship, name) {
|
663
|
+
if (relationship.kind == kind && childModel instanceof relationship.type) {
|
664
|
+
relationshipName = relationship.key;
|
705
665
|
}
|
706
|
-
)
|
666
|
+
});
|
707
667
|
return relationshipName;
|
708
668
|
},
|
709
|
-
|
710
669
|
findHasManyRelationshipNameForFixtureAdapter: function (belongToModelType, childModelType) {
|
711
670
|
var relationshipName;
|
712
|
-
Ember.get(belongToModelType, 'relationshipsByName').forEach(
|
713
|
-
|
714
|
-
|
715
|
-
childModelType == relationship.type) {
|
716
|
-
relationshipName = relationship.key;
|
717
|
-
}
|
671
|
+
Ember.get(belongToModelType, 'relationshipsByName').forEach(function (relationship, name) {
|
672
|
+
if (relationship.kind == 'hasMany' && childModelType == relationship.type) {
|
673
|
+
relationshipName = relationship.key;
|
718
674
|
}
|
719
|
-
)
|
675
|
+
});
|
720
676
|
return relationshipName;
|
721
677
|
},
|
722
|
-
|
723
|
-
|
724
678
|
/**
|
725
679
|
Adding a pushPayload for FixtureAdapter, but using the original with
|
726
680
|
other adapters that support pushPayload.
|
@@ -737,31 +691,26 @@ DS.Store.reopen({
|
|
737
691
|
}
|
738
692
|
}
|
739
693
|
});
|
740
|
-
|
741
694
|
FactoryGuyTestMixin = Em.Mixin.create({
|
742
|
-
|
743
695
|
// Pass in the app root, which typically is App.
|
744
696
|
setup: function (app) {
|
745
697
|
this.set('container', app.__container__);
|
746
698
|
return this;
|
747
699
|
},
|
748
|
-
|
749
700
|
useFixtureAdapter: function (app) {
|
750
701
|
app.ApplicationAdapter = DS.FixtureAdapter;
|
751
702
|
this.getStore().adapterFor('application').simulateRemoteResponse = false;
|
752
703
|
},
|
753
|
-
|
754
704
|
/**
|
755
705
|
@param {String} model type like user for model User
|
756
706
|
@return {boolean} true if model's serializer is ActiveModelSerializer based
|
757
707
|
*/
|
758
708
|
usingActiveModelSerializer: function (type) {
|
759
|
-
var store = this.getStore()
|
760
|
-
var
|
761
|
-
var serializer = store.serializerFor(
|
709
|
+
var store = this.getStore();
|
710
|
+
var modelType = store.modelFor(type);
|
711
|
+
var serializer = store.serializerFor(modelType.typeKey);
|
762
712
|
return serializer instanceof DS.ActiveModelSerializer;
|
763
713
|
},
|
764
|
-
|
765
714
|
/**
|
766
715
|
Proxy to store's find method
|
767
716
|
|
@@ -772,7 +721,6 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
772
721
|
find: function (type, id) {
|
773
722
|
return this.getStore().find(type, id);
|
774
723
|
},
|
775
|
-
|
776
724
|
/**
|
777
725
|
Proxy to store's makeFixture method
|
778
726
|
|
@@ -781,19 +729,15 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
781
729
|
var store = this.getStore();
|
782
730
|
return store.makeFixture.apply(store, arguments);
|
783
731
|
},
|
784
|
-
|
785
732
|
getStore: function () {
|
786
733
|
return this.get('container').lookup('store:main');
|
787
734
|
},
|
788
|
-
|
789
735
|
pushPayload: function (type, hash) {
|
790
736
|
return this.getStore().pushPayload(type, hash);
|
791
737
|
},
|
792
|
-
|
793
738
|
pushRecord: function (type, hash) {
|
794
739
|
return this.getStore().push(type, hash);
|
795
740
|
},
|
796
|
-
|
797
741
|
/**
|
798
742
|
Using mockjax to stub an http request.
|
799
743
|
|
@@ -809,16 +753,12 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
809
753
|
responseText: json,
|
810
754
|
type: options.type || 'GET',
|
811
755
|
status: options.status || 200
|
812
|
-
}
|
813
|
-
|
756
|
+
};
|
814
757
|
if (options.data) {
|
815
|
-
request.data = options.data
|
758
|
+
request.data = options.data;
|
816
759
|
}
|
817
|
-
|
818
760
|
$.mockjax(request);
|
819
761
|
},
|
820
|
-
|
821
|
-
|
822
762
|
/**
|
823
763
|
Build url for the mockjax call. Proxy to the adapters buildURL method.
|
824
764
|
|
@@ -830,8 +770,6 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
830
770
|
var type = this.getStore().modelFor(typeName);
|
831
771
|
return this.getStore().adapterFor(type).buildURL(type.typeKey, id);
|
832
772
|
},
|
833
|
-
|
834
|
-
|
835
773
|
/**
|
836
774
|
Handling ajax GET for finding all records for a type of model.
|
837
775
|
You can mock failed find by passing in success argument as false.
|
@@ -844,53 +782,38 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
844
782
|
handleFindMany: function () {
|
845
783
|
var store = this.getStore();
|
846
784
|
// make the records and load them in the store
|
847
|
-
store.makeList.apply(store,arguments);
|
848
|
-
|
785
|
+
store.makeList.apply(store, arguments);
|
849
786
|
var name = arguments[0];
|
850
787
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
851
788
|
var responseJson = {};
|
852
|
-
responseJson[modelName]=[];
|
789
|
+
responseJson[modelName] = [];
|
853
790
|
var url = this.buildURL(modelName);
|
854
791
|
// mock the ajax call, but return nothing, since the records will be
|
855
792
|
// retrieved from the store where they were just loaded above
|
856
|
-
this.stubEndpointForHttpRequest(url, responseJson, {type: 'GET'})
|
793
|
+
this.stubEndpointForHttpRequest(url, responseJson, { type: 'GET' });
|
857
794
|
},
|
858
|
-
|
859
795
|
/**
|
860
796
|
Handling ajax POST ( create record ) for a model. You can mock
|
861
797
|
failed create by passing in success argument as false.
|
862
798
|
|
863
|
-
@param {String}
|
864
|
-
@param {
|
865
|
-
@param {Object} opts optional fixture options
|
799
|
+
@param {String} modelName name of model your creating like 'profile' for Profile
|
800
|
+
@param {Object} options hash of model options ( that do not include associations )
|
866
801
|
@param {Boolean} succeed optional flag to indicate if the request
|
867
802
|
should succeed ( default is true )
|
868
803
|
*/
|
869
|
-
handleCreate: function () {
|
870
|
-
|
871
|
-
|
872
|
-
var succeed = true;
|
873
|
-
if (Ember.typeOf(args[args.length-1]) == 'boolean') {
|
874
|
-
succeed = args.pop();
|
875
|
-
}
|
876
|
-
|
877
|
-
var name = args[0];
|
878
|
-
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
804
|
+
handleCreate: function (modelName, options, succeed) {
|
805
|
+
succeed = succeed === undefined ? true : succeed;
|
806
|
+
options = options === undefined ? {} : options;
|
879
807
|
var url = this.buildURL(modelName);
|
808
|
+
var definition = FactoryGuy.modelDefinitions[modelName];
|
880
809
|
var responseJson = {};
|
881
|
-
var httpOptions = {type: 'POST'}
|
882
|
-
|
810
|
+
var httpOptions = { type: 'POST' };
|
883
811
|
if (succeed) {
|
884
|
-
|
885
|
-
// make the record and load it in the store
|
886
|
-
var model = store.makeFixture.apply(store,args);
|
887
|
-
// believe it or not .. this actually works
|
888
|
-
responseJson[modelName]=model;
|
812
|
+
responseJson[modelName] = $.extend(options,{id: definition.nextId()});
|
889
813
|
} else {
|
890
814
|
httpOptions.status = 500;
|
891
815
|
}
|
892
|
-
|
893
|
-
this.stubEndpointForHttpRequest(url, responseJson, httpOptions)
|
816
|
+
this.stubEndpointForHttpRequest(url, responseJson, httpOptions);
|
894
817
|
},
|
895
818
|
|
896
819
|
/**
|
@@ -904,14 +827,11 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
904
827
|
*/
|
905
828
|
handleUpdate: function (type, id, succeed) {
|
906
829
|
succeed = succeed === undefined ? true : succeed;
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
{type: 'PUT', status: (succeed ? 200 : 500)}
|
912
|
-
)
|
830
|
+
this.stubEndpointForHttpRequest(this.buildURL(type, id), {}, {
|
831
|
+
type: 'PUT',
|
832
|
+
status: succeed ? 200 : 500
|
833
|
+
});
|
913
834
|
},
|
914
|
-
|
915
835
|
/**
|
916
836
|
Handling ajax DELETE ( delete record ) for a model type. You can mock
|
917
837
|
failed delete by passing in success argument as false.
|
@@ -923,14 +843,11 @@ FactoryGuyTestMixin = Em.Mixin.create({
|
|
923
843
|
*/
|
924
844
|
handleDelete: function (type, id, succeed) {
|
925
845
|
succeed = succeed === undefined ? true : succeed;
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
{type: 'DELETE', status: (succeed ? 200 : 500)}
|
931
|
-
)
|
846
|
+
this.stubEndpointForHttpRequest(this.buildURL(type, id), {}, {
|
847
|
+
type: 'DELETE',
|
848
|
+
status: succeed ? 200 : 500
|
849
|
+
});
|
932
850
|
},
|
933
|
-
|
934
851
|
teardown: function () {
|
935
852
|
FactoryGuy.resetModels(this.getStore());
|
936
853
|
$.mockjax.clear();
|