kea-rails 2.0.0.pre.alpha1 → 2.0.0.pre.alpha2

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: 3d318d6587c37b80be0e5b445d30e98b24823cd4
4
- data.tar.gz: 5f299d6ec9ddd4773ea906cb33e0ea9a48bbab29
3
+ metadata.gz: 5b12b0bc6e3f3b5f8b20b62b358b442d693509fe
4
+ data.tar.gz: e9173287dc4924671a300b414d93d1fe496e1cd4
5
5
  SHA512:
6
- metadata.gz: c9ebfc93950958bd732d2aa3ed4293fc6b54a1e8eeae4efd1a1e9248383ab8855c7e51eae0617a2f437051b33f71e543293d4f7fc8ecc11ab3a769fba7f70acb
7
- data.tar.gz: 8cf68356acd861629aeb45ee5cfa2d9ea3ef14223b4145c45cf5343c5ba8f9aada74de13b32e885e31a3f265bfa66818f4561e7225ae2602f248a4097ef0d921
6
+ metadata.gz: 3ad4d41e85feb75d4d6b7b1450491260e659a29fd35b81fdc73d6c2f1de157b59c4747b70e45a35b94e0277e95da5a93c3be53a5098b8f156c414d080027ea8d
7
+ data.tar.gz: d3da1ac2dc35410a95b31ff8680dfd85834b549407443e1644660e86867a0879d3cd950cc415318560e1316b66e48bcb1a91968b146bc6a2518259841691232a
@@ -0,0 +1,31 @@
1
+ (function(ko, $, moment) {
2
+ "use strict";
3
+
4
+ ko.extenders.date = function(target, options) {
5
+
6
+ options = options || {
7
+ internal: 'YYYY-MM-DD',
8
+ external: 'YYYY-MM-DD'
9
+ };
10
+
11
+ options.external = options.external || options.internal;
12
+
13
+ target.date = ko.computed({
14
+ read: function() {
15
+ var m = moment(target(), options.internal);
16
+
17
+ return m && m.isValid() ? m.format(options.external) : null;
18
+ },
19
+ write: function(newValue) {
20
+ var m = moment(newValue, options.external),
21
+ formatted = m && m.isValid() ? m.format(options.internal) : newValue;
22
+
23
+ target(formatted);
24
+ }
25
+ }, this);
26
+
27
+ //return the original observable
28
+ return target;
29
+ };
30
+
31
+ })(ko, $, moment);
@@ -7,6 +7,7 @@
7
7
  var defaults = {
8
8
  message: "",
9
9
  validator: function(v) {},
10
+ skipBlank: false,
10
11
  dependencies: []
11
12
  };
12
13
 
@@ -14,9 +15,15 @@
14
15
 
15
16
  target.validate = function validate(newValue) {
16
17
  var validatableValue = typeof newValue === 'undefined' ? target() : newValue,
17
- validatorResponse = options.validator(validatableValue),
18
+ validatorResponse,
18
19
  validatorResponseHandler;
19
20
 
21
+ if (options.skipBlank && target.isBlank(validatableValue)) {
22
+ return;
23
+ }
24
+
25
+ validatorResponse = options.validator(validatableValue);
26
+
20
27
  validatorResponseHandler = function validatorResponseHandler(response) {
21
28
  var result, message;
22
29
 
@@ -5,16 +5,35 @@
5
5
  ko.utils.validatorBase(target);
6
6
 
7
7
  var defaults = {
8
- message: ""
8
+ message: "",
9
+ allowBlank: true,
10
+ format: 'YYYY-MM-DD',
11
+ min: null,
12
+ max: null
9
13
  };
10
14
 
11
15
  options = $.extend({}, defaults, options);
12
16
 
13
17
  target.validate = function validate(newValue) {
14
18
  var validatableValue = typeof newValue === 'undefined' ? target() : newValue,
15
- m = moment(validatableValue, ["DD MM", "DD MM YY", "DD MM YYYY"]);
19
+ m = moment(validatableValue, options.format);
16
20
 
17
- if ( m === null || !m.isValid() || m.isBefore(moment().startOf('day')) ) {
21
+ if (options.allowBlank && target.isBlank(validatableValue)) {
22
+ target.markValid();
23
+ return;
24
+ }
25
+
26
+ if ( m === null || !m.isValid()) {
27
+ target.markInvalid(options.message);
28
+ return false;
29
+ }
30
+
31
+ if (options.min && m.isBefore(options.min)) {
32
+ target.markInvalid(options.message);
33
+ return false;
34
+ }
35
+
36
+ if (options.max && m.isAfter(options.max)) {
18
37
  target.markInvalid(options.message);
19
38
  return false;
20
39
  }
@@ -33,6 +33,14 @@
33
33
  target.validate.apply(target, args);
34
34
  };
35
35
  }
36
+
37
+ if (typeof target.isBlank === 'undefined') {
38
+ target.isBlank = function isBlank(value) {
39
+ value = arguments.length === 0 ? target() : value;
40
+
41
+ return typeof value === 'undefined' || value === null || value === '';
42
+ };
43
+ }
36
44
 
37
45
  if (typeof target.markValid === 'undefined') {
38
46
  target.markValid = function markValid() {
@@ -196,7 +196,8 @@
196
196
  };
197
197
 
198
198
  Base.prototype.serialize = function serialize(options) {
199
- var result = {},
199
+ var that = this,
200
+ result = {},
200
201
  defaultOptions,
201
202
  attributeList,
202
203
  attributeOptions,
@@ -221,7 +222,7 @@
221
222
  shouldIncludeAttribute = function shouldIncludeAttribute(name, value) {
222
223
  var skipBlank = typeof attributeOptions(name).skipBlank !== 'undefined' ? attributeOptions(name).skipBlank : options.skipBlank;
223
224
 
224
- if ( this._serializeableAttributes.indexOf(name) === -1) {
225
+ if ( that._serializableAttributes.indexOf(name) === -1) {
225
226
  return false;
226
227
  } else if (skipBlank && isBlank(value)) {
227
228
  return false;
@@ -238,11 +239,7 @@
238
239
  result._destroy = true;
239
240
  }
240
241
 
241
- this._serializeableAttributes.forEach(function(name) {
242
- if (typeof this.associations[name] !== 'undefined' || typeof this.hasManyAssociations[name] !== 'undefined') {
243
- return;
244
- }
245
-
242
+ this._serializableAttributes.forEach(function(name) {
246
243
  var value = this[name]();
247
244
 
248
245
  if (shouldIncludeAttribute(name, value)) {
@@ -258,7 +255,7 @@
258
255
  value = this[attributeName]() ? this[attributeName]().id : null;
259
256
  key = attributeName + '_id';
260
257
  } else {
261
- value = this[attributeName]().serialize();
258
+ value = this[attributeName]() ? this[attributeName]().serialize() : null;
262
259
  key = attributeName;
263
260
  }
264
261
 
@@ -267,7 +264,7 @@
267
264
  }
268
265
  });
269
266
 
270
- this.forEachHasManyAssocation('serializableHasMany', function(attributeName, modelName) {
267
+ this.forEachAssocation('serializableHasMany', function(attributeName, modelName) {
271
268
  var value = this[attributeName]().map(function(association) { return association.serialize(); } );
272
269
 
273
270
  if (shouldIncludeAttribute(attributeName, value)) {
@@ -43,26 +43,33 @@ class Kea::ModelGenerator < Rails::Generators::NamedBase
43
43
  end
44
44
 
45
45
  def attribute_initializers
46
- max_string_length = 0
47
- attribute_initializers = []
48
- association_initializers = []
46
+ unserializable_attributes = []
47
+ serializable_attribute_strings = ""
48
+ attribute_initializers = []
49
49
 
50
- attribute_initializers = @model_attributes.collect do |attribute_name|
51
- ["this.#{attribute_name}", "ko.observable();"]
50
+ ['created_at', 'updated_at'].each do |attribute|
51
+ unserializable_attributes << attribute if @klass.attribute_names.include?(attribute)
52
52
  end
53
53
 
54
- association_initializers = @model_associations.collect do |assoc|
55
- if assoc.macro == :has_one
56
- ["this.#{assoc.name}", "ko.observable();"]
57
- elsif assoc.macro == :has_many
58
- ["this.#{assoc.name}", "ko.observableArray([]);"]
59
- end
54
+ attribute_initializers << "this.unserializable_attributes(#{unserializable_attributes.collect { |a| "'#{a}'"}.join(", ")});"
55
+
56
+ @model_attributes.in_groups_of(5, false) do |group|
57
+ serializable_attribute_strings << ' ' + group.collect { |attribute| "'#{attribute}'" }.join(', ') + ",\n"
60
58
  end
61
59
 
62
- initializers = attribute_initializers + association_initializers
60
+ serializable_attribute_strings.gsub!(/,\n\z/, "\n")
63
61
 
64
- max_string_length = initializers.map { |a| a.first.length }.max
62
+ attribute_initializers << "this.serializable_attributes(\n#{serializable_attribute_strings} );"
63
+
64
+ @model_associations.each do |assoc|
65
+ attribute_initializers << case assoc.macro
66
+ when :has_one
67
+ "this.hasOne('#{assoc.name.to_s.singularize.camelize}');"
68
+ when :has_many
69
+ "this.hasMany('#{assoc.name.to_s.singularize.camelize}', '#{assoc.name}');"
70
+ end
71
+ end
65
72
 
66
- initializers.map { |i| [i.first.ljust(max_string_length + 1), " = ", i.last].join('') }
73
+ attribute_initializers
67
74
  end
68
75
  end
@@ -4,20 +4,22 @@
4
4
  var model_name = '<%= name.camelize %>',
5
5
  ModelClass;
6
6
 
7
- ModelClass = function <%= name.camelize %>(json) {
7
+ ModelClass = function <%= name.camelize %>(data) {
8
+ kea.models.Base.call(this);
8
9
  <% if options[:validatable] -%>
9
10
  kea.models.Validatable.call(this);
10
11
  <% end -%>
11
12
 
12
13
  var that = this;
13
14
 
14
- this.id = null;
15
- this.resource_path = null;
16
-
17
15
  <% if @klass -%>
18
16
  <% @attribute_initializers.each do |attribute| -%>
19
17
  <%= attribute %>
20
18
  <% end -%>
19
+
20
+ // this.addAttribute('foo', { array: true, deserialize: false, serialize: { skipBlank: true } });
21
+ // this.addAssociation('Foo', 'foo', 'hasOne', { serialize: { idOnly: true } });
22
+ // this.serializationOptions = { includeId: true };
21
23
  <% end -%>
22
24
  <% if options[:validatable] && @validators.any? -%>
23
25
 
@@ -37,53 +39,8 @@
37
39
  };
38
40
  <% end -%>
39
41
 
40
- this.fromJSON = function fromJSON(json) {
41
- this.id = json.id;
42
- this.resource_path = json.resource_path;
43
-
44
- <% if @klass -%>
45
- <% @model_attributes.each do |attribute| -%>
46
- this.<%= attribute %>( json.<%= attribute %> );
47
- <% end -%>
48
- <% @model_associations.each do |assoc| -%>
49
- <% if assoc.macro == :has_one -%>
50
-
51
- if (json.<%= assoc.name %>) {
52
- this.<%= assoc.name %>( new app.models.<%= assoc.name.to_s.singularize.camelize %>(json.<%= assoc.name %>) );
53
- }
54
- <% elsif assoc.macro == :has_many -%>
55
-
56
- if (json.<%= assoc.name %>) {
57
- this.<%= assoc.name %>.removeAll();
58
-
59
- json.<%= assoc.name %>.forEach(function(<%= assoc.name.to_s.singularize %>) {
60
- that.<%= assoc.name %>.push( new app.models.<%= assoc.name.to_s.singularize.camelize %>(<%= assoc.name.to_s.singularize %>) );
61
- });
62
- }
63
- <% end -%>
64
- <% end -%>
65
- <% end -%>
66
- };
67
-
68
- this.serialize = function serialize() {
69
- return {
70
- <% if @klass -%>
71
- <% @model_attributes.each do |attribute| -%>
72
- <%= attribute %>: this.<%= attribute %>(),
73
- <% end -%>
74
- <% @model_associations.each do |assoc| -%>
75
- <% if assoc.macro == :has_one -%>
76
- <%= assoc.name %>: this.<%= assoc.name %>().serialize(),
77
- <% elsif assoc.macro == :has_many -%>
78
- <%= assoc.name %>: this.<%= assoc.name %>().map(function(<%= assoc.name.to_s.singularize %>) { <%= assoc.name.to_s.singularize %>.serialize(); }),
79
- <% end -%>
80
- <% end -%>
81
- <% end -%>
82
- };
83
- };
84
-
85
- if (json) {
86
- this.fromJSON(json);
42
+ if (data) {
43
+ this.deserialize(data);
87
44
  }
88
45
  };
89
46
 
@@ -1,3 +1,3 @@
1
1
  module Kea
2
- VERSION = "2.0.0-alpha1"
2
+ VERSION = "2.0.0-alpha2"
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: 2.0.0.pre.alpha1
4
+ version: 2.0.0.pre.alpha2
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-03-09 00:00:00.000000000 Z
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,6 +44,7 @@ files:
44
44
  - app/assets/javascripts/kea/bindings/validation_state.js
45
45
  - app/assets/javascripts/kea/bindings/wait_for_vm.js
46
46
  - app/assets/javascripts/kea/components/confirmation_button.js
47
+ - app/assets/javascripts/kea/extenders/date.js
47
48
  - app/assets/javascripts/kea/extenders/equals.js
48
49
  - app/assets/javascripts/kea/extenders/external_validator.js
49
50
  - app/assets/javascripts/kea/extenders/min_length.js