ember-data-factory-guy 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/.travis.yml +7 -0
- data/Gruntfile.js +18 -3
- data/README.md +122 -16
- data/bower.json +6 -2
- data/dist/ember-data-factory-guy.js +264 -107
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/dist/factory_guy_has_many.js +188 -0
- data/ember-data-factory-guy.gemspec +1 -1
- data/lib/ember-data-factory-guy.rb +0 -1
- data/package.json +4 -4
- data/src/factory_guy.js +114 -89
- data/src/{factory_guy_helper_mixin.js → factory_guy_test_mixin.js} +2 -1
- data/src/model_definition.js +98 -0
- data/src/sequence.js +16 -0
- data/src/store.js +40 -23
- data/tests/active_model_adapter_factory_test.js +45 -5
- data/tests/factory_guy_test.js +93 -0
- data/tests/fixture_adapter_factory_test.js +14 -40
- data/tests/index.html +1 -0
- data/tests/rest_adapter_factory_test.js +18 -4
- data/tests/support/factories/project_factory.js +3 -1
- data/tests/support/factories/user_factory.js +1 -1
- data/tests/support/libs/sinon.js +4287 -0
- data/tests/support/models/user.js +1 -1
- data/tests/support/test_helper.js +2 -2
- data/tests/test_setup.js +4291 -2
- data/vendor/assets/javascripts/ember_data_factory_guy.js +155 -113
- metadata +8 -3
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Sequence=function(fn){var index=1;var fn=fn;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 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(sequenceName){if(!sequences[sequenceName]){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequences[sequenceName].next()};this.build=function(name,opts){var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,opts);for(attr in fixture){if(typeof fixture[attr]=="function"){fixture[attr]=fixture[attr].call(this)}}if(!fixture.id){fixture.id=modelId++}return fixture};this.buildList=function(name,number,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts))}return arr};this.reset=function(){modelId=1};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(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;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};FactoryGuy=Ember.Object.reopenClass({modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},generate:function(sequenceName){return function(){return this.generate(sequenceName)}},lookupModelForName:function(name){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition.model}}},lookupDefinitionForName:function(name){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(name,opts){var definition=this.lookupDefinitionForName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts)},buildList:function(name,number,opts){var definition=this.lookupDefinitionForName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,opts)},resetModels:function(store){var typeMaps=store.typeMaps;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}});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.setBelongsToFixturesAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var self=this;var model;Em.run(function(){model=self.push(modelName,fixture);self.setBelongsToRESTAdapter(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},setBelongsToFixturesAdapter: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})}})}},setBelongsToRESTAdapter: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}});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},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())}});
|
@@ -0,0 +1,188 @@
|
|
1
|
+
(function(){
|
2
|
+
var get = Ember.get, set = Ember.set, setProperties = Ember.setProperties;
|
3
|
+
|
4
|
+
function asyncHasMany(record, type, options, meta) {
|
5
|
+
var relationship = record._relationships[key],
|
6
|
+
promiseLabel = "DS: Async hasMany " + record + " : " + key;
|
7
|
+
|
8
|
+
if (!relationship) {
|
9
|
+
var resolver = Ember.RSVP.defer(promiseLabel);
|
10
|
+
relationship = buildRelationship(record, key, options, function(store, data) {
|
11
|
+
var link = data.links && data.links[key];
|
12
|
+
var rel;
|
13
|
+
if (link) {
|
14
|
+
rel = store.findHasMany(record, link, meta, resolver);
|
15
|
+
} else {
|
16
|
+
rel = store.findMany(record, data[key], meta.type, resolver);
|
17
|
+
}
|
18
|
+
// cache the promise so we can use it
|
19
|
+
// when we come back and don't need to rebuild
|
20
|
+
// the relationship.
|
21
|
+
set(rel, 'promise', resolver.promise);
|
22
|
+
return rel;
|
23
|
+
});
|
24
|
+
}
|
25
|
+
|
26
|
+
var promise = relationship.get('promise').then(function() {
|
27
|
+
return relationship;
|
28
|
+
}, null, "DS: Async hasMany records received");
|
29
|
+
|
30
|
+
return DS.PromiseArray.create({
|
31
|
+
promise: promise
|
32
|
+
});
|
33
|
+
}
|
34
|
+
|
35
|
+
function buildRelationship(record, key, options, callback) {
|
36
|
+
var rels = record._relationships;
|
37
|
+
|
38
|
+
if (rels[key]) { return rels[key]; }
|
39
|
+
|
40
|
+
var data = get(record, 'data'),
|
41
|
+
store = get(record, 'store');
|
42
|
+
|
43
|
+
var relationship = rels[key] = callback.call(record, store, data);
|
44
|
+
|
45
|
+
return setProperties(relationship, {
|
46
|
+
owner: record,
|
47
|
+
name: key,
|
48
|
+
isPolymorphic: options.polymorphic
|
49
|
+
});
|
50
|
+
}
|
51
|
+
|
52
|
+
function hasRelationship(type, options) {
|
53
|
+
options = options || {};
|
54
|
+
|
55
|
+
var meta = {
|
56
|
+
type: type,
|
57
|
+
isRelationship: true,
|
58
|
+
options: options,
|
59
|
+
kind: 'hasMany'
|
60
|
+
};
|
61
|
+
|
62
|
+
return Ember.computed('data', function(key) {
|
63
|
+
var adapter = this.store.adapterFor('application');
|
64
|
+
if (adapter instanceof DS.FixtureAdapter) {
|
65
|
+
var relationship = this._relationships[key],
|
66
|
+
promiseLabel = "DS: Async hasMany " + this + " : " + key;
|
67
|
+
|
68
|
+
if (!relationship) {
|
69
|
+
var resolver = Ember.RSVP.defer(promiseLabel);
|
70
|
+
relationship = buildRelationship(this, key, options, function(store, data) {
|
71
|
+
var link = data.links && data.links[key];
|
72
|
+
var rel;
|
73
|
+
if (link) {
|
74
|
+
rel = store.findHasMany(this, link, meta, resolver);
|
75
|
+
} else {
|
76
|
+
rel = store.findMany(this, data[key], meta.type, resolver);
|
77
|
+
}
|
78
|
+
// cache the promise so we can use it
|
79
|
+
// when we come back and don't need to rebuild
|
80
|
+
// the relationship.
|
81
|
+
set(rel, 'promise', resolver.promise);
|
82
|
+
return rel;
|
83
|
+
});
|
84
|
+
}
|
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
|
+
});
|
93
|
+
}
|
94
|
+
|
95
|
+
return buildRelationship(this, key, options, function(store, data) {
|
96
|
+
var records = data[key];
|
97
|
+
Ember.assert("You looked up the '" + key + "' relationship on '" + this + "' but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (`DS.hasMany({ async: true })`)", Ember.A(records).everyProperty('isEmpty', false));
|
98
|
+
return store.findMany(this, data[key], meta.type);
|
99
|
+
});
|
100
|
+
}).meta(meta).readOnly();
|
101
|
+
}
|
102
|
+
|
103
|
+
/**
|
104
|
+
`DS.hasMany` is used to define One-To-Many and Many-To-Many
|
105
|
+
relationships on a [DS.Model](/api/data/classes/DS.Model.html).
|
106
|
+
|
107
|
+
`DS.hasMany` takes an optional hash as a second parameter, currently
|
108
|
+
supported options are:
|
109
|
+
|
110
|
+
- `async`: A boolean value used to explicitly declare this to be an async relationship.
|
111
|
+
- `inverse`: A string used to identify the inverse property on a related model.
|
112
|
+
|
113
|
+
#### One-To-Many
|
114
|
+
To declare a one-to-many relationship between two models, use
|
115
|
+
`DS.belongsTo` in combination with `DS.hasMany`, like this:
|
116
|
+
|
117
|
+
```javascript
|
118
|
+
App.Post = DS.Model.extend({
|
119
|
+
comments: DS.hasMany('comment')
|
120
|
+
});
|
121
|
+
|
122
|
+
App.Comment = DS.Model.extend({
|
123
|
+
post: DS.belongsTo('post')
|
124
|
+
});
|
125
|
+
```
|
126
|
+
|
127
|
+
#### Many-To-Many
|
128
|
+
To declare a many-to-many relationship between two models, use
|
129
|
+
`DS.hasMany`:
|
130
|
+
|
131
|
+
```javascript
|
132
|
+
App.Post = DS.Model.extend({
|
133
|
+
tags: DS.hasMany('tag')
|
134
|
+
});
|
135
|
+
|
136
|
+
App.Tag = DS.Model.extend({
|
137
|
+
posts: DS.hasMany('post')
|
138
|
+
});
|
139
|
+
```
|
140
|
+
|
141
|
+
#### Explicit Inverses
|
142
|
+
|
143
|
+
Ember Data will do its best to discover which relationships map to
|
144
|
+
one another. In the one-to-many code above, for example, Ember Data
|
145
|
+
can figure out that changing the `comments` relationship should update
|
146
|
+
the `post` relationship on the inverse because post is the only
|
147
|
+
relationship to that model.
|
148
|
+
|
149
|
+
However, sometimes you may have multiple `belongsTo`/`hasManys` for the
|
150
|
+
same type. You can specify which property on the related model is
|
151
|
+
the inverse using `DS.hasMany`'s `inverse` option:
|
152
|
+
|
153
|
+
```javascript
|
154
|
+
var belongsTo = DS.belongsTo,
|
155
|
+
hasMany = DS.hasMany;
|
156
|
+
|
157
|
+
App.Comment = DS.Model.extend({
|
158
|
+
onePost: belongsTo('post'),
|
159
|
+
twoPost: belongsTo('post'),
|
160
|
+
redPost: belongsTo('post'),
|
161
|
+
bluePost: belongsTo('post')
|
162
|
+
});
|
163
|
+
|
164
|
+
App.Post = DS.Model.extend({
|
165
|
+
comments: hasMany('comment', {
|
166
|
+
inverse: 'redPost'
|
167
|
+
})
|
168
|
+
});
|
169
|
+
```
|
170
|
+
|
171
|
+
You can also specify an inverse on a `belongsTo`, which works how
|
172
|
+
you'd expect.
|
173
|
+
|
174
|
+
@namespace
|
175
|
+
@method hasMany
|
176
|
+
@for DS
|
177
|
+
@param {String or DS.Model} type the model type of the relationship
|
178
|
+
@param {Object} options a hash of options
|
179
|
+
@return {Ember.computed} relationship
|
180
|
+
*/
|
181
|
+
DS.hasMany = function(type, options) {
|
182
|
+
if (typeof type === 'object') {
|
183
|
+
options = type;
|
184
|
+
type = undefined;
|
185
|
+
}
|
186
|
+
return hasRelationship(type, options);
|
187
|
+
}
|
188
|
+
}).call();
|
@@ -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.1.
|
4
|
+
s.version = "0.1.2"
|
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"]
|
@@ -7,7 +7,6 @@ module EmberDataFixtureFactory
|
|
7
7
|
elsif defined? ::Sprockets
|
8
8
|
root_dir = File.expand_path(File.dirname(File.dirname(__FILE__)))
|
9
9
|
# Set up asset paths for Sprockets apps
|
10
|
-
p "root_dir #{root_dir} #{File.join(root_dir, "vendor", "assets", "javascripts")}"
|
11
10
|
::Sprockets.append_path File.join(root_dir, "vendor", "assets", "javascripts")
|
12
11
|
end
|
13
12
|
end
|
data/package.json
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
{
|
2
2
|
"name": "ember-data-factory-guy",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.1.2",
|
4
4
|
"authors": [
|
5
|
-
"
|
6
|
-
"
|
5
|
+
"Daniel Sudol <dansudol@yahoo.com>",
|
6
|
+
"Opak Alex <opak.alexandr@gmail.com>"
|
7
7
|
],
|
8
8
|
"description": "Factory for testing Ember application",
|
9
9
|
"main": "dist/ember-data-factory-guy.js",
|
@@ -20,7 +20,7 @@
|
|
20
20
|
},
|
21
21
|
|
22
22
|
"scripts": {
|
23
|
-
"test": "
|
23
|
+
"test": "grunt test"
|
24
24
|
},
|
25
25
|
|
26
26
|
"repository": {
|
data/src/factory_guy.js
CHANGED
@@ -1,137 +1,162 @@
|
|
1
1
|
FactoryGuy = Ember.Object.reopenClass({
|
2
|
-
|
3
|
-
fixtureLookup: {},
|
4
|
-
modelIds: {},
|
2
|
+
modelDefinitions: {},
|
5
3
|
|
6
4
|
/**
|
7
5
|
```javascript
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
7
|
+
Person = DS.Model.extend({
|
8
|
+
type: DS.attr('string'),
|
9
|
+
name: DS.attr('string')
|
10
|
+
})
|
11
|
+
|
12
|
+
FactoryGuy.define('person', {
|
13
|
+
sequences: {
|
14
|
+
personName: function(num) {
|
15
|
+
return 'person #' + num;
|
16
|
+
},
|
17
|
+
personType: function(num) {
|
18
|
+
return 'person type #' + num;
|
19
|
+
}
|
20
|
+
},
|
21
|
+
default: {
|
22
|
+
type: 'normal',
|
23
|
+
name: FactoryGuy.generate('personName')
|
24
|
+
},
|
25
|
+
dude: {
|
26
|
+
type: FactoryGuy.generate('personType')
|
27
|
+
},
|
28
|
+
});
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
For the Person model, you can define fixtures like 'dude' or just use 'person'
|
33
|
+
and get default values.
|
34
|
+
|
35
|
+
And to get those fixtures you would call them this way:
|
36
|
+
|
37
|
+
FactoryGuy.build('person') or FactoryGuy.build('dude')
|
38
|
+
|
39
|
+
@param model the model to define
|
40
|
+
@param config your model definition object
|
34
41
|
*/
|
35
42
|
define: function (model, config) {
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
if (key != 'default') {
|
41
|
-
this.fixtureLookup[key] = model;
|
42
|
-
}
|
43
|
+
if (this.modelDefinitions[model]) {
|
44
|
+
this.modelDefinitions[model].merge(config);
|
45
|
+
} else {
|
46
|
+
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
43
47
|
}
|
44
|
-
// setup id
|
45
|
-
this.modelIds[model] = 0;
|
46
48
|
},
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
*/
|
52
|
-
getModelInfo: function (model) {
|
53
|
-
if (!this.fixtureStore[model]) {
|
54
|
-
this.fixtureStore[model] = {};
|
50
|
+
generate: function (sequenceName) {
|
51
|
+
return function () {
|
52
|
+
return this.generate(sequenceName);
|
55
53
|
}
|
56
|
-
return this.fixtureStore[model];
|
57
54
|
},
|
58
55
|
|
56
|
+
|
59
57
|
/**
|
60
58
|
|
61
|
-
|
62
|
-
|
59
|
+
@param name fixture name could be model name like 'user'
|
60
|
+
or specific user like 'admin'
|
61
|
+
@returns model associated with fixture name
|
63
62
|
*/
|
64
63
|
lookupModelForName: function (name) {
|
65
|
-
|
66
|
-
|
67
|
-
if (
|
68
|
-
model
|
64
|
+
for (model in this.modelDefinitions) {
|
65
|
+
var definition = this.modelDefinitions[model];
|
66
|
+
if (definition.matchesName(name)) {
|
67
|
+
return definition.model;
|
69
68
|
}
|
70
69
|
}
|
71
|
-
return model;
|
72
70
|
},
|
73
71
|
|
74
72
|
/**
|
75
|
-
|
73
|
+
|
74
|
+
@param name fixture name could be model name like 'user'
|
75
|
+
or specific user like 'admin'
|
76
|
+
@returns definition associated with model
|
76
77
|
*/
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
lookupDefinitionForName: function (name) {
|
79
|
+
for (model in this.modelDefinitions) {
|
80
|
+
var definition = this.modelDefinitions[model];
|
81
|
+
if (definition.matchesName(name)) {
|
82
|
+
return definition;
|
83
|
+
}
|
84
|
+
}
|
81
85
|
},
|
82
86
|
|
87
|
+
|
83
88
|
/**
|
84
|
-
|
89
|
+
Build fixtures for model or specific fixture name. For example:
|
85
90
|
|
86
|
-
|
87
|
-
|
91
|
+
FactoryGuy.build('user') for User model
|
92
|
+
FactoryGuy.build('bob') for User model with bob attributes
|
88
93
|
|
89
|
-
|
90
|
-
|
91
|
-
|
94
|
+
@param name fixture name
|
95
|
+
@param opts options that will override default fixture values
|
96
|
+
@returns {*}
|
92
97
|
*/
|
93
98
|
build: function (name, opts) {
|
94
|
-
var
|
95
|
-
if (!
|
96
|
-
throw new Error("
|
99
|
+
var definition = this.lookupDefinitionForName(name);
|
100
|
+
if (!definition) {
|
101
|
+
throw new Error("Can't find that factory named [" + name + "]");
|
97
102
|
}
|
103
|
+
return definition.build(name, opts);
|
104
|
+
},
|
105
|
+
|
106
|
+
/**
|
107
|
+
Build list of fixtures for model or specific fixture name. For example:
|
98
108
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
109
|
+
FactoryGuy.buildList('user', 2) for 2 User models
|
110
|
+
FactoryGuy.build('bob', 2) for 2 User model with bob attributes
|
111
|
+
|
112
|
+
@param name fixture name
|
113
|
+
@param number number of fixtures to create
|
114
|
+
@param opts options that will override default fixture values
|
115
|
+
@returns list of fixtures
|
116
|
+
*/
|
117
|
+
buildList: function (name, number, opts) {
|
118
|
+
var definition = this.lookupDefinitionForName(name);
|
119
|
+
if (!definition) {
|
120
|
+
throw new Error("Can't find that factory named [" + name + "]");
|
105
121
|
}
|
106
|
-
return
|
122
|
+
return definition.buildList(name, number, opts);
|
107
123
|
},
|
108
124
|
|
109
125
|
/**
|
110
|
-
|
111
|
-
|
126
|
+
Clear model instances from FIXTURES array, and from store cache.
|
127
|
+
Reset the id sequence for the models back to zero.
|
112
128
|
*/
|
113
129
|
resetModels: function (store) {
|
114
130
|
var typeMaps = store.typeMaps;
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
131
|
+
for (model in this.modelDefinitions) {
|
132
|
+
var definition = this.modelDefinitions[model];
|
133
|
+
definition.reset();
|
134
|
+
try {
|
135
|
+
var modelType = store.modelFor(definition.model);
|
136
|
+
if (store.usingFixtureAdapter()) {
|
137
|
+
modelType.FIXTURES = [];
|
138
|
+
}
|
119
139
|
store.unloadAll(modelType);
|
140
|
+
} catch (e) {
|
120
141
|
}
|
121
|
-
} else {
|
122
|
-
for (model in typeMaps) {
|
123
|
-
store.unloadAll(typeMaps[model].type);
|
124
|
-
}
|
142
|
+
// } else {
|
143
|
+
// for (model in typeMaps) {
|
144
|
+
// store.unloadAll(typeMaps[model].type);
|
145
|
+
// }
|
146
|
+
// }
|
147
|
+
|
148
|
+
// for (model in this.modelDefinitions) {
|
149
|
+
// this.modelDefinitions[model].reset();
|
125
150
|
}
|
126
|
-
this.modelIds = {}
|
127
151
|
},
|
128
152
|
|
129
153
|
/**
|
130
|
-
|
131
|
-
|
154
|
+
Push fixture to model's FIXTURES array.
|
155
|
+
Used when store's adapter is a DS.FixtureAdapter.
|
132
156
|
|
133
|
-
|
134
|
-
|
157
|
+
@param modelClass DS.Model type
|
158
|
+
@param fixture the fixture to add
|
159
|
+
@returns json fixture data
|
135
160
|
*/
|
136
161
|
pushFixture: function (modelClass, fixture) {
|
137
162
|
if (!modelClass['FIXTURES']) {
|
@@ -140,4 +165,4 @@ FactoryGuy = Ember.Object.reopenClass({
|
|
140
165
|
modelClass['FIXTURES'].push(fixture);
|
141
166
|
return fixture;
|
142
167
|
}
|
143
|
-
})
|
168
|
+
})
|