ende 0.5.20 → 0.5.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. data/component.json +1 -1
  3. data/components/indefinido/indemma/master/.gitignore +17 -0
  4. data/components/indefinido/indemma/master/.ruby-gemset +1 -0
  5. data/components/indefinido/indemma/master/.ruby-version +1 -0
  6. data/components/indefinido/indemma/master/Gemfile +12 -0
  7. data/components/indefinido/indemma/master/Guardfile +39 -0
  8. data/components/indefinido/indemma/master/History.md +0 -0
  9. data/components/indefinido/indemma/master/Readme.md +445 -0
  10. data/components/indefinido/indemma/master/build/development.js +26123 -0
  11. data/components/indefinido/indemma/master/build/release.js +16741 -0
  12. data/components/indefinido/indemma/master/build/test.js +26123 -0
  13. data/components/indefinido/indemma/master/component.json +1 -1
  14. data/components/indefinido/indemma/master/components/chaijs/assertion-error/1.0.0/component.json +18 -0
  15. data/components/indefinido/indemma/master/components/chaijs/assertion-error/1.0.0/index.js +110 -0
  16. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/.gitignore +22 -0
  17. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/.mailmap +1 -0
  18. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/.npmignore +14 -0
  19. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/.travis.yml +14 -0
  20. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/CONTRIBUTING.md +173 -0
  21. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/History.md +895 -0
  22. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/Makefile +93 -0
  23. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/README.md +99 -0
  24. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/ReleaseNotes.md +482 -0
  25. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/bower.json +27 -0
  26. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/chai.js +4782 -0
  27. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/component.json +50 -0
  28. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/index.js +1 -0
  29. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/karma.conf.js +28 -0
  30. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/karma.sauce.js +41 -0
  31. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/assertion.js +130 -0
  32. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/config.js +50 -0
  33. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/core/assertions.js +1314 -0
  34. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/interface/assert.js +1056 -0
  35. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/interface/expect.js +12 -0
  36. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/interface/should.js +78 -0
  37. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/addChainableMethod.js +111 -0
  38. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/addMethod.js +43 -0
  39. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/addProperty.js +40 -0
  40. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/flag.js +32 -0
  41. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/getActual.js +18 -0
  42. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/getEnumerableProperties.js +25 -0
  43. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/getMessage.js +49 -0
  44. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/getName.js +20 -0
  45. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/getPathValue.js +102 -0
  46. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/getProperties.js +35 -0
  47. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/index.js +114 -0
  48. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/inspect.js +320 -0
  49. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/objDisplay.js +49 -0
  50. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/overwriteChainableMethod.js +53 -0
  51. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/overwriteMethod.js +51 -0
  52. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/overwriteProperty.js +54 -0
  53. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/test.js +26 -0
  54. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/transferFlags.js +44 -0
  55. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai/utils/type.js +45 -0
  56. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/lib/chai.js +87 -0
  57. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/package.json +42 -0
  58. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/sauce.browsers.js +128 -0
  59. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/support/sauce/sauce_connect_block.sh +7 -0
  60. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/support/sauce/sauce_connect_setup.sh +53 -0
  61. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/assert.js +638 -0
  62. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/auth/.gitkeep +0 -0
  63. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/bootstrap/index.js +22 -0
  64. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/bootstrap/karma.js +22 -0
  65. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/configuration.js +133 -0
  66. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/display/errors.js +14 -0
  67. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/display/message.js +47 -0
  68. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/expect.js +814 -0
  69. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/globalShould.js +15 -0
  70. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/plugins.js +24 -0
  71. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/should.js +744 -0
  72. data/components/indefinido/indemma/master/components/chaijs/chai/1.9.1/test/utilities.js +309 -0
  73. data/components/indefinido/indemma/master/components/chaijs/deep-eql/0.1.3/component.json +20 -0
  74. data/components/indefinido/indemma/master/components/chaijs/deep-eql/0.1.3/lib/eql.js +257 -0
  75. data/components/indefinido/indemma/master/components/chaijs/type-detect/0.1.1/component.json +18 -0
  76. data/components/indefinido/indemma/master/components/chaijs/type-detect/0.1.1/lib/type.js +142 -0
  77. data/components/indefinido/indemma/master/components/component/bind/1.0.0/component.json +14 -0
  78. data/components/indefinido/indemma/master/components/component/bind/1.0.0/index.js +23 -0
  79. data/components/indefinido/indemma/master/components/component/jquery/1.0.0/component.json +10 -0
  80. data/components/indefinido/indemma/master/components/component/jquery/1.0.0/index.js +10308 -0
  81. data/components/indefinido/indemma/master/components/component/type/1.0.0/component.json +20 -0
  82. data/components/indefinido/indemma/master/components/component/type/1.0.0/index.js +32 -0
  83. data/components/indefinido/indemma/master/components/indefinido/advisable/master/component.json +21 -0
  84. data/components/indefinido/indemma/master/components/indefinido/advisable/master/index.js +1 -0
  85. data/components/indefinido/indemma/master/components/indefinido/advisable/master/lib/advisable.js +60 -0
  86. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/component.json +39 -0
  87. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/index.js +1 -0
  88. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/adapters/rivets.js +40 -0
  89. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/legacy/notifierable.js +145 -0
  90. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/legacy/schedulerable.js +147 -0
  91. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/lookup.js +38 -0
  92. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/observable/keypath_observer.js +38 -0
  93. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/observable/observation.js +45 -0
  94. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/observable/selection.js +57 -0
  95. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/observable/self_observer.js +38 -0
  96. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/observable.js +97 -0
  97. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/lib/platform.js +12 -0
  98. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/vendor/observe-js/observe.js +1631 -0
  99. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/vendor/shims/accessors-legacy.js +92 -0
  100. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/vendor/shims/accessors.js +248 -0
  101. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/vendor/shims/array.indexOf.js +8 -0
  102. data/components/indefinido/indemma/master/components/indefinido/observable/es6-modules/vendor/shims/object.create.js +77 -0
  103. data/components/indefinido/indemma/master/components/pluma/assimilate/0.4.0/component.json +25 -0
  104. data/components/indefinido/indemma/master/components/pluma/assimilate/0.4.0/dist/assimilate.js +127 -0
  105. data/components/indefinido/indemma/master/karma.conf.js +75 -0
  106. data/components/indefinido/indemma/master/lib/record/associable.js +4 -2
  107. data/components/indefinido/indemma/master/lib/record/dirtyable.js +1 -1
  108. data/components/indefinido/indemma/master/package.json +9 -0
  109. data/components/indefinido/indemma/master/spec/record/associable_spec.js +211 -0
  110. data/components/indefinido/indemma/master/spec/record/dirtyable_spec.js +57 -0
  111. data/components/indefinido/indemma/master/spec/record/maid_spec.js +40 -0
  112. data/components/indefinido/indemma/master/spec/record/persistable_spec.js +46 -0
  113. data/components/indefinido/indemma/master/spec/record/queryable_spec.js +39 -0
  114. data/components/indefinido/indemma/master/spec/record/resource_spec.js +93 -0
  115. data/components/indefinido/indemma/master/spec/record/rest_spec.js +32 -0
  116. data/components/indefinido/indemma/master/spec/record/restfulable_spec.js +346 -0
  117. data/components/indefinido/indemma/master/spec/record/scopable_spec.js +242 -0
  118. data/components/indefinido/indemma/master/spec/record/storable_spec.js +44 -0
  119. data/components/indefinido/indemma/master/spec/record/translationable.js +28 -0
  120. data/components/indefinido/indemma/master/spec/record/validatable_spec.js +142 -0
  121. data/components/indefinido/indemma/master/spec/record/validations/associated_spec.js +43 -0
  122. data/components/indefinido/indemma/master/spec/record/validations/confirmation_spec.js +36 -0
  123. data/components/indefinido/indemma/master/spec/record/validations/cpf_spec.js +34 -0
  124. data/components/indefinido/indemma/master/spec/record/validations/presence_spec.js +28 -0
  125. data/components/indefinido/indemma/master/spec/record/validations/remote_spec.js +87 -0
  126. data/components/indefinido/indemma/master/spec/record/validations/type_spec.js +48 -0
  127. data/components/indefinido/indemma/master/spec/record_spec.js +49 -0
  128. data/components/indefinido/indemma/master/spec/spec_helper.js +15 -0
  129. data/components/indefinido/indemma/master/spec/support/value_objects/phone.js +45 -0
  130. data/components/indefinido/indemma/master/src/lib/extensions/rivets.coffee +17 -0
  131. data/components/indefinido/indemma/master/src/lib/record/associable.coffee +419 -0
  132. data/components/indefinido/indemma/master/src/lib/record/dirtyable.coffee +47 -0
  133. data/components/indefinido/indemma/master/src/lib/record/errors.coffee +20 -0
  134. data/components/indefinido/indemma/master/src/lib/record/maid.coffee +16 -0
  135. data/components/indefinido/indemma/master/src/lib/record/persistable.coffee +34 -0
  136. data/components/indefinido/indemma/master/src/lib/record/queryable.coffee +29 -0
  137. data/components/indefinido/indemma/master/src/lib/record/resource.coffee +107 -0
  138. data/components/indefinido/indemma/master/src/lib/record/rest.coffee +34 -0
  139. data/components/indefinido/indemma/master/src/lib/record/restfulable.coffee +487 -0
  140. data/components/indefinido/indemma/master/src/lib/record/scopable.coffee +279 -0
  141. data/components/indefinido/indemma/master/src/lib/record/storable.coffee +49 -0
  142. data/components/indefinido/indemma/master/src/lib/record/translationable.coffee +18 -0
  143. data/components/indefinido/indemma/master/src/lib/record/validatable.coffee +241 -0
  144. data/components/indefinido/indemma/master/src/lib/record/validations/associated.coffee +34 -0
  145. data/components/indefinido/indemma/master/src/lib/record/validations/confirmation.coffee +17 -0
  146. data/components/indefinido/indemma/master/src/lib/record/validations/cpf.coffee +57 -0
  147. data/components/indefinido/indemma/master/src/lib/record/validations/presence.coffee +16 -0
  148. data/components/indefinido/indemma/master/src/lib/record/validations/remote.coffee +61 -0
  149. data/components/indefinido/indemma/master/src/lib/record/validations/type.coffee +31 -0
  150. data/components/indefinido/indemma/master/src/lib/record/validations/validatorable.coffee +5 -0
  151. data/components/indefinido/indemma/master/src/lib/record.coffee +143 -0
  152. data/components/indefinido/indemma/master/src/spec/record/associable_spec.coffee +205 -0
  153. data/components/indefinido/indemma/master/src/spec/record/dirtyable_spec.coffee +47 -0
  154. data/components/indefinido/indemma/master/src/spec/record/maid_spec.coffee +34 -0
  155. data/components/indefinido/indemma/master/src/spec/record/persistable_spec.coffee +39 -0
  156. data/components/indefinido/indemma/master/src/spec/record/queryable_spec.coffee +33 -0
  157. data/components/indefinido/indemma/master/src/spec/record/resource_spec.coffee +69 -0
  158. data/components/indefinido/indemma/master/src/spec/record/rest_spec.coffee +22 -0
  159. data/components/indefinido/indemma/master/src/spec/record/restfulable_spec.coffee +257 -0
  160. data/components/indefinido/indemma/master/src/spec/record/scopable_spec.coffee +227 -0
  161. data/components/indefinido/indemma/master/src/spec/record/storable_spec.coffee +39 -0
  162. data/components/indefinido/indemma/master/src/spec/record/translationable.coffee +19 -0
  163. data/components/indefinido/indemma/master/src/spec/record/validatable_spec.coffee +127 -0
  164. data/components/indefinido/indemma/master/src/spec/record/validations/associated_spec.coffee +35 -0
  165. data/components/indefinido/indemma/master/src/spec/record/validations/confirmation_spec.coffee +25 -0
  166. data/components/indefinido/indemma/master/src/spec/record/validations/cpf_spec.coffee +27 -0
  167. data/components/indefinido/indemma/master/src/spec/record/validations/presence_spec.coffee +24 -0
  168. data/components/indefinido/indemma/master/src/spec/record/validations/remote_spec.coffee +74 -0
  169. data/components/indefinido/indemma/master/src/spec/record/validations/type_spec.coffee +33 -0
  170. data/components/indefinido/indemma/master/src/spec/record_spec.coffee +29 -0
  171. data/components/indefinido/indemma/master/src/spec/spec_helper.coffee +11 -0
  172. data/components/indefinido/indemma/master/src/spec/support/value_objects/phone.coffee +30 -0
  173. data/components/indefinido/indemma/master/vendor/object/mixin.js +196 -0
  174. data/components/indefinido/indemma/master/vendor/spec/sinon/assert.js +199 -0
  175. data/components/indefinido/indemma/master/vendor/spec/sinon/behavior.js +334 -0
  176. data/components/indefinido/indemma/master/vendor/spec/sinon/call.js +205 -0
  177. data/components/indefinido/indemma/master/vendor/spec/sinon/collection.js +155 -0
  178. data/components/indefinido/indemma/master/vendor/spec/sinon/match.js +245 -0
  179. data/components/indefinido/indemma/master/vendor/spec/sinon/mock.js +451 -0
  180. data/components/indefinido/indemma/master/vendor/spec/sinon/sandbox.js +144 -0
  181. data/components/indefinido/indemma/master/vendor/spec/sinon/spy.js +413 -0
  182. data/components/indefinido/indemma/master/vendor/spec/sinon/stub.js +161 -0
  183. data/components/indefinido/indemma/master/vendor/spec/sinon/test.js +77 -0
  184. data/components/indefinido/indemma/master/vendor/spec/sinon/test_case.js +99 -0
  185. data/components/indefinido/indemma/master/vendor/spec/sinon/util/event.js +94 -0
  186. data/components/indefinido/indemma/master/vendor/spec/sinon/util/fake_server.js +212 -0
  187. data/components/indefinido/indemma/master/vendor/spec/sinon/util/fake_server_with_clock.js +83 -0
  188. data/components/indefinido/indemma/master/vendor/spec/sinon/util/fake_timers.js +405 -0
  189. data/components/indefinido/indemma/master/vendor/spec/sinon/util/fake_xdomain_request.js +206 -0
  190. data/components/indefinido/indemma/master/vendor/spec/sinon/util/fake_xml_http_request.js +575 -0
  191. data/components/indefinido/indemma/master/vendor/spec/sinon/util/timers_ie.js +31 -0
  192. data/components/indefinido/indemma/master/vendor/spec/sinon/util/xdr_ie.js +14 -0
  193. data/components/indefinido/indemma/master/vendor/spec/sinon/util/xhr_ie.js +19 -0
  194. data/components/indefinido/indemma/master/vendor/spec/sinon.js +383 -0
  195. data/lib/assets/javascripts/value_objects/phone.js.coffee +3 -1
  196. data/lib/assets/javascripts/widgets/viewer/plugins/scopable.js.coffee +5 -5
  197. data/lib/ende/version.rb +1 -1
  198. data/vendor/assets/components/ende_build.css +4 -4
  199. data/vendor/assets/components/ende_build.js +7974 -7889
  200. metadata +191 -5
  201. data/vendor/assets/component/build.css +0 -112
  202. data/vendor/assets/components/build.css +0 -112
  203. data/vendor/assets/components/indemma_with_none.js +0 -30553
@@ -0,0 +1,34 @@
1
+ $ = require 'jquery'
2
+
3
+ module.exports =
4
+ # TODO render route in the object itself with getter, and normalize this methods
5
+ get : (data) -> request.call @, 'get' , (if @_id then "#{@route}/#{@_id}" else @route), data
6
+ put : (data) -> request.call @, 'put' , (if @_id then "#{@route}/#{@_id}" else @route), data # TODO change from put to patch
7
+ post : (data) -> request.call @, 'post' , @route, data
8
+ delete: (data) -> request.call @, 'delete', (if @_id then "#{@route}/#{@_id}" else @route), data
9
+
10
+
11
+ # TODO move to serialization module
12
+ data_for = (data) ->
13
+ param_name = @resource.param_name || @resource.toString()
14
+
15
+ # TODO optmize this serialization lookup
16
+ if not data and @json
17
+ data = {}
18
+ data[param_name] = @json()
19
+
20
+ if data and data[param_name]
21
+ delete data[param_name]['id']
22
+ delete data[param_name]['_id']
23
+
24
+ data
25
+
26
+ request = (method, url, data) ->
27
+ data = data_for.call @, data
28
+
29
+ $.ajax
30
+ url : url
31
+ data : data
32
+ type : method
33
+ dataType: 'json'
34
+ context : @
@@ -0,0 +1,487 @@
1
+ merge = require('assimilate').withStrategy 'deep'
2
+ type = require 'type'
3
+ observable = require('observable').mixin
4
+ $ = require 'jquery' # TODO remove jquery dependency and use simple promises implementation
5
+ rest = require './rest.js'
6
+ root = exports ? @
7
+
8
+ `import './dirtyable.js'`
9
+
10
+ util =
11
+ model:
12
+ map: (records) ->
13
+ @ record for record in records
14
+
15
+
16
+ restful =
17
+ model:
18
+ # returns an array of promises
19
+ create: (params..., callback) ->
20
+ throw new TypeError("No arguments provided for #{@resource}.create") unless arguments.length
21
+ unless typeof callback == 'function'
22
+ params.push callback
23
+ callback = undefined
24
+
25
+ params.unshift {} unless params.length
26
+
27
+ savings = []
28
+ for attributes in params
29
+ # TODO accept dirty as attribute on record creation
30
+ record = @ attributes
31
+ record.dirty = true
32
+ savings.push record.save callback
33
+
34
+ $.when savings...
35
+
36
+ # returns a promise
37
+ # TODO move to scopable
38
+ every: (conditions = {}, doned, failed) ->
39
+ if typeof conditions == 'function'
40
+ doned = conditions
41
+ conditions = {}
42
+
43
+ # TODO Consider parent resources
44
+ # if @parent and not @parent._id
45
+ # return callback.call @model, []
46
+
47
+ $.when(rest.get.call @, conditions)
48
+ .then(util.model.map )
49
+ .done(doned )
50
+ .fail failed
51
+
52
+ first: (conditions = {}, callback) ->
53
+ if typeof conditions == 'function'
54
+ callback = conditions
55
+ conditions = {}
56
+
57
+ namespaced = conditions[@resource] || {}
58
+ namespaced.limit = 1
59
+ namespaced.order = 'desc'
60
+
61
+ # TODO should fail when server returns more then one record
62
+ @every conditions, callback
63
+
64
+ # TODO better treating of arguments
65
+ get: (action, data = {}) ->
66
+ # TODO better way to override route
67
+ old_route = @route
68
+
69
+ # TODO extract route generation concern
70
+ default_route = '/'
71
+ default_route += @resource.scope + '/' if @resource.scope?
72
+ default_route += if @resource.singular then @resource.name else model.pluralize @resource.name
73
+
74
+ # TODO think why this code is here
75
+ @route = default_route unless default_route == @route
76
+
77
+ if action
78
+ # TODO Get own property descriptor, or better way do override route
79
+ Object.defineProperty @, 'route',
80
+ value: "#{default_route}/#{action}"
81
+ configurable: true
82
+
83
+ # TODO not allow resource overriding
84
+ resource = data.resource
85
+ data = data.json() if data and data.json
86
+
87
+ if resource?
88
+ payload = data
89
+ data = {}
90
+ data[resource] = payload
91
+
92
+ promise = rest.get.call @, data
93
+
94
+ # TODO Get own property descriptor, or better way do override route
95
+ Object.defineProperty @, 'route',
96
+ value: old_route
97
+ configurable: true
98
+
99
+ promise
100
+
101
+ put: rest.put
102
+ delete: rest.delete
103
+
104
+ record:
105
+ ready: (callback) -> callback.call @
106
+ reload: (params...) ->
107
+
108
+ # TODO better signature implementation
109
+ data = params.pop()
110
+ params.push data if type(data) != 'object'
111
+
112
+ promise = rest.get.call @, data || {}
113
+ promise.done @assign_attributes
114
+ promise.fail @failed
115
+
116
+ @reloading = promise
117
+ # Assign ready callback before, to allow promise override
118
+ @ready = ->
119
+ console.warn "resource.ready was deprecated, please use resource.reloading.done"
120
+ promise.done arguments...
121
+
122
+ # Bind one time save callbacks
123
+ promise.done param for param in params
124
+
125
+ promise
126
+
127
+
128
+ # TODO sufix association attributes with '_attributes' and remove
129
+ # double checking in this method
130
+ assign_attributes: (attributes) ->
131
+
132
+ # TODO only set associations on nested attributes!
133
+ # TODO implement setter on has_many association and move this code there
134
+ # First assign has_many associations
135
+ for association_name in model[@resource.toString()].has_many
136
+ associations_attributes = attributes[association_name + "_attributes"] or attributes[association_name]
137
+
138
+ # TODO copy attributes object and don't change it inside the assignment method
139
+ # TODO sufix association attributes with '_attributes' and remove
140
+ # double checking in this method
141
+ delete attributes[association_name] # Remove loaded json data
142
+ delete attributes[association_name + '_attributes'] # Remove loaded json data
143
+
144
+ # Clear current stored cache on this association
145
+ # TODO implement setter on this association and let user to set
146
+ # it to an empty array
147
+ association = @[association_name]
148
+
149
+ unless association?
150
+ message = "Association '#{association_name}' not found. \n"
151
+ message += "For record with resource #{@resource}. \n"
152
+ message += "Probably defined on server side but not on client side.\n"
153
+ message += "Skipping association assignment!"
154
+ console.warn message
155
+ continue
156
+
157
+ # TODO implement association.clear
158
+ Array.prototype.splice.call association, 0 if association.length
159
+
160
+ # continue if no associations_attributes were found by the server
161
+ continue unless associations_attributes? and associations_attributes.length
162
+
163
+ singular_resource = model.singularize association_name
164
+
165
+ # Normalize json data for building on association
166
+ for association_attributes in associations_attributes
167
+
168
+ # TODO only nest specified nested attributes on model definition
169
+ # TODO create special deserialization method on plural association
170
+ # TODO check if we need to nest attributes in other association types
171
+ for association_name in model[singular_resource].has_many
172
+ association_attributes["#{association_name}_attributes"] = association_attributes[association_name]
173
+
174
+ # TODO copy attributes object and don't change it inside
175
+ # the assignment method
176
+ delete association_attributes[association_name]
177
+
178
+ # Load new associations_attributes on this association
179
+ association.add associations_attributes...
180
+
181
+
182
+ # Nested attributes
183
+ # TODO implement setter on has_one association and move this
184
+ # code there
185
+ for association_name in model[@resource.toString()].has_one
186
+ association_attributes = attributes[association_name]
187
+
188
+ # TODO copy attributes object and don't change it inside the
189
+ # assignment method
190
+ delete attributes[association_name]
191
+ delete attributes[association_name + "_attributes"]
192
+ if association_attributes
193
+ associated = @[association_name] || @["build_#{association_name}"] _id: association_attributes._id
194
+ associated.assign_attributes association_attributes
195
+ @[association_name] = associated
196
+
197
+
198
+ # Nested attributes
199
+ # TODO implement setter on belongs_to association and move this
200
+ # code there
201
+ for association_name in model[@resource.toString()].belongs_to
202
+ association_attributes = attributes[association_name]?.json?() ? attributes[association_name]
203
+
204
+ # TODO copy attributes object and don't change it inside the
205
+ # assignment method
206
+ delete attributes[association_name]
207
+ delete attributes[association_name + "_attributes"]
208
+ if association_attributes
209
+ associated = @[association_name] || @["build_#{association_name}"] _id: association_attributes._id
210
+ associated.assign_attributes association_attributes
211
+ @[association_name] = associated
212
+
213
+
214
+ # Assign remaining attributes
215
+ # TODO see if it is a best practice not overriding unchanged attributes
216
+ # TODO rename attributes for properties
217
+ for name, attribute of attributes when attribute isnt @[name]
218
+ # TODO faster object property assignment, get from model definition, instead of checking every attribute
219
+ # TODO implement custom comparator for each object when es7 is out
220
+ if type(attribute) == 'object'
221
+ @[name] = attributes[name] if JSON.stringify(attribute) != JSON.stringify @[name]
222
+ else
223
+ @[name] = attributes[name]
224
+
225
+ destroy: (doned, failed, data) ->
226
+ throw new Error 'Can\'t delete record without id!' unless @id? or @_id?
227
+
228
+ promise = rest.delete.call @, data
229
+ promise.done @destroyed
230
+ promise.fail @failed
231
+
232
+ # Bind one time save callbacks
233
+ promise.done doned
234
+ promise.fail failed
235
+
236
+ promise
237
+
238
+ saving: false
239
+ salvation: null
240
+ save: (doned, failed, data) ->
241
+ lock = JSON.stringify @json()
242
+
243
+ # When saving and receive save command again check if the model
244
+ # has changed, then abort the salvation operation and send a new
245
+ # save request
246
+ # TODO check dirty property instead of lock!
247
+ if @saving
248
+ if @lock == lock
249
+ return @salvation
250
+ else
251
+ @salvation.abort()
252
+
253
+ # TODO better lock generation
254
+ @lock = lock
255
+
256
+ # TODO remove jquery dependency
257
+ # TODO think with wich value makes more sense to resolve the
258
+ # absence of need to save the model
259
+ salvation = $.Deferred().resolveWith @, null unless @dirty
260
+ @saving = true
261
+ salvation ||= rest[if @_id then 'put' else 'post'].call @, data
262
+ @salvation = salvation
263
+
264
+ salvation.done @saved
265
+ salvation.fail @failed
266
+ salvation.always -> @saving = false
267
+
268
+ # Bind one time save callbacks
269
+ salvation.done doned
270
+ salvation.fail failed
271
+
272
+ salvation
273
+
274
+ saved: (data) ->
275
+
276
+ # TODO better lock generation
277
+ if @lock == JSON.stringify(@json())
278
+ @dirty = false
279
+ delete @lock
280
+
281
+ @assign_attributes data if data?
282
+
283
+ throw "Not supported after_save callback: " + callback for callback in @after_save if @after_save
284
+
285
+ # Parse error json if any
286
+ failed: (xhr, error, status) ->
287
+ payload = xhr.responseJSON
288
+ try payload ||= JSON.parse(xhr.responseText) catch e
289
+ payload ||= xhr.responseText
290
+
291
+ # When client fail
292
+ switch xhr.status
293
+ # TODO move to validatable
294
+ when 0
295
+ message = status or xhr.statusText
296
+ switch message
297
+ when 'abort'
298
+ console.info "salvation probably aborted"
299
+ when 'error'
300
+ console.info "server probably unreachable"
301
+ else
302
+ throw new Error 'Unhandled status code for xhr'
303
+
304
+ when 422
305
+
306
+ definition = model[@resource.toString()]
307
+
308
+ for attribute_name, messages of payload.errors
309
+
310
+ # TODO add support for error checking message introspection
311
+ # Check for association errors
312
+ if (!definition.associations)
313
+ # TODO update this attribute when associations are dinamically changed
314
+ definition.associations = definition.has_one.concat(definition.has_many.concat(definition.belongs_to))
315
+
316
+ # Only add errors to existing attributes
317
+ # TODO shorten this verification
318
+ unless @hasOwnProperty(attribute_name) or definition.hasOwnProperty(attribute_name) or definition.associations.indexOf(attribute_name) != -1 or attribute_name == 'base'
319
+
320
+ message = "Server returned an validation error message for a attribute that is not defined in your model.\n"
321
+ message += "The attribute was '#{attribute_name}', the model resource was '#{@resource}'.\n"
322
+ message += "The model definition keys were '#{JSON.stringify Object.keys definition }'.\n"
323
+ message += "Please remove server validation, or update your model definition."
324
+ throw new TypeError message
325
+
326
+ for message in messages
327
+ @errors.add attribute_name, 'server', server_message: message
328
+
329
+ # Unknown fail
330
+ else
331
+ message = "Fail in #{@resource}.save:\n"
332
+ message += "Record: #{@}\n"
333
+ message += "Status: #{status} (#{(payload || xhr).status})\n"
334
+ message += "Error : #{payload.error || payload.message || payload}"
335
+ console.log message
336
+
337
+ # Finish saving
338
+ @saving = false
339
+
340
+ # TODO move to record.coffee
341
+ toString: ->
342
+ serialized = {}
343
+ serialized[@resource] = @json()
344
+
345
+ try
346
+ # TODO figure out why it throws circular references sometines
347
+ JSON.stringify serialized
348
+ catch e
349
+ console.warn "restfulable.toString: Failed to stringify record: #{e.message}. retrying..."
350
+
351
+ for name, property of serialized
352
+ delete serialized[name] if typeof property == 'object'
353
+
354
+ JSON.stringify serialized
355
+
356
+ # TODO move this to serializable module
357
+ # TODO figure out why sometimes is rendering a circular referenced json
358
+ # TODO rename to toJSON
359
+ json: (options = {}) ->
360
+ json = {}
361
+
362
+ definition = model[@resource.toString()]
363
+
364
+ for name of @
365
+ # TODO smarter way to ignore Dom node fix properties
366
+ continue if observable.ignores.indexOf(name) != -1
367
+
368
+ # TODO treat other associations to!
369
+ # TODO create association reflection for god sake!
370
+ nested = @nested_attributes.indexOf(name) != -1
371
+
372
+ # Skip association attributes that are note nested TODO create
373
+ # an associations array
374
+ continue if not nested and (definition.belongs_to.indexOf(name) != -1 or definition.has_one.indexOf(name) != -1 or definition.has_many.indexOf(name) != -1)
375
+
376
+ # TODO Bypass only undefined values so we can erase data on server
377
+ value = @[name]
378
+ continue unless value?
379
+
380
+ nature = type value
381
+ continue if nature == 'function'
382
+
383
+ if nature == 'object' or nature == 'element'
384
+
385
+ if nested
386
+ unless value.json
387
+ console.warn "json: Tryied to serialize nested attribute '#{name}' without serialization method!"
388
+ continue
389
+
390
+ # TODO move nested attributes to model definition and
391
+ # implement toJSON there
392
+ json["#{name}_attributes"] = value.json options[name]
393
+
394
+ # Serialize complex type values
395
+ else if value.toJSON? || value.json?
396
+ # FIXME sometimes wrong pluralization occurs and we cannot
397
+ # skip association objects, so detect them and skip here
398
+ continue if value.resource
399
+
400
+ # TODO rename json to toJSON
401
+ if value.json?
402
+ json[name] = value.json options[name]
403
+ else
404
+ json[name] = value.toJSON options[name]
405
+
406
+ # It is a complex type value without serializtion support so
407
+ # we just ignore it
408
+ else
409
+ # TODO maybe log warning based on debug or info flag here?
410
+ continue
411
+
412
+ else
413
+
414
+ # Serialize primitive type values
415
+ json[name] = value
416
+
417
+ # Remove observable options and dom node properties
418
+ json = observable.unobserve json
419
+
420
+ for name, value of options.methods ? {}
421
+ method = @[name]
422
+ if typeof method == 'function'
423
+ json[name] = method()
424
+ else
425
+ json[name] = method
426
+
427
+ # TODO Store reserved words in a array
428
+ # TODO Use _.omit function
429
+ # TODO Use object.defineProperty to not need to delete this properties
430
+ # Remove model reserved words
431
+ delete json.dirty
432
+ delete json.resource
433
+ delete json.route
434
+ delete json.initial_route # TODO implement better initial_route and remove attribute from here
435
+
436
+ delete json.after_initialize
437
+ delete json.before_initialize
438
+ delete json.parent_resource
439
+ delete json.nested_attributes
440
+
441
+ delete json.reloading
442
+ delete json.ready
443
+
444
+ delete json.saving
445
+ delete json.salvation
446
+ delete json.sustained
447
+
448
+ delete json.element
449
+ delete json.default
450
+ delete json.lock
451
+
452
+ delete json.validated
453
+ delete json.validation
454
+ delete json.errors
455
+
456
+ json
457
+
458
+
459
+ # TODO pt udeprecation warning on json method
460
+ # TODO rename json method to toJSON
461
+ restful.toJSON = restful.json
462
+
463
+
464
+ # Extend indemma
465
+ model = window.model # TODO better way to get parent
466
+ record = window.record # TODO better way to get parent
467
+
468
+ model.restfulable = true
469
+
470
+ record.mix (recordable) ->
471
+ merge recordable, restful.record
472
+
473
+
474
+ model.mix (modelable ) ->
475
+ merge modelable , restful.model
476
+
477
+
478
+ model.associable && model.associable.mix (singular_association, plural_association) ->
479
+
480
+ # TODO move route setting to plural_association.after_mix
481
+ plural_association.get = ->
482
+ @route ||= "#{@parent.route}/#{@parent._id}/#{model.pluralize @resource.name}" if @parent?
483
+ rest.get.apply @, arguments
484
+
485
+ plural_association.post = ->
486
+ @route ||= "#{@parent.route}/#{@parent._id}/#{model.pluralize @resource.name}" if @parent?
487
+ rest.post.apply @, arguments