api_maker 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/app/api_maker/api_helpers/api_maker_helpers.rb +5 -0
  3. data/app/api_maker/services/can_can/load_abilities.rb +30 -0
  4. data/app/api_maker/services/devise/sign_in.rb +64 -0
  5. data/app/api_maker/services/devise/sign_out.rb +9 -0
  6. data/app/api_maker/services/models/find_or_create_by.rb +18 -0
  7. data/app/channels/api_maker/subscriptions_channel.rb +33 -2
  8. data/app/controllers/api_maker/base_controller.rb +7 -3
  9. data/app/controllers/api_maker/commands_controller.rb +26 -4
  10. data/app/controllers/api_maker/session_statuses_controller.rb +1 -1
  11. data/app/services/api_maker/abilities_loader.rb +104 -0
  12. data/app/services/api_maker/application_service.rb +2 -1
  13. data/app/services/api_maker/base_command.rb +248 -0
  14. data/app/services/api_maker/collection_command_service.rb +29 -15
  15. data/app/services/api_maker/collection_loader.rb +124 -0
  16. data/app/services/api_maker/command_failed_error.rb +3 -0
  17. data/app/services/api_maker/command_response.rb +17 -6
  18. data/app/services/api_maker/command_service.rb +3 -3
  19. data/app/services/api_maker/create_command.rb +11 -26
  20. data/app/services/api_maker/create_command_service.rb +3 -3
  21. data/app/services/api_maker/database_type.rb +9 -0
  22. data/app/services/api_maker/deep_merge_params.rb +26 -0
  23. data/app/services/api_maker/deserializer.rb +35 -0
  24. data/app/services/api_maker/destroy_command.rb +15 -21
  25. data/app/services/api_maker/destroy_command_service.rb +3 -3
  26. data/app/services/api_maker/generate_react_native_api_service.rb +3 -19
  27. data/app/services/api_maker/include_helpers.rb +17 -0
  28. data/app/services/api_maker/index_command.rb +8 -88
  29. data/app/services/api_maker/index_command_service.rb +5 -5
  30. data/app/services/api_maker/js_method_namer_service.rb +1 -1
  31. data/app/services/api_maker/locals_from_controller.rb +14 -0
  32. data/app/services/api_maker/member_command_service.rb +15 -13
  33. data/app/services/api_maker/model_classes_java_script_generator_service.rb +37 -0
  34. data/app/services/api_maker/model_content_generator_service.rb +17 -21
  35. data/app/services/api_maker/models/save.rb +29 -0
  36. data/app/services/api_maker/models_finder_service.rb +6 -2
  37. data/app/services/api_maker/models_generator_service.rb +6 -43
  38. data/app/services/api_maker/move_components_to_routes.rb +50 -0
  39. data/app/services/api_maker/primary_id_for_model.rb +6 -0
  40. data/app/services/api_maker/reset_indexed_db_service.rb +36 -0
  41. data/app/services/api_maker/routes_file_reloader.rb +20 -0
  42. data/app/services/api_maker/select_columns_on_collection.rb +78 -0
  43. data/app/services/api_maker/select_parser.rb +32 -0
  44. data/app/services/api_maker/service_command.rb +27 -0
  45. data/app/services/api_maker/service_command_service.rb +14 -0
  46. data/app/services/api_maker/simple_model_errors.rb +52 -0
  47. data/app/services/api_maker/update_command.rb +8 -24
  48. data/app/services/api_maker/update_command_service.rb +3 -3
  49. data/app/services/api_maker/valid_command.rb +4 -13
  50. data/app/services/api_maker/valid_command_service.rb +3 -3
  51. data/app/services/api_maker/validation_errors_generator_service.rb +146 -0
  52. data/app/views/api_maker/_data.html.erb +17 -11
  53. data/config/routes.rb +0 -2
  54. data/lib/api_maker/ability.rb +22 -7
  55. data/lib/api_maker/ability_loader.rb +9 -6
  56. data/lib/api_maker/base_collection_instance.rb +15 -0
  57. data/lib/api_maker/base_resource.rb +135 -9
  58. data/lib/api_maker/base_service.rb +14 -0
  59. data/lib/api_maker/collection_serializer.rb +95 -34
  60. data/lib/api_maker/command_spec_helper.rb +41 -11
  61. data/lib/api_maker/configuration.rb +31 -4
  62. data/lib/api_maker/expect_to_able_to_helper.rb +31 -0
  63. data/lib/api_maker/individual_command.rb +24 -9
  64. data/lib/api_maker/javascript/model-template.js.erb +39 -25
  65. data/lib/api_maker/javascript/models.js.erb +6 -0
  66. data/lib/api_maker/loader.rb +1 -1
  67. data/lib/api_maker/memory_storage.rb +1 -1
  68. data/lib/api_maker/model_extensions.rb +34 -18
  69. data/lib/api_maker/permitted_params_argument.rb +5 -1
  70. data/lib/api_maker/preloader.rb +71 -32
  71. data/lib/api_maker/preloader_base.rb +108 -0
  72. data/lib/api_maker/preloader_belongs_to.rb +34 -33
  73. data/lib/api_maker/preloader_has_many.rb +45 -39
  74. data/lib/api_maker/preloader_has_one.rb +30 -47
  75. data/lib/api_maker/railtie.rb +3 -11
  76. data/lib/api_maker/relationship_preloader.rb +42 -0
  77. data/lib/api_maker/resource_routing.rb +18 -4
  78. data/lib/api_maker/result_parser.rb +34 -20
  79. data/lib/api_maker/serializer.rb +53 -22
  80. data/lib/api_maker/spec_helper/browser_logs.rb +14 -0
  81. data/lib/api_maker/spec_helper/execute_collection_command.rb +46 -0
  82. data/lib/api_maker/spec_helper/execute_member_command.rb +52 -0
  83. data/lib/api_maker/spec_helper/expect_no_browser_errors.rb +18 -0
  84. data/lib/api_maker/spec_helper/wait_for_expect.rb +20 -0
  85. data/lib/api_maker/spec_helper/wait_for_flash_message.rb +21 -0
  86. data/lib/api_maker/spec_helper.rb +112 -48
  87. data/lib/api_maker/version.rb +1 -1
  88. data/lib/api_maker.rb +7 -3
  89. metadata +108 -89
  90. data/README.md +0 -476
  91. data/app/controllers/api_maker/devise_controller.rb +0 -60
  92. data/lib/api_maker/base_command.rb +0 -81
  93. data/lib/api_maker/javascript/api.js +0 -92
  94. data/lib/api_maker/javascript/base-model.js +0 -543
  95. data/lib/api_maker/javascript/bootstrap/attribute-row.jsx +0 -16
  96. data/lib/api_maker/javascript/bootstrap/attribute-rows.jsx +0 -47
  97. data/lib/api_maker/javascript/bootstrap/card.jsx +0 -79
  98. data/lib/api_maker/javascript/bootstrap/checkbox.jsx +0 -127
  99. data/lib/api_maker/javascript/bootstrap/checkboxes.jsx +0 -105
  100. data/lib/api_maker/javascript/bootstrap/live-table.jsx +0 -168
  101. data/lib/api_maker/javascript/bootstrap/money-input.jsx +0 -136
  102. data/lib/api_maker/javascript/bootstrap/radio-buttons.jsx +0 -80
  103. data/lib/api_maker/javascript/bootstrap/select.jsx +0 -168
  104. data/lib/api_maker/javascript/bootstrap/string-input.jsx +0 -203
  105. data/lib/api_maker/javascript/cable-connection-pool.js +0 -169
  106. data/lib/api_maker/javascript/cable-subscription-pool.js +0 -111
  107. data/lib/api_maker/javascript/cable-subscription.js +0 -33
  108. data/lib/api_maker/javascript/collection.js +0 -186
  109. data/lib/api_maker/javascript/commands-pool.js +0 -123
  110. data/lib/api_maker/javascript/custom-error.js +0 -14
  111. data/lib/api_maker/javascript/deserializer.js +0 -35
  112. data/lib/api_maker/javascript/devise.js.erb +0 -113
  113. data/lib/api_maker/javascript/error-logger.js +0 -119
  114. data/lib/api_maker/javascript/event-connection.jsx +0 -24
  115. data/lib/api_maker/javascript/event-created.jsx +0 -26
  116. data/lib/api_maker/javascript/event-destroyed.jsx +0 -26
  117. data/lib/api_maker/javascript/event-emitter-listener.jsx +0 -32
  118. data/lib/api_maker/javascript/event-listener.jsx +0 -41
  119. data/lib/api_maker/javascript/event-updated.jsx +0 -26
  120. data/lib/api_maker/javascript/form-data-to-object.js +0 -70
  121. data/lib/api_maker/javascript/included.js +0 -39
  122. data/lib/api_maker/javascript/key-value-store.js +0 -47
  123. data/lib/api_maker/javascript/logger.js +0 -23
  124. data/lib/api_maker/javascript/model-name.js +0 -21
  125. data/lib/api_maker/javascript/models-response-reader.js +0 -43
  126. data/lib/api_maker/javascript/paginate.jsx +0 -128
  127. data/lib/api_maker/javascript/params.js +0 -68
  128. data/lib/api_maker/javascript/resource-route.jsx +0 -75
  129. data/lib/api_maker/javascript/resource-routes.jsx +0 -36
  130. data/lib/api_maker/javascript/result.js +0 -25
  131. data/lib/api_maker/javascript/session-status-updater.js +0 -113
  132. data/lib/api_maker/javascript/sort-link.jsx +0 -88
  133. data/lib/api_maker/javascript/updated-attribute.jsx +0 -60
  134. data/lib/api_maker/preloader_through.rb +0 -101
  135. data/lib/api_maker/relationship_includer.rb +0 -42
@@ -1,543 +0,0 @@
1
- import CableConnectionPool from "./cable-connection-pool"
2
- import Collection from "./collection"
3
- import CommandsPool from "./commands-pool"
4
- import CustomError from "./custom-error"
5
- import FormDataToObject from "./form-data-to-object"
6
- import ModelName from "./model-name"
7
- import Money from "js-money"
8
- import objectToFormData from "object-to-formdata"
9
-
10
- const inflection = require("inflection")
11
-
12
- export default class BaseModel {
13
- static modelClassData() {
14
- throw new Error("modelClassData should be overriden by child")
15
- }
16
-
17
- static async find(id) {
18
- var primaryKeyName = this.modelClassData().primaryKey
19
- var query = {}
20
- query[`${primaryKeyName}_eq`] = id
21
-
22
- var model = await this.ransack(query).first()
23
-
24
- if (model) {
25
- return model
26
- } else {
27
- throw new CustomError("Record not found")
28
- }
29
- }
30
-
31
- static modelName() {
32
- return new ModelName({modelClassData: this.modelClassData()})
33
- }
34
-
35
- static ransack(query = {}) {
36
- return new Collection({modelClass: this}, {ransack: query})
37
- }
38
-
39
- constructor(args = {}) {
40
- this.changes = {}
41
- this.newRecord = args.isNewRecord
42
- this.relationshipsCache = {}
43
-
44
- if (args && args.data && args.data.a) {
45
- this._readModelDataFromArgs(args)
46
- } else if (args.a) {
47
- this.modelData = args.a
48
- } else if (args) {
49
- this.modelData = args
50
- } else {
51
- this.modelData = {}
52
- }
53
- }
54
-
55
- isAssociationLoaded(associationName) {
56
- if (associationName in this.relationshipsCache)
57
- return true
58
-
59
- return false
60
- }
61
-
62
- connect(eventName, callback) {
63
- var cableSubscription = CableConnectionPool.current().connectEvent(this.modelClassData().name, this._primaryKey(), eventName, callback)
64
- return cableSubscription
65
- }
66
-
67
- static connectCreated(callback) {
68
- var cableSubscription = CableConnectionPool.current().connectCreated(this.modelClassData().name, callback)
69
- return cableSubscription
70
- }
71
-
72
- connectDestroyed(callback) {
73
- var cableSubscription = CableConnectionPool.current().connectDestroyed(this.modelClassData().name, this._primaryKey(), callback)
74
- return cableSubscription
75
- }
76
-
77
- connectUpdated(callback) {
78
- var cableSubscription = CableConnectionPool.current().connectUpdate(this.modelClassData().name, this._primaryKey(), callback)
79
- return cableSubscription
80
- }
81
-
82
- assignAttributes(newAttributes) {
83
- for(var key in newAttributes) {
84
- var oldValue = this._getAttribute(key)
85
- var originalValue = this.modelData[key]
86
- var newValue = newAttributes[key]
87
-
88
- if (newValue != oldValue) {
89
- if (newValue == originalValue) {
90
- delete this.changes[key]
91
- } else {
92
- this.changes[key] = newValue
93
- }
94
- }
95
- }
96
- }
97
-
98
- cacheKey() {
99
- if (this.isPersisted()) {
100
- var keyParts = [
101
- this.modelClassData().paramKey,
102
- this._primaryKey()
103
- ]
104
-
105
- if ("updated_at" in this.modelData) {
106
- keyParts.push(`updatedAt-${this.updatedAt().getTime()}`)
107
- }
108
-
109
- return keyParts.join("-")
110
- } else {
111
- return this.uniqueKey()
112
- }
113
- }
114
-
115
- async create() {
116
- var paramKey = this.modelClassData().paramKey
117
- var modelData = this.getAttributes()
118
- var dataToUse = {}
119
- dataToUse[paramKey] = modelData
120
-
121
- var response = await CommandsPool.addCommand({args: dataToUse, command: `${this.modelClassData().collectionName}-create`, collectionName: this.modelClassData().collectionName, primaryKey: this._primaryKey(), type: "create"}, {})
122
-
123
- if (response.success) {
124
- if (response.model) {
125
- this._setNewModelData(response.model.a)
126
- this.changes = {}
127
- }
128
-
129
- return {model: this, response: response}
130
- } else {
131
- throw new new CustomError("Response wasn't successful", {model: this, response: response})
132
- }
133
- }
134
-
135
- async createRaw(data) {
136
- var formData = FormDataToObject.toObject(data)
137
- var response = await CommandsPool.addCommand({args: formData, command: `${this.modelClassData().collectionName}-create`, collectionName: this.modelClassData().collectionName, primaryKey: this._primaryKey(), type: "create"}, {})
138
-
139
- if (response.success) {
140
- if (response.model) {
141
- this._setNewModelData(response.model.a)
142
- this.changes = {}
143
- }
144
-
145
- return {model: this, response: response}
146
- } else {
147
- throw new CustomError("Response wasn't successful", {model: this, response: response})
148
- }
149
- }
150
-
151
- async destroy() {
152
- var response = await CommandsPool.addCommand({command: `${this.modelClassData().collectionName}-destroy`, collectionName: this.modelClassData().collectionName, primaryKey: this._primaryKey(), type: "destroy"}, {})
153
-
154
- if (response.success) {
155
- if (response.model) {
156
- this._setNewModelData(response.model.a)
157
- this.changes = {}
158
- }
159
-
160
- return {model: this, response: response}
161
- } else {
162
- throw new CustomError("Response wasn't successful", {model: this, response: response})
163
- }
164
- }
165
-
166
- getAttributes() {
167
- return Object.assign(this.modelData, this.changes)
168
- }
169
-
170
- static humanAttributeName(attributeName) {
171
- var keyName = this.modelClassData().i18nKey
172
- return I18n.t(`activerecord.attributes.${keyName}.${BaseModel.snakeCase(attributeName)}`)
173
- }
174
-
175
- static snakeCase(string) {
176
- return inflection.underscore(string)
177
- }
178
-
179
- isAttributeChanged(attributeName) {
180
- var attributeNameUnderscore = inflection.underscore(attributeName)
181
- var attributeData = this.modelClassData().attributes.find(attribute => attribute.name == attributeNameUnderscore)
182
-
183
- if (!attributeData) {
184
- var attributeNames = this.modelClassData().attributes.map(attribute => attribute.name)
185
- throw new Error(`Couldn't find an attribute by that name: "${attributeName}" in: ${attributeNames.join(", ")}`)
186
- }
187
-
188
- if (!(attributeNameUnderscore in this.changes))
189
- return false
190
-
191
- var oldValue = this.modelData[attributeNameUnderscore]
192
- var newValue = this.changes[attributeNameUnderscore]
193
- var changedMethod = this[`_is${inflection.camelize(attributeData.type, true)}Changed`]
194
-
195
- if (!changedMethod)
196
- throw new Error(`Don't know how to handle type: ${attributeData.type}`)
197
-
198
- return changedMethod(oldValue, newValue)
199
- }
200
-
201
- savedChangeToAttribute(attributeName) {
202
- if (!this.previousModelData)
203
- return false
204
-
205
- var attributeNameUnderscore = inflection.underscore(attributeName)
206
- var attributeData = this.modelClassData().attributes.find(attribute => attribute.name == attributeNameUnderscore)
207
-
208
- if (!attributeData) {
209
- var attributeNames = this.modelClassData().attributes.map(attribute => attribute.name)
210
- throw new Error(`Couldn't find an attribute by that name: "${attributeName}" in: ${attributeNames.join(", ")}`)
211
- }
212
-
213
- if (!(attributeNameUnderscore in this.previousModelData))
214
- return true
215
-
216
- var oldValue = this.previousModelData[attributeNameUnderscore]
217
- var newValue = this.modelData[attributeNameUnderscore]
218
- var changedMethodName = `_is${inflection.camelize(attributeData.type)}Changed`
219
- var changedMethod = this[changedMethodName]
220
-
221
- if (!changedMethod)
222
- throw new Error(`Don't know how to handle type: ${attributeData.type}`)
223
-
224
- return changedMethod(oldValue, newValue)
225
- }
226
-
227
- _setNewModelData(modelData) {
228
- this.previousModelData = this.modelData
229
- this.modelData = modelData
230
- }
231
-
232
- _isDateChanged(oldValue, newValue) {
233
- if (Date.parse(oldValue) != Date.parse(newValue))
234
- return true
235
- }
236
-
237
- _isIntegerChanged(oldValue, newValue) {
238
- if (parseInt(oldValue) != parseInt(newValue))
239
- return true
240
- }
241
-
242
- _isStringChanged(oldValue, newValue) {
243
- var oldConvertedValue = `${oldValue}`
244
- var newConvertedValue = `${newValue}`
245
-
246
- if (oldConvertedValue != newConvertedValue)
247
- return true
248
- }
249
-
250
- isChanged() {
251
- var keys = Object.keys(this.changes)
252
-
253
- if (keys.length > 0) {
254
- return true
255
- } else {
256
- return false
257
- }
258
- }
259
-
260
- isNewRecord() {
261
- if (this.newRecord === false) {
262
- return false
263
- } else if ("id" in this.modelData && this.modelData.id) {
264
- return false
265
- } else {
266
- return true
267
- }
268
- }
269
-
270
- isPersisted() {
271
- return !this.isNewRecord()
272
- }
273
-
274
- modelClassData() {
275
- return this.constructor.modelClassData()
276
- }
277
-
278
- async reload() {
279
- var primaryKeyName = this.modelClassData().primaryKey
280
- var query = {}
281
- query[`${primaryKeyName}_eq`] = this._primaryKey()
282
-
283
- var model = await this.constructor.ransack(query).first()
284
- this._setNewModelData(model.modelData)
285
- this.changes = {}
286
- }
287
-
288
- save() {
289
- if (this.isNewRecord()) {
290
- return this.create()
291
- } else {
292
- return this.update()
293
- }
294
- }
295
-
296
- saveRaw(rawData) {
297
- if (this.isNewRecord()) {
298
- return this.createRaw(rawData)
299
- } else {
300
- return this.updateRaw(rawData)
301
- }
302
- }
303
-
304
- async update(newAttributes = null) {
305
- if (newAttributes)
306
- this.assignAttributes(newAttributes)
307
-
308
- if (this.changes.length == 0)
309
- return resolve({model: this})
310
-
311
- var paramKey = this.modelClassData().paramKey
312
- var modelData = this.changes
313
- var dataToUse = {}
314
- dataToUse[paramKey] = modelData
315
-
316
- var response = await CommandsPool.addCommand({args: dataToUse, command: `${this.modelClassData().collectionName}-update`, collectionName: this.modelClassData().collectionName, primaryKey: this._primaryKey(), type: "update"}, {})
317
-
318
- if (response.success) {
319
- if (response.model) {
320
- this._setNewModelData(response.model.a)
321
- this.changes = {}
322
- }
323
-
324
- return {"model": this, "response": response}
325
- } else {
326
- throw new CustomError("Response wasn't successful", {"model": this, "response": response})
327
- }
328
- }
329
-
330
- async updateRaw(data) {
331
- var formData = FormDataToObject.toObject(data)
332
- var response = await CommandsPool.addCommand({args: formData, command: `${this.modelClassData().collectionName}-update`, collectionName: this.modelClassData().collectionName, primaryKey: this._primaryKey(), type: "update"}, {})
333
-
334
- if (response.success) {
335
- if (response.model) {
336
- this._setNewModelData(response.model.a)
337
- this.changes = {}
338
- }
339
-
340
- return {model: this, response: response}
341
- } else {
342
- throw new CustomError("Response wasn't successful", {"model": this, "response": response})
343
- }
344
- }
345
-
346
- isValid() {
347
- throw new Error("Not implemented yet")
348
- }
349
-
350
- async isValidOnServer() {
351
- var modelData = this.getAttributes()
352
- var paramKey = this.modelClassData().paramKey
353
- var dataToUse = {}
354
- dataToUse[paramKey] = modelData
355
-
356
- var response = await CommandsPool.addCommand({args: dataToUse, command: `${this.modelClassData().collectionName}-valid`, collectionName: this.modelClassData().collectionName, primaryKey: this._primaryKey(), type: "valid"}, {})
357
-
358
- return {valid: response.valid, errors: response.errors}
359
- }
360
-
361
- modelClass() {
362
- return this.constructor
363
- }
364
-
365
- preloadRelationship(relationshipName, model) {
366
- this.relationshipsCache[BaseModel.snakeCase(relationshipName)] = model
367
- }
368
-
369
- uniqueKey() {
370
- if (!this.uniqueKeyValue) {
371
- var min = 500000000000000000
372
- var max = 999999999999999999
373
- var randomBetween = Math.floor(Math.random() * (max - min + 1) + min)
374
- this.uniqueKeyValue = randomBetween
375
- }
376
-
377
- return this.uniqueKeyValue
378
- }
379
-
380
- static _callCollectionCommand(args, commandArgs) {
381
- return CommandsPool.addCommand(args, commandArgs)
382
- }
383
-
384
- _callMemberCommand(args, commandArgs) {
385
- return CommandsPool.addCommand(args, commandArgs)
386
- }
387
-
388
- static _postDataFromArgs(args) {
389
- var postData
390
-
391
- if (args) {
392
- if (args instanceof FormData) {
393
- postData = args
394
- } else {
395
- postData = objectToFormData(args, {}, null, "args")
396
- }
397
- } else {
398
- postData = new FormData()
399
- }
400
-
401
- return postData
402
- }
403
-
404
- _getAttribute(attributeName) {
405
- if (attributeName in this.changes) {
406
- return this.changes[attributeName]
407
- } else if (attributeName in this.modelData) {
408
- return this.modelData[attributeName]
409
- } else if (this.isNewRecord()) {
410
- // Return null if this is a new record and the attribute name is a recognized attribute
411
- var attributes = this.modelClassData().attributes
412
- for(var attribute of attributes) {
413
- if (attribute.name == attributeName)
414
- return null
415
- }
416
- }
417
-
418
- throw new Error(`No such attribute: ${this.modelClassData().name}#${attributeName}`)
419
- }
420
-
421
- _getAttributeDateTime(attributeName) {
422
- var value = this._getAttribute(attributeName)
423
-
424
- if (!value) {
425
- return value
426
- } else if (value instanceof Date) {
427
- return value
428
- } else {
429
- return new Date(value)
430
- }
431
- }
432
-
433
- _isPresent(value) {
434
- if (!value) {
435
- return false
436
- } else if (typeof value == "string" && value.match(/^\s*$/)) {
437
- return false
438
- }
439
-
440
- return true
441
- }
442
-
443
- _getAttributeMoney(attributeName) {
444
- var value = this._getAttribute(attributeName)
445
-
446
- if (!value)
447
- return null
448
-
449
- var cents = value.amount
450
- var currency = value.currency
451
- return Money.fromInteger(cents, currency)
452
- }
453
-
454
- async _loadBelongsToReflection(args, queryArgs = {}) {
455
- if (args.reflectionName in this.relationshipsCache) {
456
- return this.relationshipsCache[args.reflectionName]
457
- } else {
458
- var collection = new Collection(args, queryArgs)
459
- var model = await collection.first()
460
- this.relationshipsCache[args.reflectionName] = model
461
- return model
462
- }
463
- }
464
-
465
- _readBelongsToReflection(args) {
466
- if (!(args.reflectionName in this.relationshipsCache)) {
467
- if (this.isNewRecord())
468
- return null
469
-
470
- throw new Error(`${this.modelClassData().name}#${args.reflectionName} hasn't been loaded yet`)
471
- }
472
-
473
- return this.relationshipsCache[args.reflectionName]
474
- }
475
-
476
- async _loadHasOneReflection(args, queryArgs = {}) {
477
- if (args.reflectionName in this.relationshipsCache) {
478
- return this.relationshipsCache[args.reflectionName]
479
- } else {
480
- var collection = new Collection(args, queryArgs)
481
- var model = await collection.first()
482
- this.relationshipsCache[args.reflectionName] = model
483
- return model
484
- }
485
- }
486
-
487
- _readHasOneReflection(args) {
488
- if (!(args.reflectionName in this.relationshipsCache)) {
489
- if (this.isNewRecord())
490
- return null
491
-
492
- throw new Error(`${this.modelClassData().name}#${args.reflectionName} hasn't been loaded yet`)
493
- }
494
-
495
- return this.relationshipsCache[args.reflectionName]
496
- }
497
-
498
- _readModelDataFromArgs(args) {
499
- this.modelData = args.data.a
500
- this.includedRelationships = args.data.r
501
- }
502
-
503
- _readIncludedRelationships(included) {
504
- if (!this.includedRelationships)
505
- return
506
-
507
- for(var relationshipName in this.includedRelationships) {
508
- var relationshipData = this.includedRelationships[relationshipName]
509
- var relationshipClassData = this.modelClassData().relationships.find(relationship => relationship.name == relationshipName)
510
-
511
- if (!relationshipClassData)
512
- throw new Error(`No relationship on ${this.modelClassData().name} by that name: ${relationshipName}`)
513
-
514
- var relationshipType = relationshipClassData.collectionName
515
-
516
- if (!relationshipData) {
517
- this.relationshipsCache[relationshipName] = null
518
- } else if (Array.isArray(relationshipData)) {
519
- var result = []
520
-
521
- for(var relationshipId of relationshipData) {
522
- var model = included.getModel(relationshipType, relationshipId)
523
- result.push(model)
524
- }
525
-
526
- this.relationshipsCache[relationshipName] = result
527
- } else {
528
- var model = included.getModel(relationshipType, relationshipData)
529
- this.relationshipsCache[relationshipName] = model
530
- }
531
- }
532
- }
533
-
534
- _primaryKey() {
535
- return this._getAttribute(this.modelClassData().primaryKey)
536
- }
537
-
538
- static _token() {
539
- var csrfTokenElement = document.querySelector("meta[name='csrf-token']")
540
- if (csrfTokenElement)
541
- return csrfTokenElement.getAttribute("content")
542
- }
543
- }
@@ -1,16 +0,0 @@
1
- import React from "react"
2
-
3
- export default class BootstrapAttributeRow extends React.Component {
4
- render() {
5
- return (
6
- <tr>
7
- <th>
8
- {this.props.label}
9
- </th>
10
- <td>
11
- {this.props.value || this.props.children}
12
- </td>
13
- </tr>
14
- )
15
- }
16
- }
@@ -1,47 +0,0 @@
1
- import AttributeRow from "./attribute-row"
2
- import PropTypes from "prop-types"
3
- import React from "react"
4
-
5
- export default class BootstrapAttributeRows extends React.Component {
6
- static propTypes = {
7
- attributes: PropTypes.array.isRequired,
8
- model: PropTypes.object.isRequired
9
- }
10
-
11
- constructor(props) {
12
- super(props)
13
- this.state = {
14
- classObject: props.model.modelClass()
15
- }
16
- }
17
-
18
- render() {
19
- return this.props.attributes.map((attribute) =>
20
- <AttributeRow key={`attribute-${attribute}`} label={this.state.classObject.humanAttributeName(attribute)}>
21
- {this.valueContent(attribute)}
22
- </AttributeRow>
23
- )
24
- }
25
-
26
- value(attribute) {
27
- if (!(attribute in this.props.model))
28
- throw new Error(`Attribute not found: ${this.props.model.modelClassData().name}#${attribute}`)
29
-
30
- return this.props.model[attribute]()
31
- }
32
-
33
- valueContent(attribute) {
34
- var value = this.value(attribute)
35
-
36
- if (value instanceof Date) {
37
- return I18n.strftime(value, "%Y-%m-%d %H:%M")
38
- } else if (typeof value === "boolean") {
39
- if (value)
40
- return I18n.t("js.shared.yes")
41
-
42
- return I18n.t("js.shared.no")
43
- } else {
44
- return this.value(attribute)
45
- }
46
- }
47
- }
@@ -1,79 +0,0 @@
1
- import PropTypes from "prop-types"
2
- import PropTypesExact from "prop-types-exact"
3
- import React from "react"
4
-
5
- export default class Card extends React.Component {
6
- static defaultProps = {
7
- responsiveTable: true
8
- }
9
- static propTypes = PropTypesExact({
10
- className: PropTypes.string,
11
- children: PropTypes.node,
12
- controls: PropTypes.node,
13
- header: PropTypes.string,
14
- onClick: PropTypes.func,
15
- striped: PropTypes.bool,
16
- style: PropTypes.object,
17
- responsiveTable: PropTypes.bool,
18
- table: PropTypes.bool
19
- })
20
-
21
- render() {
22
- var { children, controls, header, onClick, style, table } = this.props
23
-
24
- return (
25
- <div className={this.classNames()} onClick={onClick} ref="card" style={style}>
26
- {(controls || header) &&
27
- <div className="card-header">
28
- {header}
29
- {controls &&
30
- <div className="float-right">
31
- {controls}
32
- </div>
33
- }
34
- </div>
35
- }
36
- <div className={this.bodyClassNames()}>
37
- {table &&
38
- <table className={this.tableClassNames()}>
39
- {children}
40
- </table>
41
- }
42
- {!table && children}
43
- </div>
44
- </div>
45
- )
46
- }
47
-
48
- classNames() {
49
- var classNames = ["component-bootstrap-card", "card", "card-default"]
50
-
51
- if (this.props.className)
52
- classNames.push(this.props.className)
53
-
54
- return classNames.join(" ")
55
- }
56
-
57
- bodyClassNames() {
58
- var classNames = ["card-body"]
59
-
60
- if (this.props.table) {
61
- if (this.props.responsiveTable){
62
- classNames.push("table-responsive")
63
- }
64
-
65
- classNames.push("p-0")
66
- }
67
-
68
- return classNames.join(" ")
69
- }
70
-
71
- tableClassNames() {
72
- var classNames = ["table", "table-hover", "mb-0", "w-100"]
73
-
74
- if (this.props.striped)
75
- classNames.push("table-striped")
76
-
77
- return classNames.join(" ")
78
- }
79
- }