ende 0.2.9 → 0.2.10

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.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/lib/assets/javascripts/aura/extensions/devise.js.coffee +2 -2
  3. data/lib/assets/javascripts/widgets/viewer/main.js.coffee +1 -1
  4. data/lib/ende/version.rb +1 -1
  5. data/vendor/assets/components/build.js +638 -133
  6. data/vendor/components/indefinido-indemma/.gitignore +14 -0
  7. data/vendor/components/indefinido-indemma/.gitignore~ +2 -0
  8. data/vendor/components/indefinido-indemma/.ruby-gemset +1 -0
  9. data/vendor/components/indefinido-indemma/.ruby-version +1 -0
  10. data/vendor/components/indefinido-indemma/Gemfile +13 -0
  11. data/vendor/components/indefinido-indemma/Guardfile +39 -0
  12. data/vendor/components/indefinido-indemma/History.md +0 -0
  13. data/vendor/components/indefinido-indemma/Readme.md +447 -0
  14. data/vendor/components/indefinido-indemma/build/development.js +340 -0
  15. data/vendor/components/indefinido-indemma/build/release.js +22039 -0
  16. data/vendor/components/indefinido-indemma/build/test.js +22039 -0
  17. data/vendor/components/indefinido-indemma/component.json +7 -3
  18. data/vendor/components/indefinido-indemma/components/chaijs-assertion-error/component.json +18 -0
  19. data/vendor/components/indefinido-indemma/components/chaijs-assertion-error/index.js +110 -0
  20. data/vendor/components/indefinido-indemma/components/chaijs-chai/component.json +47 -0
  21. data/vendor/components/indefinido-indemma/components/chaijs-chai/index.js +1 -0
  22. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/assertion.js +130 -0
  23. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/core/assertions.js +1270 -0
  24. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/interface/assert.js +1080 -0
  25. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/interface/expect.js +12 -0
  26. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/interface/should.js +76 -0
  27. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/addChainableMethod.js +94 -0
  28. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/addMethod.js +37 -0
  29. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/addProperty.js +40 -0
  30. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/eql.js +129 -0
  31. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/flag.js +32 -0
  32. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getActual.js +19 -0
  33. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getEnumerableProperties.js +25 -0
  34. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getMessage.js +49 -0
  35. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getName.js +20 -0
  36. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getPathValue.js +102 -0
  37. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/getProperties.js +35 -0
  38. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/index.js +108 -0
  39. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/inspect.js +320 -0
  40. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/objDisplay.js +48 -0
  41. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/overwriteMethod.js +51 -0
  42. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/overwriteProperty.js +54 -0
  43. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/test.js +26 -0
  44. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/transferFlags.js +44 -0
  45. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai/utils/type.js +45 -0
  46. data/vendor/components/indefinido-indemma/components/chaijs-chai/lib/chai.js +80 -0
  47. data/vendor/components/indefinido-indemma/components/component-bind/component.json +14 -0
  48. data/vendor/components/indefinido-indemma/components/component-bind/index.js +24 -0
  49. data/vendor/components/indefinido-indemma/components/component-jquery/component.json +14 -0
  50. data/vendor/components/indefinido-indemma/components/component-jquery/index.js +9601 -0
  51. data/vendor/components/indefinido-indemma/components/component-type/component.json +18 -0
  52. data/vendor/components/indefinido-indemma/components/component-type/index.js +32 -0
  53. data/vendor/components/indefinido-indemma/components/indefinido-advisable/component.json +21 -0
  54. data/vendor/components/indefinido-indemma/components/indefinido-advisable/index.js +1 -0
  55. data/vendor/components/indefinido-indemma/components/indefinido-advisable/lib/advisable.js +60 -0
  56. data/vendor/components/indefinido-indemma/components/indefinido-observable/component.json +25 -0
  57. data/vendor/components/indefinido-indemma/components/indefinido-observable/components/cjohansen-sinon/sinon.js +4290 -0
  58. data/vendor/components/indefinido-indemma/components/indefinido-observable/index.js +1 -0
  59. data/vendor/components/indefinido-indemma/components/indefinido-observable/lib/adapters/rivets.js +26 -0
  60. data/vendor/components/indefinido-indemma/components/indefinido-observable/lib/observable.js +323 -0
  61. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/accessors-legacy.js +92 -0
  62. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/accessors.js +173 -0
  63. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/array.indexOf.js +8 -0
  64. data/vendor/components/indefinido-indemma/components/indefinido-observable/vendor/shims/object.create.js +77 -0
  65. data/vendor/components/indefinido-indemma/components/kapit-observe-utils/component.json +13 -0
  66. data/vendor/components/indefinido-indemma/components/paulmillr-es6-shim/component.json +17 -0
  67. data/vendor/components/indefinido-indemma/components/paulmillr-es6-shim/es6-shim.js +996 -0
  68. data/vendor/components/indefinido-indemma/components/pluma-assimilate/component.json +25 -0
  69. data/vendor/components/indefinido-indemma/components/pluma-assimilate/dist/assimilate.js +127 -0
  70. data/vendor/components/indefinido-indemma/karma.conf.js +86 -0
  71. data/vendor/components/indefinido-indemma/lib/record/associable.js +229 -82
  72. data/vendor/components/indefinido-indemma/lib/record/errors.js +1 -0
  73. data/vendor/components/indefinido-indemma/lib/record/persistable.js +32 -0
  74. data/vendor/components/indefinido-indemma/lib/record/queryable.js +32 -0
  75. data/vendor/components/indefinido-indemma/lib/record/resource.js +12 -4
  76. data/vendor/components/indefinido-indemma/lib/record/rest.js +1 -1
  77. data/vendor/components/indefinido-indemma/lib/record/restfulable.js +38 -27
  78. data/vendor/components/indefinido-indemma/lib/record/scopable.js +15 -2
  79. data/vendor/components/indefinido-indemma/lib/record/storable.js +48 -0
  80. data/vendor/components/indefinido-indemma/lib/record/validatable.js +10 -5
  81. data/vendor/components/indefinido-indemma/lib/record/validations/cpf.js +1 -1
  82. data/vendor/components/indefinido-indemma/lib/record.js +15 -12
  83. data/vendor/components/indefinido-indemma/spec/record/associable_spec.js +137 -0
  84. data/vendor/components/indefinido-indemma/spec/record/persistable_spec.js +36 -0
  85. data/vendor/components/indefinido-indemma/spec/record/queryable_spec.js +33 -0
  86. data/vendor/components/indefinido-indemma/spec/record/resource_spec.js +93 -0
  87. data/vendor/components/indefinido-indemma/spec/record/rest_spec.js +32 -0
  88. data/vendor/components/indefinido-indemma/spec/record/restfulable_spec.js +288 -0
  89. data/vendor/components/indefinido-indemma/spec/record/scopable_spec.js +212 -0
  90. data/vendor/components/indefinido-indemma/spec/record/storable_spec.js +53 -0
  91. data/vendor/components/indefinido-indemma/spec/record/translationable.js +28 -0
  92. data/vendor/components/indefinido-indemma/spec/record/validatable_spec.js +111 -0
  93. data/vendor/components/indefinido-indemma/spec/record/validations/associated_spec.js +43 -0
  94. data/vendor/components/indefinido-indemma/spec/record/validations/confirmation_spec.js +36 -0
  95. data/vendor/components/indefinido-indemma/spec/record/validations/cpf_spec.js +35 -0
  96. data/vendor/components/indefinido-indemma/spec/record/validations/presence_spec.js +28 -0
  97. data/vendor/components/indefinido-indemma/spec/record/validations/remote_spec.js +87 -0
  98. data/vendor/components/indefinido-indemma/spec/record/validations/type_spec.js +48 -0
  99. data/vendor/components/indefinido-indemma/spec/record_spec.js +37 -0
  100. data/vendor/components/indefinido-indemma/spec/spec_helper.js +11 -0
  101. data/vendor/components/indefinido-indemma/spec/support/value_objects/phone.js +45 -0
  102. data/vendor/components/indefinido-indemma/src/lib/extensions/rivets.coffee +17 -0
  103. data/vendor/components/indefinido-indemma/src/lib/record/associable.coffee +342 -0
  104. data/vendor/components/indefinido-indemma/src/lib/record/errors.coffee +20 -0
  105. data/vendor/components/indefinido-indemma/src/lib/record/maid.coffee +16 -0
  106. data/vendor/components/indefinido-indemma/src/lib/record/persistable.coffee +27 -0
  107. data/vendor/components/indefinido-indemma/src/lib/record/queryable.coffee +29 -0
  108. data/vendor/components/indefinido-indemma/src/lib/record/resource.coffee +106 -0
  109. data/vendor/components/indefinido-indemma/src/lib/record/rest.coffee +28 -0
  110. data/vendor/components/indefinido-indemma/src/lib/record/restfulable.coffee +348 -0
  111. data/vendor/components/indefinido-indemma/src/lib/record/scopable.coffee +275 -0
  112. data/vendor/components/indefinido-indemma/src/lib/record/storable.coffee +46 -0
  113. data/vendor/components/indefinido-indemma/src/lib/record/translationable.coffee +18 -0
  114. data/vendor/components/indefinido-indemma/src/lib/record/validatable.coffee +217 -0
  115. data/vendor/components/indefinido-indemma/src/lib/record/validations/associated.coffee +32 -0
  116. data/vendor/components/indefinido-indemma/src/lib/record/validations/confirmation.coffee +19 -0
  117. data/vendor/components/indefinido-indemma/src/lib/record/validations/cpf.coffee +58 -0
  118. data/vendor/components/indefinido-indemma/src/lib/record/validations/presence.coffee +19 -0
  119. data/vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee +65 -0
  120. data/vendor/components/indefinido-indemma/src/lib/record/validations/type.coffee +32 -0
  121. data/vendor/components/indefinido-indemma/src/lib/record.coffee +136 -0
  122. data/vendor/components/indefinido-indemma/src/spec/record/associable_spec.coffee +130 -0
  123. data/vendor/components/indefinido-indemma/src/spec/record/persistable_spec.coffee +30 -0
  124. data/vendor/components/indefinido-indemma/src/spec/record/queryable_spec.coffee +27 -0
  125. data/vendor/components/indefinido-indemma/src/spec/record/resource_spec.coffee +69 -0
  126. data/vendor/components/indefinido-indemma/src/spec/record/rest_spec.coffee +22 -0
  127. data/vendor/components/indefinido-indemma/src/spec/record/restfulable_spec.coffee +207 -0
  128. data/vendor/components/indefinido-indemma/src/spec/record/scopable_spec.coffee +191 -0
  129. data/vendor/components/indefinido-indemma/src/spec/record/storable_spec.coffee +40 -0
  130. data/vendor/components/indefinido-indemma/src/spec/record/translationable.coffee +19 -0
  131. data/vendor/components/indefinido-indemma/src/spec/record/validatable_spec.coffee +100 -0
  132. data/vendor/components/indefinido-indemma/src/spec/record/validations/associated_spec.coffee +35 -0
  133. data/vendor/components/indefinido-indemma/src/spec/record/validations/confirmation_spec.coffee +25 -0
  134. data/vendor/components/indefinido-indemma/src/spec/record/validations/cpf_spec.coffee +28 -0
  135. data/vendor/components/indefinido-indemma/src/spec/record/validations/presence_spec.coffee +24 -0
  136. data/vendor/components/indefinido-indemma/src/spec/record/validations/remote_spec.coffee +74 -0
  137. data/vendor/components/indefinido-indemma/src/spec/record/validations/type_spec.coffee +33 -0
  138. data/vendor/components/indefinido-indemma/src/spec/record_spec.coffee +23 -0
  139. data/vendor/components/indefinido-indemma/src/spec/spec_helper.coffee +9 -0
  140. data/vendor/components/indefinido-indemma/src/spec/support/value_objects/phone.coffee +30 -0
  141. data/vendor/components/indefinido-indemma/vendor/object/mixin.js +196 -0
  142. data/vendor/components/indefinido-indemma/vendor/owl/pluralize.js +190 -0
  143. metadata +130 -2
@@ -0,0 +1,217 @@
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
+ # TODO use http://validatejs.org for internal handling
16
+
17
+ messages =
18
+ blank: (attribute_name) ->
19
+ attribute_name = @human_attribute_name attribute_name
20
+ "O campo #{attribute_name} não pode ficar em branco."
21
+
22
+ cpf: (attribute_name) ->
23
+ attribute_name = @human_attribute_name attribute_name
24
+ "O campo #{attribute_name} não está válido."
25
+
26
+ confirmation: (attribute_name) ->
27
+ confirmation_attribute_name = @human_attribute_name attribute_name
28
+ attribute_name = @human_attribute_name attribute_name.replace '_confirmation', ''
29
+ "O campo #{attribute_name} não está de acordo com o campo #{confirmation_attribute_name}."
30
+
31
+ associated: (attribute_name) ->
32
+ attribute_name = @human_attribute_name attribute_name
33
+ "O registro associado #{attribute_name} não é válido."
34
+
35
+ server: (attribute_name, options) ->
36
+ # TODO Better checking of base attribute case, better yet remove
37
+ # checking from here, just like active record
38
+ if attribute_name == 'base'
39
+ options.server_message
40
+ else
41
+ attribute_name = @human_attribute_name attribute_name
42
+ "#{attribute_name} #{options.server_message}."
43
+
44
+ type: (attribute_name, options) ->
45
+ attribute_name = @human_attribute_name attribute_name
46
+ "O campo #{attribute_name} não está válido."
47
+
48
+ # TODO move to errorsable.coffee
49
+ errorsable = stampit
50
+ add: (attribute_name, message_key, options) ->
51
+
52
+ @push [attribute_name, message_key, options]
53
+ # TODO Also push to attribute named
54
+ # @[attribute_name].push message_key, message, options
55
+
56
+ @messages[attribute_name] = ''
57
+
58
+ translator = messages[message_key]
59
+
60
+ if translator?
61
+ @messages[attribute_name] += translator.call @model, attribute_name, options
62
+ else
63
+ @messages[attribute_name] += message_key
64
+
65
+ clear: ->
66
+ if @length
67
+ @length = 0
68
+ @messages[attribute_name] = null for attribute_name of @messages
69
+
70
+ push: Array.prototype.push
71
+ splice: Array.prototype.splice
72
+ indexOf: Array.prototype.indexOf
73
+ ,
74
+ model: null
75
+ messages: null
76
+ length: 0
77
+ , ->
78
+ @messages = {}
79
+ @
80
+
81
+
82
+ initializers =
83
+ define_triggers: ->
84
+
85
+ # TODO remove the extra inheritance level of model[@resource]
86
+ @errors = errorsable model: model[@resource]
87
+
88
+ # TODO only add after save when resourceable is included
89
+ # TODO @after_initialize validate_field_types
90
+ # TODO only execute save operation if record is valid
91
+ @before 'save', -> @validate() if @save
92
+
93
+ # TODO move this functionality control to validatorable
94
+ @validated = false
95
+ @subscribe 'dirty', (value) -> @validated = false
96
+
97
+ Object.defineProperty @, 'valid',
98
+ get: ->
99
+
100
+ @validate()
101
+
102
+ if @validation.state() == 'resolved'
103
+ !@errors.length
104
+ else
105
+ null
106
+
107
+ set: -> throw new TypeError "You can't set the value for the valid property."
108
+ enumerable: false
109
+
110
+ create_validators: (definitions) ->
111
+
112
+ @validators = []
113
+
114
+ for name, validator of manager.validators
115
+ definition = definitions[validator.definition_key]
116
+
117
+ if definition
118
+ definition = [definition] unless type(definition) == 'array'
119
+
120
+ for validator_options in definition
121
+
122
+ # Parse validation definition
123
+ validator_options = attribute_name: validator_options unless type(validator_options) == 'object'
124
+ validator_options.model = @
125
+
126
+ # Instantiate validator
127
+ @validators.push validator validator_options
128
+
129
+ # Clear out definition to prevent validators from becoming
130
+ # attributes
131
+ delete definitions[validator.definition_key]
132
+
133
+ # Model and Record extensions
134
+ # TODO Use stampit!
135
+ extensions =
136
+ model:
137
+ validators: null
138
+
139
+ record:
140
+
141
+ validate_attribute: (attribute, doned, failed) ->
142
+ # TODO better clearing of single attribute error message
143
+ @errors.messages[attribute] = null
144
+
145
+ results = [@, attribute]
146
+
147
+ # TODO copy validators reference from model object to record object
148
+ # TODO update json serializer
149
+ # TODO filter validators for attribute
150
+ for validator in model[@resource.toString()].validators
151
+ if validator.attribute_name is attribute
152
+ results.push validator.validate_each @, validator.attribute_name, @[validator.attribute_name]
153
+
154
+ validation = jQuery.when.apply jQuery, results
155
+ validation.done doned
156
+ validation.fail failed
157
+
158
+ validation
159
+
160
+ validate: (doned, failed) ->
161
+ return @validation if @validated and not @dirty
162
+
163
+ @errors.clear()
164
+ results = [@]
165
+
166
+ # TODO copy validators reference from model object to record object
167
+ # TODO update json serializer
168
+ for validator in model[@resource.toString()].validators
169
+ results.push validator.validate_each @, validator.attribute_name, @[validator.attribute_name]
170
+
171
+ @validation = jQuery.when results...
172
+ @validation.done doned
173
+ @validation.fail failed
174
+
175
+ # TODO store this callback
176
+ @validation.then (record) -> record.validated = true
177
+
178
+ @validation
179
+
180
+
181
+ # Validators management
182
+ manager =
183
+
184
+ validators: {}
185
+
186
+ # TODO async validator loading
187
+ # for: (name) ->
188
+ # builder = @validators[name] ||= require "validations/#{name}"
189
+
190
+
191
+ validatable = stampit
192
+ validate: -> throw new Error 'Composed factory must override the validate method'
193
+ validate_each: -> throw new Error 'Composed factory must override the validate each method'
194
+
195
+
196
+ # TODO better stampit integration
197
+ model.mix (modelable) ->
198
+ jQuery.extend modelable, extensions.model
199
+
200
+ jQuery.extend modelable.record, extensions.record
201
+
202
+ modelable.after_mix.unshift initializers.create_validators
203
+ modelable.record.after_initialize.push initializers.define_triggers
204
+
205
+ model.validators = manager.validators
206
+
207
+ # Globalize definitions
208
+ root.validatable = validatable
209
+ root.manager = manager
210
+
211
+ # TODO async validator loading
212
+ require './validations/confirmation'
213
+ require './validations/associated'
214
+ require './validations/presence'
215
+ require './validations/remote'
216
+ require './validations/type'
217
+ 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,136 @@
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
+ # TODO usar deferred
17
+ before_initialize: []
18
+ all: ->
19
+ # TODO transform model in a array like object and store cache in root
20
+ @cache
21
+ # TODO better find support
22
+ create: (params...) ->
23
+ # Implementat non restful model creation
24
+ throw 'model.create not implemented yet, try using the restful.model.create method'
25
+ # find: (id) ->
26
+ # @where id: id, true
27
+ where: (conditions, first = false) ->
28
+ results = []
29
+ conditions.id = [conditions.id] if type(conditions.id) != 'array'
30
+ # TODO transform model in a array like object and store cache in root
31
+ for record in @cache when conditions.id.indexOf(record._id) isnt -1
32
+ if first
33
+ return record
34
+ else
35
+ results.push record
36
+
37
+ if first then null else results
38
+
39
+ initialize_record = (data = {resource: @resource, parent_resource: @parent_resource}) ->
40
+ data.resource ||= @resource
41
+ data.parent_resource ||= @resource.parent || @parent_resource
42
+ data.route ||= @route
43
+ data.nested_attributes = @nested_attributes || []
44
+
45
+ # instance = record.call extend data, @record # TODO remove @record from outside scop
46
+ after_initialize = (data.after_initialize || []).concat(@record.after_initialize)
47
+
48
+ creation = extend Object.create(data), @record, creation, after_initialize: after_initialize
49
+
50
+ # TODO use deferred instead of before_initialize array
51
+ for callback, index in @record.before_initialize
52
+ callback.call @, creation
53
+
54
+ instance = record.call creation # TODO remove @record from outside scope
55
+
56
+
57
+ # Call and remove used callbacks
58
+ # TODO use deferred instead of after_initialize array
59
+ for callback, index in instance.after_initialize
60
+ callback.call instance, instance
61
+
62
+ delete instance.after_initialize
63
+
64
+ instance
65
+
66
+
67
+ # Create model
68
+ mixer = (options) ->
69
+ throw 'Model mixin called incorrectly call with model.call {} instead of model({})' if @ == window
70
+ mixer.stale = true unless mixer.stale # Prevent model changes
71
+
72
+ # TODO Use stampit and solve this mess!!
73
+ if @record and @record.after_initialize
74
+ after_initialize = @record.after_initialize.splice 0
75
+ else
76
+ after_initialize = []
77
+
78
+ instance = bind @, initialize_record
79
+
80
+ extend instance, merge @, modelable
81
+
82
+ @record = instance.record = merge {}, instance.record, modelable.record
83
+ @record.after_initialize = instance.record.after_initialize = instance.record.after_initialize.concat after_initialize
84
+
85
+ @record.before_initialize = instance.record.before_initialize.concat []
86
+
87
+ callback.call instance, instance for callback in modelable.after_mix
88
+
89
+ # Store model for later use
90
+
91
+ # TODO implement correctly stampit usage, and remove the need for
92
+ # direct storage
93
+ mixer[@resource.name || @resource.toString()] = instance
94
+
95
+ mixer.mix = (blender) ->
96
+ throw "Trying to change model mixin with #{object} but model already used.\nCheck your configuration order" if @stale
97
+
98
+ blender modelable
99
+
100
+ # window.model
101
+ mixer
102
+
103
+ @record = do -> # mixin
104
+
105
+ callbacks =
106
+ # TODO search for a existing word and rename method, 'smudge' perhaps?
107
+ dirtify: ->
108
+ # TODO add suport to subscribe to any property
109
+ # @subscribe (prop, value, old) ->
110
+ # if prop isnt 'dirty' and not @dirty and value isnt old
111
+ # console.groupCollapsed "◉ Property '#{prop}' dirtied a #{@resource}"
112
+ # console.log old, "→", value
113
+ # console.log @
114
+ # console.groupEnd()
115
+ # @dirty = true
116
+
117
+ recordable =
118
+ # TODO usar deferred
119
+ dirty: false
120
+ after_initialize: [ callbacks.dirtify ]
121
+
122
+ that = (data) ->
123
+ throw "Mixin called incorrectly, call mixin with call method: record.call(object, data)" if @ == window
124
+
125
+ data ||= {}
126
+ after_initialize = (@after_initialize || []).concat(data.after_initialize || []).concat(recordable.after_initialize)
127
+ advisable observable(extend(@, recordable, data, after_initialize: after_initialize))
128
+
129
+
130
+ that.mix = (blender) ->
131
+ blender recordable
132
+
133
+ that
134
+
135
+ exports.record = @record
136
+ exports.model = @model