kea-rails 1.0.9 → 2.0.0.pre.alpha1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4da6393851b76167dd2176aa44c4b8c5ac95324
4
- data.tar.gz: d6403e529a02f942d584612fbeb0e3bc56b2edad
3
+ metadata.gz: 3d318d6587c37b80be0e5b445d30e98b24823cd4
4
+ data.tar.gz: 5f299d6ec9ddd4773ea906cb33e0ea9a48bbab29
5
5
  SHA512:
6
- metadata.gz: 6eac8d1bc587254405cc834826d17536ebb7b309f59da7f2b28bd214f80cb045a6982d2109867b12b8b7d0ea74683eef54ba8615260eb134cb23de0470ad5206
7
- data.tar.gz: 3945382663310e144f494ef575cea287d1782a99dc2f10f94a46c3e38d8d003a9257a7f6ecf3978cb9ac073ae0bf68292fd8cbff42525f4ef5f9638108e57660
6
+ metadata.gz: c9ebfc93950958bd732d2aa3ed4293fc6b54a1e8eeae4efd1a1e9248383ab8855c7e51eae0617a2f437051b33f71e543293d4f7fc8ecc11ab3a769fba7f70acb
7
+ data.tar.gz: 8cf68356acd861629aeb45ee5cfa2d9ea3ef14223b4145c45cf5343c5ba8f9aada74de13b32e885e31a3f265bfa66818f4561e7225ae2602f248a4097ef0d921
@@ -2,15 +2,134 @@
2
2
  "use strict";
3
3
 
4
4
  var Base,
5
+ Serializable,
5
6
  Validatable;
6
7
 
7
8
  Base = function Base() {
8
-
9
+
10
+ this.id = null;
11
+ this.resource_path = null;
12
+
13
+ this.persisted = ko.observable(false);
14
+
15
+ this._attributeNames = [];
16
+ this._deserializableAttributes = [];
17
+ this._serializableAttributes = [];
18
+ this._deserializableAssociations = {};
19
+ this._serializableAssociations = {};
20
+ this._deserializableHasManyAssociations = {};
21
+ this._serializableHasManyAssociations = {};
22
+
23
+ this.deserializationOptions = { attributes: {} };
24
+ this.serializationOptions = { attributes: {} };
9
25
  };
10
26
 
11
27
  Base.prototype._modelName = function _modelName() {
12
28
  console.error("Model does not implement _modelName");
13
29
  };
30
+
31
+ Base.prototype.addAttribute = function addAttribute(name, options) {
32
+ options = options || {};
33
+ options.array = typeof options.array !== 'undefined' ? options.array : false;
34
+ options.deserialize = typeof options.deserialize !== 'undefined' ? options.deserialize : true;
35
+ options.serialize = typeof options.serialize !== 'undefined' ? options.serialize : true;
36
+
37
+ if (options.array) {
38
+ this[name] = ko.observableArray([]);
39
+ } else {
40
+ this[name] = ko.observable(options.value);
41
+ }
42
+
43
+ if (options.deserialize) {
44
+ this._deserializableAttributes.push(name);
45
+
46
+ if (typeof options !== 'boolean') {
47
+ this.deserializationOptions.attributes[name] = options.deserialize;
48
+ }
49
+ }
50
+
51
+ if (options.serialize) {
52
+ this._serializableAttributes.push(name);
53
+
54
+ if (typeof options !== 'boolean') {
55
+ this.serializationOptions.attributes[name] = options.serialize;
56
+ }
57
+ }
58
+ };
59
+
60
+ Base.prototype.unserializableAttributes = function unserializableAttributes(attributeNames) {
61
+ attributeNames.forEach(function(name) {
62
+ this.addAttribute(name, {serialize: false});
63
+ }, this);
64
+ };
65
+
66
+ Base.prototype.serializableAttributes = function unserializableAttributes(attributeNames) {
67
+ attributeNames.forEach(function(name) {
68
+ this.addAttribute(name);
69
+ }, this);
70
+ };
71
+
72
+ Base.prototype.addAssociation = function addAssociation(modelName, attributeName, type, options) {
73
+ var deserializationKey,
74
+ serializationKey;
75
+
76
+ options = options || {};
77
+ options.deserialize = typeof options.deserialize !== 'undefined' ? options.deserialize : true;
78
+ options.serialize = typeof options.serialize !== 'undefined' ? options.serialize : true;
79
+
80
+ if (type === 'hasOne') {
81
+ this[attributeName] = ko.observable();
82
+ deserializationKey = '_deserializableAssociations';
83
+ serializationKey = '_serializableAssociations';
84
+
85
+ } else if (type === 'hasMany') {
86
+ this[attributeName] = ko.observableArray([]);
87
+ deserializationKey = '_deserializableHasManyAssociations';
88
+ serializationKey = '_serializableHasManyAssociations';
89
+
90
+ } else {
91
+ console.error('unsupported association type %s', type);
92
+ return;
93
+ }
94
+
95
+ if (options.deserialize) {
96
+ this[deserializationKey][attributeName] = modelName;
97
+
98
+ if (typeof options !== 'boolean') {
99
+ this.deserializationOptions.attributes[name] = options.deserialize;
100
+ }
101
+ }
102
+
103
+ if (options.serialize) {
104
+ this[serializationKey][attributeName] = modelName;
105
+
106
+ if (typeof options !== 'boolean') {
107
+ this.serializationOptions.attributes[name] = options.serialize;
108
+ }
109
+ }
110
+ };
111
+
112
+ Base.prototype.hasOne = function hasOne(modelName, attributeName, options) {
113
+ if (typeof attributeName === 'undefined') {
114
+ attributeName = modelName.toLowerCase();
115
+ }
116
+
117
+ this.addAssociation(modelName, attributeName, 'hasOne', options);
118
+ };
119
+
120
+ Base.prototype.hasMany = function hasMany(modelName, attributeName, options) {
121
+ this.addAssociation(modelName, attributeName, 'hasMany', options);
122
+ };
123
+
124
+ Base.prototype.forEachAssocation = function forEachAssocation(type, callback) {
125
+ var key = '_' + type + 'Associations';
126
+
127
+ for (var attributeName in this[key]) {
128
+ if ( this[key].hasOwnProperty(attributeName) ) {
129
+ callback.call(this, attributeName, this[key][attributeName]);
130
+ }
131
+ }
132
+ };
14
133
 
15
134
  Base.prototype.service = function service() {
16
135
  if (DEBUG) {
@@ -20,14 +139,6 @@
20
139
  return app.services[ this._modelName() ];
21
140
  };
22
141
 
23
- Base.prototype.fromJSON = function fromJSON(json) {
24
- console.error("object does not implement fromJSON: %O", this);
25
- };
26
-
27
- Base.prototype.serialize = function serialize() {
28
- console.error("object does not implement serialize: %O", this);
29
- };
30
-
31
142
  Base.prototype.update = function update(params) {
32
143
  var that = this,
33
144
  deferred;
@@ -35,7 +146,7 @@
35
146
  deferred = this.service()
36
147
  .update(that, params)
37
148
  .done(function(data) {
38
- that.fromJSON(data);
149
+ that.deserialize(data);
39
150
  });
40
151
 
41
152
  return deferred;
@@ -52,7 +163,125 @@
52
163
 
53
164
  });
54
165
  };
166
+
167
+ Base.prototype.deserialize = function deserialize(data) {
168
+ this.persisted(true);
169
+
170
+ this.id = data.id;
171
+ this.resource_path = data.resource_path;
172
+
173
+ this._deserializableAttributes.forEach(function(name) {
174
+ this[name]( data[name] );
175
+ }, this);
176
+
177
+ this.forEachAssocation('deserializable', function(attributeName, modelName) {
178
+ if ( data[attributeName] ) {
179
+ this[attributeName]( new app.models[modelName](data[attributeName]) );
180
+ }
181
+ });
182
+
183
+ this.forEachAssocation('deserializableHasMany', function(attributeName, modelName) {
184
+ if ( data[attributeName] ) {
185
+ this[attributeName].removeAll();
186
+
187
+ data[attributeName].forEach(function(associationData) {
188
+ this[attributeName].push( new app.models[modelName](associationData) );
189
+ }, this);
190
+ }
191
+ });
192
+
193
+ if (typeof this.fromJS === 'function') {
194
+ this.fromJS(data);
195
+ }
196
+ };
197
+
198
+ Base.prototype.serialize = function serialize(options) {
199
+ var result = {},
200
+ defaultOptions,
201
+ attributeList,
202
+ attributeOptions,
203
+ isBlank,
204
+ shouldIncludeAttribute;
205
+
206
+ defaultOptions = {
207
+ includeId: false,
208
+ attributes: {}
209
+ };
210
+
211
+ options = $.extend({}, defaultOptions, this.serializationOptions, options);
212
+
213
+ attributeOptions = function attributeOptions(attributeName) {
214
+ return options.attributes[attributeName] || {};
215
+ };
216
+
217
+ isBlank = function isBlank(value) {
218
+ return typeof value === 'undefined' || value === null || value === '' || (Array.isArray(value) && value.length === 0 );
219
+ };
220
+
221
+ shouldIncludeAttribute = function shouldIncludeAttribute(name, value) {
222
+ var skipBlank = typeof attributeOptions(name).skipBlank !== 'undefined' ? attributeOptions(name).skipBlank : options.skipBlank;
223
+
224
+ if ( this._serializeableAttributes.indexOf(name) === -1) {
225
+ return false;
226
+ } else if (skipBlank && isBlank(value)) {
227
+ return false;
228
+ } else {
229
+ return true;
230
+ }
231
+ };
232
+
233
+ if (options.includeId) {
234
+ result.id = this.id;
235
+ }
236
+
237
+ if (this._destroy) {
238
+ result._destroy = true;
239
+ }
240
+
241
+ this._serializeableAttributes.forEach(function(name) {
242
+ if (typeof this.associations[name] !== 'undefined' || typeof this.hasManyAssociations[name] !== 'undefined') {
243
+ return;
244
+ }
245
+
246
+ var value = this[name]();
247
+
248
+ if (shouldIncludeAttribute(name, value)) {
249
+ result[name] = value;
250
+ }
251
+ }, this);
252
+
253
+ this.forEachAssocation('serializable', function(attributeName, modelName) {
254
+ var value,
255
+ key;
256
+
257
+ if (attributeOptions(attributeName).idOnly) {
258
+ value = this[attributeName]() ? this[attributeName]().id : null;
259
+ key = attributeName + '_id';
260
+ } else {
261
+ value = this[attributeName]().serialize();
262
+ key = attributeName;
263
+ }
55
264
 
265
+ if (shouldIncludeAttribute(attributeName, value)) {
266
+ result[key] = value;
267
+ }
268
+ });
269
+
270
+ this.forEachHasManyAssocation('serializableHasMany', function(attributeName, modelName) {
271
+ var value = this[attributeName]().map(function(association) { return association.serialize(); } );
272
+
273
+ if (shouldIncludeAttribute(attributeName, value)) {
274
+ result[attributeName + '_attributes'] = value;
275
+ }
276
+ });
277
+
278
+ if (typeof this.toJS === 'function') {
279
+ result = this.toJS(result);
280
+ }
281
+
282
+ return result;
283
+ };
284
+
56
285
  kea.models.Base = Base;
57
286
 
58
287
  Validatable = function Validatable() {
@@ -84,7 +313,7 @@
84
313
  this.forEachValidatableField = function forEachValidatableField(callback) {
85
314
  ko.utils.arrayForEach(this.validatableFields(), function(field) {
86
315
  if (that[field].isValidatable) {
87
- callback.call(that, that[field]);
316
+ callback.call(that, that[field], field);
88
317
  }
89
318
  });
90
319
  };
@@ -96,7 +325,7 @@
96
325
  var association = that[association_name]();
97
326
 
98
327
  if (association.isValidatable && association.isValidatable()) {
99
- callback.call(that, association);
328
+ callback.call(that, association, association_name);
100
329
  }
101
330
  });
102
331
 
@@ -136,15 +365,15 @@
136
365
  this.validationMessages = ko.computed(function validationMessages() {
137
366
  var messages = [];
138
367
 
139
- that.forEachValidatableField(function(observable) {
368
+ that.forEachValidatableField(function(observable, fieldName) {
140
369
  if (observable.hasError()) {
141
- messages.push(observable.validationMessage());
370
+ messages.push(fieldName + ': ' + observable.validationMessage());
142
371
  }
143
372
  }, this);
144
373
 
145
- that.forEachValidatableAssociation(function(association) {
374
+ that.forEachValidatableAssociation(function(association, name) {
146
375
  if (association.hasErrors()) {
147
- messages = messages.concat(association.validationMessages());
376
+ messages = name + ': ' + messages.concat(association.validationMessages());
148
377
  }
149
378
  });
150
379
 
@@ -133,11 +133,11 @@
133
133
 
134
134
  } else {
135
135
 
136
- if (typeof modelObject.refreshFromJSON === 'function') {
136
+ if (typeof modelObject.refreshFromJS === 'function') {
137
137
  modelObject.refresh(data);
138
138
 
139
139
  } else {
140
- modelObject.fromJSON(data);
140
+ modelObject.deserialize(data);
141
141
  }
142
142
 
143
143
  return modelObject;
@@ -1,3 +1,3 @@
1
1
  module Kea
2
- VERSION = "1.0.9"
2
+ VERSION = "2.0.0-alpha1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kea-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 2.0.0.pre.alpha1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan-Christian Foeh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-13 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4'
19
+ version: '4.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4'
26
+ version: '4.1'
27
27
  description: A collection of helpers for structuring Knockout.js applications
28
28
  email:
29
29
  - jan@programmanstalt.de
@@ -226,12 +226,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
226
226
  version: '0'
227
227
  required_rubygems_version: !ruby/object:Gem::Requirement
228
228
  requirements:
229
- - - ">="
229
+ - - ">"
230
230
  - !ruby/object:Gem::Version
231
- version: '0'
231
+ version: 1.3.1
232
232
  requirements: []
233
233
  rubyforge_project:
234
- rubygems_version: 2.4.6
234
+ rubygems_version: 2.4.5
235
235
  signing_key:
236
236
  specification_version: 4
237
237
  summary: A collection of helpers for structuring Knockout.js applications