ende 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/component.json +1 -1
  3. data/lib/assets/javascripts/aura/extensions/devise.js.coffee +5 -2
  4. data/lib/assets/javascripts/aura/extensions/models.js.coffee.erb +4 -3
  5. data/lib/assets/javascripts/value_objects/phone.js.coffee +8 -2
  6. data/lib/assets/javascripts/widgets/viewer/main.js.coffee +18 -21
  7. data/lib/ende/version.rb +1 -1
  8. data/lib/ende.rb +2 -2
  9. data/vendor/assets/javascripts/ende/build.js +485 -4879
  10. data/vendor/components/component-querystring/component.json +18 -0
  11. data/vendor/components/component-querystring/index.js +49 -0
  12. data/vendor/components/component-trim/component.json +13 -0
  13. data/vendor/components/component-trim/index.js +17 -0
  14. data/vendor/components/indefinido-indemma/.gitignore +14 -0
  15. data/vendor/components/indefinido-indemma/.ruby-gemset +1 -0
  16. data/vendor/components/indefinido-indemma/.ruby-version +1 -0
  17. data/vendor/components/indefinido-indemma/Gemfile +13 -0
  18. data/vendor/components/indefinido-indemma/Guardfile +39 -0
  19. data/vendor/components/indefinido-indemma/History.md +0 -0
  20. data/vendor/components/indefinido-indemma/Readme.md +443 -0
  21. data/vendor/components/indefinido-indemma/build/development.js +331 -0
  22. data/vendor/components/indefinido-indemma/build/release.js +21693 -0
  23. data/vendor/components/indefinido-indemma/build/test.js +331 -0
  24. data/vendor/components/indefinido-indemma/component.json +8 -9
  25. data/vendor/components/indefinido-indemma/components/chaijs-assertion-error/component.json +18 -0
  26. data/vendor/components/indefinido-indemma/components/chaijs-assertion-error/index.js +110 -0
  27. data/vendor/components/indefinido-indemma/components/chaijs-chai/component.json +47 -0
  28. data/vendor/components/indefinido-indemma/components/chaijs-chai/index.js +1 -0
  29. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/assertion.js +130 -0
  30. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/core/assertions.js +1270 -0
  31. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/interface/assert.js +1080 -0
  32. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/interface/expect.js +12 -0
  33. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/interface/should.js +76 -0
  34. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/addChainableMethod.js +94 -0
  35. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/addMethod.js +37 -0
  36. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/addProperty.js +40 -0
  37. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/eql.js +129 -0
  38. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/flag.js +32 -0
  39. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getActual.js +19 -0
  40. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getEnumerableProperties.js +25 -0
  41. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getMessage.js +49 -0
  42. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getName.js +20 -0
  43. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getPathValue.js +102 -0
  44. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getProperties.js +35 -0
  45. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/index.js +108 -0
  46. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/inspect.js +320 -0
  47. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/objDisplay.js +48 -0
  48. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/overwriteMethod.js +51 -0
  49. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/overwriteProperty.js +54 -0
  50. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/test.js +26 -0
  51. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/transferFlags.js +44 -0
  52. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/type.js +45 -0
  53. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai.js +80 -0
  54. data/vendor/components/indefinido-indemma/components/component-bind/component.json +14 -0
  55. data/vendor/components/indefinido-indemma/components/component-bind/index.js +24 -0
  56. data/vendor/components/indefinido-indemma/components/component-jquery/component.json +14 -0
  57. data/vendor/components/indefinido-indemma/components/component-jquery/index.js +9601 -0
  58. data/vendor/components/indefinido-indemma/components/component-type/component.json +18 -0
  59. data/vendor/components/indefinido-indemma/components/component-type/index.js +32 -0
  60. data/vendor/components/indefinido-indemma/components/indefinido-advisable/component.json +21 -0
  61. data/vendor/components/indefinido-indemma/components/indefinido-advisable/index.js +1 -0
  62. data/vendor/components/indefinido-indemma/components/indefinido-advisable/lib/advisable.js +60 -0
  63. data/vendor/components/indefinido-indemma/components/indefinido-observable/component.json +25 -0
  64. data/vendor/components/indefinido-indemma/components/indefinido-observable/components/cjohansen-sinon/sinon.js +4290 -0
  65. data/vendor/components/indefinido-indemma/components/indefinido-observable/index.js +1 -0
  66. data/vendor/components/indefinido-indemma/components/indefinido-observable/lib/adapters/rivets.js +26 -0
  67. data/vendor/components/indefinido-indemma/components/indefinido-observable/lib/observable.js +323 -0
  68. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/accessors-legacy.js +92 -0
  69. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/accessors.js +173 -0
  70. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/array.indexOf.js +8 -0
  71. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/object.create.js +77 -0
  72. data/vendor/components/indefinido-indemma/components/kapit-observe-utils/component.json +13 -0
  73. data/vendor/components/indefinido-indemma/components/pluma-assimilate/component.json +25 -0
  74. data/vendor/components/indefinido-indemma/components/pluma-assimilate/dist/assimilate.js +87 -0
  75. data/vendor/components/indefinido-indemma/karma.conf.js +86 -0
  76. data/vendor/components/indefinido-indemma/lib/record/associable.js +6 -6
  77. data/vendor/components/indefinido-indemma/lib/record/errors.js +1 -0
  78. data/vendor/components/indefinido-indemma/lib/record/resource.js +14 -3
  79. data/vendor/components/indefinido-indemma/lib/record/rest.js +7 -4
  80. data/vendor/components/indefinido-indemma/lib/record/restfulable.js +45 -15
  81. data/vendor/components/indefinido-indemma/lib/record/scopable.js +13 -3
  82. data/vendor/components/indefinido-indemma/lib/record/validatable.js +20 -9
  83. data/vendor/components/indefinido-indemma/lib/record/validations/confirmation.js +1 -1
  84. data/vendor/components/indefinido-indemma/lib/record/validations/remote.js +6 -5
  85. data/vendor/components/indefinido-indemma/lib/record/validations/type.js +29 -0
  86. data/vendor/components/indefinido-indemma/lib/record.js +1 -11
  87. data/vendor/components/indefinido-indemma/spec/record/associable_spec.js +76 -0
  88. data/vendor/components/indefinido-indemma/spec/record/resource_spec.js +90 -0
  89. data/vendor/components/indefinido-indemma/spec/record/rest_spec.js +32 -0
  90. data/vendor/components/indefinido-indemma/spec/record/restfulable_spec.js +232 -0
  91. data/vendor/components/indefinido-indemma/spec/record/scopable_spec.js +201 -0
  92. data/vendor/components/indefinido-indemma/spec/record/translationable.js +28 -0
  93. data/vendor/components/indefinido-indemma/spec/record/validatable_spec.js +111 -0
  94. data/vendor/components/indefinido-indemma/spec/record/validations/associated_spec.js +43 -0
  95. data/vendor/components/indefinido-indemma/spec/record/validations/confirmation_spec.js +36 -0
  96. data/vendor/components/indefinido-indemma/spec/record/validations/cpf_spec.js +35 -0
  97. data/vendor/components/indefinido-indemma/spec/record/validations/presence_spec.js +28 -0
  98. data/vendor/components/indefinido-indemma/spec/record/validations/remote_spec.js +86 -0
  99. data/vendor/components/indefinido-indemma/spec/record/validations/type_spec.js +48 -0
  100. data/vendor/components/indefinido-indemma/spec/record_spec.js +37 -0
  101. data/vendor/components/indefinido-indemma/spec/spec_helper.js +11 -0
  102. data/vendor/components/indefinido-indemma/spec/support/value_objects/phone.js +45 -0
  103. data/vendor/components/indefinido-indemma/src/lib/extensions/rivets.coffee +17 -0
  104. data/vendor/components/indefinido-indemma/src/lib/record/associable.coffee +173 -0
  105. data/vendor/components/indefinido-indemma/src/lib/record/errors.coffee +20 -0
  106. data/vendor/components/indefinido-indemma/src/lib/record/maid.coffee +16 -0
  107. data/vendor/components/indefinido-indemma/src/lib/record/resource.coffee +103 -0
  108. data/vendor/components/indefinido-indemma/src/lib/record/rest.coffee +28 -0
  109. data/vendor/components/indefinido-indemma/src/lib/record/restfulable.coffee +314 -0
  110. data/vendor/components/indefinido-indemma/src/lib/record/scopable.coffee +266 -0
  111. data/vendor/components/indefinido-indemma/src/lib/record/translationable.coffee +18 -0
  112. data/vendor/components/indefinido-indemma/src/lib/record/validatable.coffee +209 -0
  113. data/vendor/components/indefinido-indemma/src/lib/record/validations/associated.coffee +32 -0
  114. data/vendor/components/indefinido-indemma/src/lib/record/validations/confirmation.coffee +19 -0
  115. data/vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee +58 -0
  116. data/vendor/components/indefinido-indemma/src/lib/record/validations/presence.coffee +19 -0
  117. data/vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee +65 -0
  118. data/vendor/components/indefinido-indemma/src/lib/record/validations/type.coffee +32 -0
  119. data/vendor/components/indefinido-indemma/src/lib/record.coffee +123 -0
  120. data/vendor/components/indefinido-indemma/src/spec/record/associable_spec.coffee +63 -0
  121. data/vendor/components/indefinido-indemma/src/spec/record/resource_spec.coffee +64 -0
  122. data/vendor/components/indefinido-indemma/src/spec/record/rest_spec.coffee +22 -0
  123. data/vendor/components/indefinido-indemma/src/spec/record/restfulable_spec.coffee +164 -0
  124. data/vendor/components/indefinido-indemma/src/spec/record/scopable_spec.coffee +181 -0
  125. data/vendor/components/indefinido-indemma/src/spec/record/translationable.coffee +19 -0
  126. data/vendor/components/indefinido-indemma/src/spec/record/validatable_spec.coffee +100 -0
  127. data/vendor/components/indefinido-indemma/src/spec/record/validations/associated_spec.coffee +35 -0
  128. data/vendor/components/indefinido-indemma/src/spec/record/validations/confirmation_spec.coffee +25 -0
  129. data/vendor/components/indefinido-indemma/src/spec/record/validations/cpf_spec.coffee +28 -0
  130. data/vendor/components/indefinido-indemma/src/spec/record/validations/presence_spec.coffee +24 -0
  131. data/vendor/components/indefinido-indemma/src/spec/record/validations/remote_spec.coffee +72 -0
  132. data/vendor/components/indefinido-indemma/src/spec/record/validations/type_spec.coffee +33 -0
  133. data/vendor/components/indefinido-indemma/src/spec/record_spec.coffee +23 -0
  134. data/vendor/components/indefinido-indemma/src/spec/spec_helper.coffee +9 -0
  135. data/vendor/components/indefinido-indemma/src/spec/support/value_objects/phone.coffee +30 -0
  136. data/vendor/components/indefinido-indemma/vendor/owl/pluralize.js +190 -0
  137. data/vendor/components/indefinido-observable/lib/observable.js +3 -0
  138. metadata +119 -2
@@ -0,0 +1,209 @@
1
+ require './translationable'
2
+
3
+ root = exports ? window
4
+ stampit = require '../../vendor/stampit'
5
+ observable = require('observable').mixin
6
+ type = require 'type'
7
+
8
+ # TODO better model require
9
+ # model = require ...
10
+
11
+ # TODO Add base settings to validation
12
+ # TODO implement method
13
+ # model[resource].validators_on 'field' # Get all validators related to this field
14
+
15
+ messages =
16
+ blank: (attribute_name) ->
17
+ attribute_name = @human_attribute_name attribute_name
18
+ "O campo #{attribute_name} não pode ficar em branco."
19
+
20
+ cpf: (attribute_name) ->
21
+ attribute_name = @human_attribute_name attribute_name
22
+ "O campo #{attribute_name} não está válido."
23
+
24
+ confirmation: (attribute_name) ->
25
+ confirmation_attribute_name = @human_attribute_name attribute_name
26
+ attribute_name = @human_attribute_name attribute_name.replace '_confirmation', ''
27
+ "O campo #{attribute_name} não está de acordo com o campo #{confirmation_attribute_name}."
28
+
29
+ associated: (attribute_name) ->
30
+ attribute_name = @human_attribute_name attribute_name
31
+ "O registro associado #{attribute_name} não é válido."
32
+
33
+ server: (attribute_name, options) ->
34
+ attribute_name = @human_attribute_name attribute_name
35
+ "#{attribute_name} #{options.server_message}."
36
+
37
+ type: (attribute_name, options) ->
38
+ attribute_name = @human_attribute_name attribute_name
39
+ "O campo #{attribute_name} não está válido."
40
+
41
+ # TODO move to errorsable.coffee
42
+ errorsable = stampit
43
+ add: (attribute_name, message_key, options) ->
44
+
45
+ @push [attribute_name, message_key, options]
46
+ # TODO Also push to attribute named
47
+ # @[attribute_name].push message_key, message, options
48
+
49
+ @messages[attribute_name] = ''
50
+
51
+ translator = messages[message_key]
52
+
53
+ if translator?
54
+ @messages[attribute_name] += translator.call @model, attribute_name, options
55
+ else
56
+ @messages[attribute_name] += message_key
57
+
58
+ clear: ->
59
+ if @length
60
+ @length = 0
61
+ @messages[attribute_name] = null for attribute_name of @messages
62
+
63
+ push: Array.prototype.push
64
+ splice: Array.prototype.splice
65
+ ,
66
+ model: null
67
+ messages: null
68
+ length: 0
69
+ , ->
70
+ @messages = {}
71
+ @
72
+
73
+
74
+ initializers =
75
+ define_triggers: ->
76
+
77
+ # TODO remove the extra inheritance level of model[@resource]
78
+ @errors = errorsable model: model[@resource]
79
+
80
+ # TODO only add after save when resourceable is included
81
+ # TODO @after_initialize validate_field_types
82
+ # TODO only execute save operation if record is valid
83
+ @before 'save', -> @validate() if @save
84
+
85
+ # TODO move this functionality control to validatorable
86
+ @validated = false
87
+ @subscribe 'dirty', -> @validated = false
88
+
89
+ Object.defineProperty @, 'valid',
90
+ get: ->
91
+
92
+ @validate()
93
+
94
+ if @validation.state() == 'resolved'
95
+ !@errors.length
96
+ else
97
+ null
98
+
99
+ set: -> throw new TypeError "You can't set the value for the valid property."
100
+ enumerable: false
101
+
102
+ create_validators: (definitions) ->
103
+
104
+ @validators = []
105
+
106
+ for name, validator of manager.validators
107
+ definition = definitions[validator.definition_key]
108
+
109
+ if definition
110
+ definition = [definition] unless type(definition) == 'array'
111
+
112
+ for validator_options in definition
113
+
114
+ # Parse validation definition
115
+ validator_options = attribute_name: validator_options unless type(validator_options) == 'object'
116
+ validator_options.model = @
117
+
118
+ # Instantiate validator
119
+ @validators.push validator validator_options
120
+
121
+ # Clear out definition to prevent validators from becoming
122
+ # attributes
123
+ delete definitions[validator.definition_key]
124
+
125
+ # Model and Record extensions
126
+ # TODO Use stampit!
127
+ extensions =
128
+ model:
129
+ validators: null
130
+
131
+ record:
132
+
133
+ validate_attribute: (attribute, doned, failed) ->
134
+ # TODO better clearing of single attribute error message
135
+ @errors.messages[attribute] = null
136
+
137
+ results = [@, attribute]
138
+
139
+ # TODO copy validators reference from model object to record object
140
+ # TODO update json serializer
141
+ # TODO filter validators for attribute
142
+ for validator in model[@resource.toString()].validators
143
+ if validator.attribute_name is attribute
144
+ results.push validator.validate_each @, validator.attribute_name, @[validator.attribute_name]
145
+
146
+ validation = jQuery.when.apply jQuery, results
147
+ validation.done doned
148
+ validation.fail failed
149
+
150
+ validation
151
+
152
+ validate: (doned, failed) ->
153
+ return @validation if @validated
154
+
155
+ @errors.clear()
156
+ results = [@]
157
+
158
+ # TODO copy validators reference from model object to record object
159
+ # TODO update json serializer
160
+ for validator in model[@resource.toString()].validators
161
+ results.push validator.validate_each @, validator.attribute_name, @[validator.attribute_name]
162
+
163
+ @validation = jQuery.when results...
164
+ @validation.done doned
165
+ @validation.fail failed
166
+
167
+ # TODO store this callback
168
+ @validation.then (record) -> record.validated = true
169
+
170
+ @validation
171
+
172
+
173
+ # Validators management
174
+ manager =
175
+
176
+ validators: {}
177
+
178
+ # TODO async validator loading
179
+ # for: (name) ->
180
+ # builder = @validators[name] ||= require "validations/#{name}"
181
+
182
+
183
+ validatable = stampit
184
+ validate: -> throw new Error 'Composed factory must override the validate method'
185
+ validate_each: -> throw new Error 'Composed factory must override the validate each method'
186
+
187
+
188
+ # TODO better stampit integration
189
+ model.mix (modelable) ->
190
+ jQuery.extend modelable, extensions.model
191
+
192
+ jQuery.extend modelable.record, extensions.record
193
+
194
+ modelable.after_mix.unshift initializers.create_validators
195
+ modelable.record.after_initialize.push initializers.define_triggers
196
+
197
+ model.validators = manager.validators
198
+
199
+ # Globalize definitions
200
+ root.validatable = validatable
201
+ root.manager = manager
202
+
203
+ # TODO async validator loading
204
+ require './validations/confirmation'
205
+ require './validations/associated'
206
+ require './validations/presence'
207
+ require './validations/remote'
208
+ require './validations/type'
209
+ require './validations/cpf'
@@ -0,0 +1,32 @@
1
+ # TODO Add base settings to validation
2
+ # model = window.model
3
+
4
+ # TODO implement method
5
+ # model[resource].validators_on 'field' # Get all validators related to this field
6
+
7
+ validations = require '../validatable'
8
+ stampit = require '../../../vendor/stampit'
9
+
10
+ associationable = stampit
11
+ validate_each: (record, attribute, value) ->
12
+ # TODO figure out why this method is being called twice
13
+ if record[attribute]
14
+
15
+ # TODO detect association type, and then validate
16
+ # current we only support has_one associations
17
+ unless model[record.resource].has_one.indexOf(attribute) != -1
18
+ throw new Error 'Only has_one associations are supported to validates_associated'
19
+
20
+ associated_validation = record[attribute].validate()
21
+
22
+ associated_validation.done ->
23
+ if record[attribute].errors.length
24
+ record.errors.add attribute, 'associated', @options
25
+
26
+ associated_validation
27
+
28
+
29
+ composed = stampit.compose(validations.validatable, associationable)
30
+ composed.definition_key = 'validates_associated'
31
+
32
+ validations.manager.validators.association = composed
@@ -0,0 +1,19 @@
1
+ # TODO Add base settings to validation
2
+ # model = window.model
3
+
4
+ # TODO implement method
5
+ # model[resource].validators_on 'field' # Get all validators related to this field
6
+
7
+ validations = require '../validatable'
8
+ stampit = require '../../../vendor/stampit'
9
+
10
+ confirmationable = stampit
11
+ validate_each: (record, attribute, value) ->
12
+ if record[attribute] != record["#{attribute}_confirmation"]
13
+ record.errors.add "#{attribute}_confirmation", 'confirmation', @options
14
+
15
+
16
+ composed = stampit.compose(validations.validatable, confirmationable)
17
+ composed.definition_key = 'validates_confirmation_of'
18
+
19
+ validations.manager.validators.confirmation = composed
@@ -0,0 +1,58 @@
1
+ # TODO Add base settings to validation
2
+ # model = window.model
3
+
4
+ # TODO implement method
5
+ # model[resource].validators_on 'field' # Get all validators related to this field
6
+
7
+ validations = require '../validatable'
8
+ stampit = require '../../../vendor/stampit'
9
+
10
+ cpfable = stampit
11
+ validate_format: (value) ->
12
+ value = value.replace /[\.\-]/g, ""
13
+
14
+ # Wrong cpf size
15
+ return false if value.length < 11
16
+
17
+ # Dummy but valid values (000.000.000-00, 111.111.111-11, ...)
18
+ return false if value.match /^(0+|1+|2+|3+|4+|5+|6+|7+|8+|9+)$/
19
+
20
+ # Mod 11 validation
21
+ c = value.substr 0, 9
22
+ dv = value.substr 9, 2
23
+ d1 = 0
24
+ v = false
25
+ i = 0
26
+
27
+ for i in [1..9]
28
+ d1 += c.charAt(i) * (10 - i)
29
+
30
+ return false if d1 == 0
31
+
32
+ d1 = 11 - (d1 % 11)
33
+ d1 = 0 if (d1 > 9 )
34
+
35
+ return false if +dv.charAt 0 != d1
36
+
37
+ d1 *= 2
38
+ for i in [1..9]
39
+ d1 += c.charAt(i) * (11 - i)
40
+
41
+
42
+ d1 = 11 - (d1 % 11)
43
+ d1 = 0 if (d1 > 9 )
44
+
45
+ return false if +dv.charAt(1) != d1
46
+
47
+ true
48
+
49
+
50
+ validate_each: (record, attribute, value) ->
51
+ record.errors.add attribute, 'cpf', @options if value and not @validate_format value
52
+
53
+
54
+
55
+
56
+ composed = stampit.compose validations.validatable, cpfable
57
+ composed.definition_key = 'validates_cpf_format'
58
+ validations.manager.validators.cpf = composed
@@ -0,0 +1,19 @@
1
+ # TODO Add base settings to validation
2
+ # model = window.model
3
+
4
+ # TODO implement method
5
+ # model[resource].validators_on 'field' # Get all validators related to this field
6
+
7
+ validations = require '../validatable'
8
+ stampit = require '../../../vendor/stampit'
9
+
10
+ presenceable = stampit
11
+ validate_each: (record, attribute, value) ->
12
+ if value == null or value == '' or value == undefined
13
+ record.errors.add attribute, 'blank', @options
14
+
15
+
16
+ composed = stampit.compose(validations.validatable, presenceable)
17
+ composed.definition_key = 'validates_presence_of'
18
+
19
+ validations.manager.validators.presence = composed
@@ -0,0 +1,65 @@
1
+ # TODO Add base settings to validation
2
+ # model = window.model
3
+
4
+ # TODO implement method
5
+ # model[resource].validators_on 'field' # Get all validators related to this field
6
+
7
+ root = exports ? window
8
+
9
+ validations = require '../validatable'
10
+ rest = require '../rest'
11
+ stampit = require '../../../vendor/stampit'
12
+
13
+ remoteable = stampit
14
+ validate_each: (record, attribute, value) ->
15
+ data = @json record
16
+
17
+ # TODO best partial application
18
+ # @post(data).done(_.partial @succeeded, record)
19
+ @post(data).done((json) => @succeeded(json, record))
20
+
21
+ json: (record) ->
22
+ param = @resource.param_name || @resource.toString()
23
+
24
+ data = {}
25
+ data[param] = record.json()
26
+
27
+ # TODO stop renaming id field, lol
28
+ data[param].id ||= data[param]._id
29
+ delete data[param]._id
30
+
31
+ data
32
+
33
+ # TODO Use rest.post
34
+ post: (data) ->
35
+
36
+ jQuery.ajax
37
+ url : @route
38
+ data : data
39
+ type : 'post'
40
+ dataType: 'json'
41
+ context : @
42
+
43
+ succeeded: (json, record) ->
44
+ error_messages = json[@attribute_name]
45
+
46
+ return unless error_messages
47
+
48
+ for error_message in error_messages
49
+ record.errors.add @attribute_name, 'server', server_message: error_message
50
+ ,
51
+ message: "Remote validation failed"
52
+ route: null
53
+ , ->
54
+ # TODO discover why @model.route is coming null!
55
+ # Desglobalize model constant
56
+ pluralized_resource = model.pluralize @model.resource.toString()
57
+ @resource = @model.resource
58
+ @route ||= "/#{pluralized_resource}/validate"
59
+ @
60
+
61
+
62
+ composed = stampit.compose(validations.validatable, remoteable)
63
+ composed.definition_key = 'validates_remotely'
64
+
65
+ validations.manager.validators.remote = composed
@@ -0,0 +1,32 @@
1
+ # TODO Add base settings to validation
2
+ # model = window.model
3
+
4
+ # TODO implement method
5
+ # model[resource].validators_on 'field' # Get all validators related to this field
6
+
7
+ validations = require '../validatable'
8
+ stampit = require '../../../vendor/stampit'
9
+
10
+ typeable = stampit
11
+ validate_each: (record, attribute, value) ->
12
+ # TODO store type on validator instantiation
13
+ @type ||= model[record.resource.toString()][attribute]
14
+
15
+ if value
16
+
17
+ if value instanceof @type
18
+
19
+ # TODO store type_name on costructor
20
+ @type_name ||= @type.name
21
+
22
+ unless value.valid # Will trigger validation
23
+ record.errors.add attribute, 'type', type_name: @type_name?
24
+
25
+ else
26
+ throw new Error "Invalid attribute value type! Found #{typeof value} expected #{@type.name}"
27
+
28
+
29
+ composed = stampit.compose(validations.validatable, typeable)
30
+ composed.definition_key = 'validates_type_of'
31
+
32
+ validations.manager.validators.type = composed
@@ -0,0 +1,123 @@
1
+ $ = require 'jquery'
2
+ type = require 'type'
3
+ bind = require 'bind'
4
+ observable = require('observable').mixin
5
+ advisable = require('advisable' ).mixin
6
+ extend = require 'assimilate'
7
+ merge = require('assimilate').withStrategy 'deep'
8
+
9
+ # TODO support other type of associations
10
+ @model = do -> # mixin
11
+ modelable =
12
+ after_mix: []
13
+ record:
14
+ # TODO usar deferred
15
+ after_initialize: []
16
+ all: ->
17
+ # TODO transform model in a array like object and store cache in root
18
+ @cache
19
+ # TODO better find support
20
+ create: (params...) ->
21
+ # Implementat non restful model creation
22
+ throw 'model.create not implemented yet, try using the restful.model.create method'
23
+ find: (id) ->
24
+ @where id: id, true
25
+ where: (conditions, first = false) ->
26
+ results = []
27
+ conditions.id = [conditions.id] if type(conditions.id) != 'array'
28
+ # TODO transform model in a array like object and store cache in root
29
+ for record in @cache when conditions.id.indexOf(record._id) isnt -1
30
+ if first
31
+ return record
32
+ else
33
+ results.push record
34
+
35
+ if first then null else results
36
+
37
+ initialize_record = (data = {resource: @resource, parent_resource: @parent_resource}) ->
38
+ data.resource ||= @resource
39
+ data.parent_resource ||= @resource.parent || @parent_resource
40
+ data.route ||= @route
41
+ data.nested_attributes = @nested_attributes || []
42
+
43
+
44
+ # instance = record.call extend data, @record # TODO remove @record from outside scop
45
+ after_initialize = (data.after_initialize || []).concat(@record.after_initialize)
46
+ instance = record.call extend Object.create(data), @record, after_initialize: after_initialize # TODO remove @record from outside scope
47
+
48
+ # Call and remove used callbacks
49
+ callback.call instance, instance for callback in instance.after_initialize
50
+ delete instance.after_initialize
51
+
52
+ instance
53
+
54
+
55
+ # Create model
56
+ mixer = (options) ->
57
+ throw 'Model mixin called incorrectly call with model.call {} instead of model({})' if @ == window
58
+ mixer.stale = true unless mixer.stale # Prevent model changes
59
+
60
+ # TODO Use stampit and solve this mess!!
61
+ if @record and @record.after_initialize
62
+ after_initialize = @record.after_initialize.splice 0
63
+ else
64
+ after_initialize = []
65
+
66
+ instance = bind @, initialize_record
67
+
68
+ extend instance, merge @, modelable
69
+
70
+ @record = instance.record = merge {}, instance.record, modelable.record
71
+ @record.after_initialize = instance.record.after_initialize = instance.record.after_initialize.concat after_initialize
72
+
73
+
74
+ callback.call instance, instance for callback in modelable.after_mix
75
+
76
+ # Store model for later use
77
+
78
+ # TODO implement correctly stampit usage, and remove the need for
79
+ # direct storage
80
+ mixer[@resource.name || @resource.toString()] = instance
81
+
82
+ mixer.mix = (blender) ->
83
+ throw "Trying to change model mixin with #{object} but model already used.\nCheck your configuration order" if @stale
84
+
85
+ blender modelable
86
+
87
+ # window.model
88
+ mixer
89
+
90
+ @record = do -> # mixin
91
+
92
+ callbacks =
93
+ # TODO search for a existing word and rename method, 'smudge' perhaps?
94
+ dirtify: ->
95
+ # TODO add suport to subscribe to any property
96
+ # @subscribe (prop, value, old) ->
97
+ # if prop isnt 'dirty' and not @dirty and value isnt old
98
+ # console.groupCollapsed "◉ Property '#{prop}' dirtied a #{@resource}"
99
+ # console.log old, "→", value
100
+ # console.log @
101
+ # console.groupEnd()
102
+ # @dirty = true
103
+
104
+ recordable =
105
+ # TODO usar deferred
106
+ dirty: false
107
+ after_initialize: [ callbacks.dirtify ]
108
+
109
+ that = (data) ->
110
+ throw "Mixin called incorrectly, call mixin with call method: record.call(object, data)" if @ == window
111
+
112
+ data ||= {}
113
+ after_initialize = (@after_initialize || []).concat(data.after_initialize || []).concat(recordable.after_initialize)
114
+ advisable observable(extend(@, recordable, data, after_initialize: after_initialize))
115
+
116
+
117
+ that.mix = (blender) ->
118
+ blender recordable
119
+
120
+ that
121
+
122
+ exports.record = @record
123
+ exports.model = @model
@@ -0,0 +1,63 @@
1
+ root = exports ? window
2
+
3
+ require 'indemma/lib/record/associable'
4
+
5
+ describe 'record', ->
6
+ record = root.record
7
+
8
+ it 'should create a record'
9
+
10
+ describe 'model', ->
11
+ model = root.model
12
+ person = corporation = null
13
+
14
+ beforeEach ->
15
+ corporation = model.call
16
+ resource: 'corporation'
17
+
18
+ # TODO implement support for self referential associations
19
+ friend = model.call
20
+ resource: 'friends'
21
+
22
+ person = model.call
23
+ resource : 'person'
24
+ has_many : 'friends'
25
+ belongs_to: 'corporation'
26
+
27
+ describe 'belongs_to', ->
28
+
29
+ it 'should add builded object to association named attribute', ->
30
+ arthur = person
31
+ name: 'Arthur Dent'
32
+
33
+ corporation = arthur.build_corporation()
34
+ arthur.should.have.property 'corporation'
35
+ expect(corporation).to.be.ok
36
+
37
+ describe 'has_many', ->
38
+
39
+ it 'should return a record factory with associations stored', ->
40
+ person.has_many.should.be.array
41
+
42
+ has_many = Array.prototype.splice.call person.has_many, 0
43
+
44
+ has_many.should.contain 'friends'
45
+
46
+ describe '#()', ->
47
+
48
+ it 'should return a record with an association object', ->
49
+ person().should.have.property 'friends'
50
+
51
+ describe '{generated_association}', ->
52
+ association = null
53
+
54
+ beforeEach -> association = person().friends
55
+
56
+ it 'should have query methods', ->
57
+ association.should.have.property 'all'
58
+ association.should.have.property 'each'
59
+ association.should.have.property 'reload'
60
+
61
+ describe '#all', ->
62
+
63
+ it 'should auto observe nested associations attributes'
@@ -0,0 +1,64 @@
1
+ require 'indemma/lib/record/restfulable'
2
+ require 'indemma/lib/record/resource'
3
+
4
+ root = exports ? window
5
+
6
+ model = root.model # TODO model = require 'indemma/model'
7
+ record = root.record # TODO model = require 'indemma/record'
8
+ jQuery = require 'component-jquery'
9
+
10
+ describe 'resource', ->
11
+
12
+ describe 'with scope option', ->
13
+ person = towel = null
14
+
15
+ beforeEach ->
16
+ person = model.call
17
+ resource: 'person'
18
+
19
+ towel = model.call
20
+ resource:
21
+ name: 'towel'
22
+ scope: 'users'
23
+
24
+
25
+ it 'should be prefixed with scope', ->
26
+ towel. route.should.be.eq '/users/towels'
27
+ towel( { } ).route.should.be.eq '/users/towels'
28
+ towel(id: 1).route.should.be.eq '/users/towels'
29
+
30
+
31
+ describe 'with singular resource', ->
32
+ towel = null
33
+
34
+ beforeEach ->
35
+ towel = model.call
36
+ resource:
37
+ name: 'towel'
38
+ singular: true
39
+
40
+ deferred = jQuery.Deferred()
41
+ deferred.resolveWith towel(name: 'Arthur'), [_id: 1]
42
+ sinon.stub(jQuery, "ajax").returns(deferred)
43
+ promises = towel.create {name: 'Arthur'}, {name: 'Ford'}
44
+
45
+ afterEach -> jQuery.ajax.restore()
46
+
47
+ it 'the route should be in singular form', ->
48
+ towel. route.should.be.eq '/towel'
49
+ towel( { } ).route.should.be.eq '/towel'
50
+ towel(id: 1).route.should.be.eq '/towel'
51
+
52
+
53
+ describe 'when included', ->
54
+ xit 'sets the resource loaded flag on model', ->
55
+ # model.resource.should.be.true
56
+
57
+ describe 'model' , ->
58
+ it 'add methods to model object'
59
+
60
+ describe '#pluralize', ->
61
+ xit 'transforms word into plural form'
62
+
63
+ describe '#singularize', ->
64
+ xit 'transforms word into singular form'