ember-data-factory-guy 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/Gruntfile.js +45 -0
- data/README.md +118 -0
- data/bower.json +35 -0
- data/dist/ember-data-factory-guy.js +376 -0
- data/dist/ember-data-factory-guy.min.js +1 -0
- data/ember-data-factory-guy.gemspec +20 -0
- data/lib/ember-data-factory-guy.rb +13 -0
- data/package.json +41 -0
- data/src/factory_guy.js +144 -0
- data/src/factory_guy_helper_mixin.js +89 -0
- data/src/has_many.js +188 -0
- data/src/store.js +142 -0
- data/tests/active_model_adapter_factory_test.js +63 -0
- data/tests/factory_guy_test.js +0 -0
- data/tests/fixture_adapter_factory_test.js +134 -0
- data/tests/index.html +38 -0
- data/tests/rest_adapter_factory_test.js +61 -0
- data/tests/store_test.js +35 -0
- data/tests/support/factories/project_factory.js +3 -0
- data/tests/support/factories/user_factory.js +10 -0
- data/tests/support/models/project.js +4 -0
- data/tests/support/models/user.js +4 -0
- data/tests/support/test_helper.js +23 -0
- data/tests/test_setup.js +22 -0
- data/vendor/assets/javascripts/ember_data_factory_guy.js +376 -0
- data/vendor/assets/javascripts/factory_guy_has_many.js +188 -0
- metadata +76 -0
@@ -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.toString().match('Fixture')) {
|
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();
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ember-data-factory-guy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- daniel sudol
|
9
|
+
- alex opak
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2014-04-23 00:00:00.000000000 +03:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
description: Create Fixtures for Ember Data
|
17
|
+
email:
|
18
|
+
- dansudol@yahoo.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gruntfile.js
|
25
|
+
- README.md
|
26
|
+
- bower.json
|
27
|
+
- dist/ember-data-factory-guy.js
|
28
|
+
- dist/ember-data-factory-guy.min.js
|
29
|
+
- ember-data-factory-guy.gemspec
|
30
|
+
- lib/ember-data-factory-guy.rb
|
31
|
+
- package.json
|
32
|
+
- src/factory_guy.js
|
33
|
+
- src/factory_guy_helper_mixin.js
|
34
|
+
- src/has_many.js
|
35
|
+
- src/store.js
|
36
|
+
- tests/active_model_adapter_factory_test.js
|
37
|
+
- tests/factory_guy_test.js
|
38
|
+
- tests/fixture_adapter_factory_test.js
|
39
|
+
- tests/index.html
|
40
|
+
- tests/rest_adapter_factory_test.js
|
41
|
+
- tests/store_test.js
|
42
|
+
- tests/support/factories/project_factory.js
|
43
|
+
- tests/support/factories/user_factory.js
|
44
|
+
- tests/support/models/project.js
|
45
|
+
- tests/support/models/user.js
|
46
|
+
- tests/support/test_helper.js
|
47
|
+
- tests/test_setup.js
|
48
|
+
- vendor/assets/javascripts/ember_data_factory_guy.js
|
49
|
+
- vendor/assets/javascripts/factory_guy_has_many.js
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://rubygems.org/gems/ember-data-factory-guy
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.3.6
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project: ember-data-factory-guy
|
72
|
+
rubygems_version: 1.6.2
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Create Fixtures for Ember Data
|
76
|
+
test_files: []
|