ember-resourceful-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWIwNzU4NTIxNmE5ZWRjZDIwYmZlYjY3NTIyMjZjMGE2NmEyZWY4Yw==
5
+ data.tar.gz: !binary |-
6
+ Y2Q1M2I3OTNiNWI2NDEyNjE3OWZmMmU5OTQyOTRmZjQ4OGEyMDU1Ng==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NGMyYjU0NzQ4NjAzZmY4M2UwYmZjMjJkZjQ2MzJmOTkzZWJkNTUyZTVmYjE5
10
+ YzcwMjIwZGI3OGE1NzA4Nzk2MTU4Y2RjYjU3OTAwNThiODA0YTMyZmE1MTNm
11
+ ZmNiZTU2MmM3MDZjNjI4MGNmNDE3NjEyMmQ1ZmZlNGY3NTNmOTk=
12
+ data.tar.gz: !binary |-
13
+ NzQ4OTVjYjM1NjVjZDBjOGFjNjRhMjE2YTAzYjBjY2IwNzI3ZTU0NWE5MjYy
14
+ NzVkMDhkODdhZGQ3Mjc3MzAzMmEwYjIwZjRmODY4OGQ5YzIwZDZhNDE0MDhl
15
+ ZjliNzA0M2RhOTJhZmU3NzE3N2UyZjc2M2ExZDU5NTY4YjhhOTk=
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dan Martens
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Ember::Resourceful::Rails
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ember-resourceful-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ember-resourceful-rails
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require "ember-resourceful-rails/version"
2
+
3
+ module EmberResourceful
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module EmberResourceful
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,399 @@
1
+ /*
2
+ * Ember Resourceful 0.0.1
3
+ *
4
+ * Copyright (c) 2012-2013 Dan Martens
5
+ * http://danmartens.com
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+ (function() {
28
+ window.Resourceful = {};
29
+
30
+ Resourceful.Resource = Ember.Object.extend({
31
+ resourceAdapter: null,
32
+ resourceProperties: null,
33
+ resourceUrl: null,
34
+ serializers: null,
35
+ deserializers: null,
36
+
37
+ isFetching: false,
38
+ isFetched: false,
39
+ isSaving: false,
40
+
41
+ init: function() {
42
+ var _this = this;
43
+
44
+ this.persistedProperties = {};
45
+ this.dirtyProperties = [];
46
+
47
+ this._super();
48
+
49
+ if (this.resourceProperties) {
50
+ this.resourceProperties.forEach(function(key) {
51
+ _this.addObserver(key, function() {
52
+ if (_this.get(key) !== _this.persistedProperties[key]) {
53
+ if (!_this.dirtyProperties.contains(key)) {
54
+ _this.dirtyProperties.pushObject(key);
55
+ }
56
+ } else {
57
+ _this.dirtyProperties.removeObject(key);
58
+ }
59
+ });
60
+ });
61
+ }
62
+ },
63
+
64
+ isNew: function() {
65
+ return this.get('id') === undefined;
66
+ }.property('id'),
67
+
68
+ isDirty: function() {
69
+ return this.get('dirtyProperties.length') !== 0;
70
+ }.property('dirtyProperties.length'),
71
+
72
+ serialize: function() {
73
+ var serialized, _this = this;
74
+
75
+ serialized = {};
76
+
77
+ this.resourceProperties.forEach(function(key) {
78
+ var _ref;
79
+ if ((_ref = _this.serializers) != null ? _ref[key] : void 0) {
80
+ serialized[key] = _this.serializers[key].call(this, _this.get(key));
81
+ } else {
82
+ serialized[key] = _this.get(key);
83
+ }
84
+ });
85
+
86
+ if (this.resourceName) {
87
+ var s = {}; s[this.resourceName] = serialized;
88
+ return s;
89
+ } else {
90
+ return serialized;
91
+ }
92
+ },
93
+
94
+ deserialize: function(json) {
95
+ var key, value, _ref;
96
+
97
+ Ember.beginPropertyChanges(this);
98
+
99
+ for (key in json) {
100
+ value = json[key];
101
+
102
+ if ((_ref = this.deserializers) != null ? _ref[key] : void 0) {
103
+ value = this.deserializers[key].call(this, value);
104
+ }
105
+
106
+ this.set(key, value);
107
+ }
108
+
109
+ Ember.endPropertyChanges(this);
110
+
111
+ this._updatePersistedProperties();
112
+
113
+ return this;
114
+ },
115
+
116
+ fetch: function(options) {
117
+ var _this = this;
118
+
119
+ this.set('isFetching', true);
120
+
121
+ if (!options) {
122
+ options = {};
123
+ }
124
+
125
+ if (!options.url) {
126
+ options.url = this._resourceUrl();
127
+ }
128
+
129
+ return this.resourceAdapter.request('read', options)
130
+ .done(function(data, textStatus, jqXHR) {
131
+ _this.deserialize(data);
132
+ _this._updatePersistedProperties();
133
+
134
+ _this.set('isFetching', false);
135
+ _this.set('isFetched', true);
136
+ });
137
+ },
138
+
139
+ save: function(options) {
140
+ var success, method, _this = this;
141
+
142
+ this.set('isSaving', true);
143
+
144
+ if (!options) {
145
+ options = {};
146
+ }
147
+
148
+ if (!options.url) {
149
+ options.url = this._resourceUrl();
150
+ }
151
+
152
+ if (!options.data) {
153
+ options.data = this.serialize();
154
+ }
155
+
156
+ method = this.get('isNew') ? 'create' : 'update';
157
+
158
+ return this.resourceAdapter.request(method, options)
159
+ .done(function(data, textStatus, jqXHR) {
160
+ _this.deserialize(data);
161
+ _this._updatePersistedProperties();
162
+
163
+ _this.set('isFetched', true);
164
+ _this.set('isSaving', false);
165
+ });
166
+ },
167
+
168
+ destroy: function(options) {
169
+ if (!options) {
170
+ options = {};
171
+ }
172
+
173
+ if (!options.url) {
174
+ options.url = this._resourceUrl();
175
+ }
176
+
177
+ return this.resourceAdapter.request('delete', options);
178
+ },
179
+
180
+ revert: function(key) {
181
+ this.set(key, this.persistedProperties[key]);
182
+ this.dirtyProperties.removeObject(key);
183
+ },
184
+
185
+ revertAll: function() {
186
+ var _this = this;
187
+
188
+ Ember.beginPropertyChanges(this);
189
+
190
+ this.dirtyProperties.forEach(function(key) {
191
+ _this.revert(key);
192
+ });
193
+
194
+ this.dirtyProperties.clear();
195
+
196
+ Ember.endPropertyChanges(this);
197
+ },
198
+
199
+ _updatePersistedProperties: function() {
200
+ if (Array.isArray(this.resourceProperties)) {
201
+ var persisted, _this = this;
202
+
203
+ persisted = {};
204
+
205
+ this.resourceProperties.forEach(function(key) {
206
+ persisted[key] = _this.get(key);
207
+ });
208
+
209
+ this.set('persistedProperties', persisted);
210
+
211
+ this.dirtyProperties.clear();
212
+ }
213
+ },
214
+
215
+ _resourceUrl: function() {
216
+ var url = this.resourceAdapter.namespace + this.constructor.resourceUrl;
217
+
218
+ if (!this.get('isNew')) {
219
+ url += '/' + this.get('id');
220
+ }
221
+
222
+ return url;
223
+ }
224
+ });
225
+
226
+ Resourceful.Resource.reopenClass({
227
+ find: function(id) {
228
+ if (this.resourceCollectionPath) {
229
+ return Ember.get(this.resourceCollectionPath).findById(id);
230
+ } else {
231
+ throw new Error('You cannot use `find()` without specifying a `resourceCollectionPath` on the Resource\'s prototype!');
232
+ }
233
+ },
234
+
235
+ all: function() {
236
+ var collection;
237
+
238
+ if (this.resourceCollectionPath) {
239
+ collection = Ember.get(this.resourceCollectionPath);
240
+
241
+ if (!collection.get('isFetched')) {
242
+ collection.fetchAll();
243
+ }
244
+
245
+ return collection.get('content');
246
+ } else {
247
+ throw new Error('You cannot use `all()` without specifying a `resourceCollectionPath` on the Resource\'s prototype!');
248
+ }
249
+ }
250
+ });
251
+
252
+ Resourceful.ResourceCollection = Ember.ArrayProxy.extend({
253
+ resourceClass: null,
254
+ resourceAdapter: null,
255
+
256
+ isFetching: false,
257
+ isFetched: false,
258
+
259
+ init: function() {
260
+ this._super();
261
+
262
+ if (!this.get('content')) {
263
+ this.set('content', []);
264
+ }
265
+ },
266
+
267
+ findById: function(id) {
268
+ var resource;
269
+
270
+ resource = this.findProperty('id', id);
271
+
272
+ if (!resource) {
273
+ resource = this.resourceClass.create({ id: id });
274
+
275
+ resource.fetch();
276
+
277
+ this.pushObject(resource);
278
+ }
279
+
280
+ return resource;
281
+ },
282
+
283
+ fetch: function(id, options) {
284
+ var resource, _this = this;
285
+
286
+ if (!options) {
287
+ options = {};
288
+ }
289
+
290
+ resource = this.resourceClass.create({ id: id });
291
+
292
+ return resource.fetch(options)
293
+ .done(function() {
294
+ _this.pushObject(resource);
295
+ });
296
+ },
297
+
298
+ fetchAll: function(options) {
299
+ var success,
300
+ _this = this;
301
+
302
+ this.set('isFetching', true);
303
+
304
+ if (!options) {
305
+ options = {};
306
+ }
307
+
308
+ if (!options.url) {
309
+ options.url = this._resourceUrl();
310
+ }
311
+
312
+ return this.resourceAdapter.request('read', options)
313
+ .done(function(data, textStatus, jqXHR) {
314
+ _this.content.clear();
315
+ _this.loadAll(data);
316
+ _this.set('isFetching', false);
317
+ _this.set('isFetched', true);
318
+ });
319
+ },
320
+
321
+ loadAll: function(json) {
322
+ var _this = this;
323
+
324
+ json.forEach(function(j) {
325
+ _this.load(j);
326
+ });
327
+ },
328
+
329
+ load: function(json) {
330
+ var resource;
331
+
332
+ resource = this.findProperty('id', json.id);
333
+
334
+ if (!resource) {
335
+ resource = this.resourceClass.create();
336
+ this.pushObject(resource);
337
+ }
338
+
339
+ resource.deserialize(json);
340
+
341
+ resource._updatePersistedProperties();
342
+ },
343
+
344
+ _resourceUrl: function() {
345
+ return this.resourceAdapter.namespace + this.resourceClass.resourceUrl;
346
+ }
347
+ });
348
+
349
+ Resourceful.ResourceAdapter = Ember.Object.extend({
350
+ namespace: '',
351
+
352
+ request: function(method, options) {
353
+ var crud, deferred, _this = this;
354
+
355
+ crud = {
356
+ 'create': 'POST',
357
+ 'update': 'PUT',
358
+ 'read': 'GET',
359
+ 'delete': 'DELETE'
360
+ };
361
+
362
+ deferred = $.Deferred();
363
+
364
+ if (!options) {
365
+ options = {};
366
+ }
367
+
368
+ options.success = function(data, textStatus, jqXHR) {
369
+ deferred.resolve(_this.prepareResponse(data), textStatus, jqXHR);
370
+ };
371
+
372
+ options.error = function(jqXHR, textStatus, errorThrown) {
373
+ deferred.reject(jqXHR, textStatus, errorThrown);
374
+ };
375
+
376
+ options = this.prepareRequest(jQuery.extend({
377
+ dataType: 'json',
378
+ type: crud[method]
379
+ }, options));
380
+
381
+ jqXHR = $.ajax(options);
382
+
383
+ ['abort'].forEach(function(method) {
384
+ deferred[method] = jqXHR[method];
385
+ });
386
+
387
+ return deferred;
388
+ },
389
+
390
+ prepareRequest: function(options) {
391
+ return options;
392
+ },
393
+
394
+ prepareResponse: function(json) {
395
+ return json;
396
+ }
397
+ });
398
+
399
+ }).call(this);
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ember-resourceful-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: Ember Resourceful Rails
56
+ email:
57
+ - martens.dan@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/ember-resourceful-rails/version.rb
63
+ - lib/ember-resourceful-rails.rb
64
+ - vendor/assets/javascripts/ember-resourceful.js
65
+ - LICENSE.txt
66
+ - README.md
67
+ homepage: ''
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.0.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Adds Ember Resourceful to the Rails Asset Pipeline
91
+ test_files: []