anoubis 1.0.1 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -0
  3. data/app/controllers/anoubis/application_controller.rb +4 -0
  4. data/app/controllers/anoubis/core/index/actions.rb +1 -1
  5. data/app/controllers/anoubis/core/index_controller.rb +3 -3
  6. data/app/controllers/anoubis/data/actions.rb +947 -0
  7. data/app/controllers/anoubis/data/callbacks.rb +66 -0
  8. data/app/controllers/anoubis/data/convert.rb +422 -0
  9. data/app/controllers/anoubis/data/defaults.rb +215 -0
  10. data/app/controllers/anoubis/data/get.rb +529 -0
  11. data/app/controllers/anoubis/data/load.rb +87 -0
  12. data/app/controllers/anoubis/data/set.rb +47 -0
  13. data/app/controllers/anoubis/data/setup.rb +102 -0
  14. data/app/controllers/anoubis/data_controller.rb +21 -0
  15. data/app/controllers/anoubis/etc/field.rb +7 -0
  16. data/app/controllers/anoubis/output/basic.rb +1 -1
  17. data/app/controllers/anoubis/sso/client/index_controller.rb +2 -2
  18. data/app/controllers/anoubis/sso/server/login_controller.rb +5 -5
  19. data/app/controllers/anoubis/sso/server/user_controller.rb +2 -2
  20. data/app/controllers/anoubis/tenant/index_controller.rb +3 -3
  21. data/app/models/anoubis/application_record.rb +141 -0
  22. data/app/services/anoubis/log_service.rb +97 -0
  23. data/app/services/anoubis/request_service.rb +134 -0
  24. data/config/locales/en.yml +20 -6
  25. data/config/locales/ru.yml +25 -13
  26. data/config/routes.rb +19 -19
  27. data/lib/anoubis/version.rb +1 -1
  28. metadata +32 -33
  29. data/app/controllers/anoubis/core/data/actions.rb +0 -962
  30. data/app/controllers/anoubis/core/data/callbacks.rb +0 -68
  31. data/app/controllers/anoubis/core/data/convert.rb +0 -407
  32. data/app/controllers/anoubis/core/data/defaults.rb +0 -217
  33. data/app/controllers/anoubis/core/data/get.rb +0 -531
  34. data/app/controllers/anoubis/core/data/load.rb +0 -89
  35. data/app/controllers/anoubis/core/data/set.rb +0 -49
  36. data/app/controllers/anoubis/core/data/setup.rb +0 -104
  37. data/app/controllers/anoubis/core/data_controller.rb +0 -28
  38. data/app/controllers/anoubis/sso/client/data/actions.rb +0 -5
  39. data/app/controllers/anoubis/sso/client/data/callbacks.rb +0 -5
  40. data/app/controllers/anoubis/sso/client/data/convert.rb +0 -5
  41. data/app/controllers/anoubis/sso/client/data/defaults.rb +0 -5
  42. data/app/controllers/anoubis/sso/client/data/get.rb +0 -5
  43. data/app/controllers/anoubis/sso/client/data/load.rb +0 -26
  44. data/app/controllers/anoubis/sso/client/data/set.rb +0 -5
  45. data/app/controllers/anoubis/sso/client/data/setup.rb +0 -5
  46. data/app/controllers/anoubis/sso/client/data_controller.rb +0 -21
  47. data/app/controllers/anoubis/tenant/data/actions.rb +0 -11
  48. data/app/controllers/anoubis/tenant/data/callbacks.rb +0 -11
  49. data/app/controllers/anoubis/tenant/data/convert.rb +0 -11
  50. data/app/controllers/anoubis/tenant/data/defaults.rb +0 -11
  51. data/app/controllers/anoubis/tenant/data/get.rb +0 -11
  52. data/app/controllers/anoubis/tenant/data/load.rb +0 -52
  53. data/app/controllers/anoubis/tenant/data/set.rb +0 -11
  54. data/app/controllers/anoubis/tenant/data/setup.rb +0 -11
  55. data/app/controllers/anoubis/tenant/data_controller.rb +0 -28
  56. data/app/controllers/anoubis/tenants_controller.rb +0 -7
  57. data/app/controllers/anoubis/users_controller.rb +0 -7
  58. data/app/mailers/anoubis/application_mailer.rb +0 -8
@@ -0,0 +1,947 @@
1
+ module Anoubis
2
+ module Data
3
+ ##
4
+ # Module presents all default actions for for {DataController}.
5
+ module Actions
6
+ ##
7
+ # Default action of {DataController}. Procedure outputs data loaded from database table.
8
+ # Authorization bearer is required.
9
+ #
10
+ # <b>API request:</b>
11
+ # GET /api/<version>/<controller>
12
+ #
13
+ # <b>Request Header:</b>
14
+ # {
15
+ # "Authorization": "Bearer <Session token>"
16
+ # }
17
+ #
18
+ # <b>Parameters:</b>
19
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
20
+ # - <b>offset</b> (String) --- starting number for selection <i>(optional value, default: 0)</i>
21
+ # - <b>limit</b> (String) --- number of selected rows <i>(optional value, default: 10)</i>
22
+ # - <b>tab</b> (String) --- the tab, is used for selected data <i>(optional value, default: first defined tab)</i>
23
+ #
24
+ # <b>Request example:</b>
25
+ # curl --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>?offset=0&limit=10
26
+ #
27
+ # <b>Results:</b>
28
+ #
29
+ # Resulting data returns in JSON format.
30
+ #
31
+ # <b>Examples:</b>
32
+ #
33
+ # <b>Success:</b> HTTP response code 200
34
+ # {
35
+ # "result": 0,
36
+ # "message": "Successful",
37
+ # "count": 5,
38
+ # "tab": "inner",
39
+ # "offset": "0",
40
+ # "limit": "10",
41
+ # "timestamp": 1563169525,
42
+ # "fields": [
43
+ # {
44
+ # "prop": "title",
45
+ # "title": "Soldier Ttitle"
46
+ # "type": "string",
47
+ # "sortable": true
48
+ # },
49
+ # {
50
+ # "prop": "name",
51
+ # "title": "Soldier Name"
52
+ # "type": "string",
53
+ # "sortable": true
54
+ # },
55
+ # {
56
+ # "prop": "age",
57
+ # "title": "Girl Age"
58
+ # "type": "string",
59
+ # "sortable": true
60
+ # }
61
+ # ],
62
+ # "data": [
63
+ # {
64
+ # "id": 1,
65
+ # "sys_title": "Sailor Moon",
66
+ # "actions": {
67
+ # "edit": "Edit: Sailor Moon",
68
+ # "delete": "Delete: Sailor Moon"
69
+ # },
70
+ # "title": "Sailor Moon",
71
+ # "name": "Banny Tsukino",
72
+ # "age": 16,
73
+ # "state": "inner"
74
+ # },
75
+ # {
76
+ # "id": 2,
77
+ # "sys_title": "Sailor Mercury",
78
+ # "actions": {
79
+ # "edit": "Edit: Sailor Mercury",
80
+ # "delete": "Delete: Sailor Mercury"
81
+ # },
82
+ # "title": "Sailor Mercury",
83
+ # "name": "Amy Mitsuno",
84
+ # "age": 16,
85
+ # "state": "inner"
86
+ # }
87
+ # ]
88
+ # }
89
+ def index
90
+ self.etc.data = Anoubis::Etc::Data.new
91
+ self.set_parent_model 'index'
92
+ self.output = Anoubis::Output::Data.new
93
+ self.output.count = self.get_table_data_count
94
+ self.setup_fields
95
+ self.output.limit = self.etc.data.limit
96
+ self.output.offset = self.etc.data.offset
97
+ self.output.fields = self.get_fields_properties if self.etc.time == 0
98
+ self.output.filter = self.get_filter_properties if self.etc.time == 0
99
+ self.output.data = self.get_table_data
100
+ self.output.tab = self.etc.tab.tab
101
+ self.output.sortable = self.is_sortable
102
+ self.output.order = self.etc.tab.order if self.etc.tab.order != ''
103
+ self.output.sort = self.etc.tab.sort if self.etc.tab.sort != nil
104
+ self.after_get_table_data
105
+ self.before_output
106
+
107
+ render json: around_output(output.to_h)
108
+ end
109
+
110
+ ##
111
+ # <i>Frame</i> action of {DataController}. Procedure outputs frame data information (title, tabs, frame buttons)
112
+ # Authorization bearer is required.
113
+ #
114
+ # <b>API request:</b>
115
+ # GET /api/<version>/<controller>/frame
116
+ #
117
+ # <b>Request Header:</b>
118
+ # {
119
+ # "Authorization": "Bearer <Session token>"
120
+ # }
121
+ #
122
+ # <b>Parameters:</b>
123
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
124
+ # - <b>offset</b> (String) --- starting number for selection <i>(optional value, default: 0)</i>
125
+ # - <b>limit</b> (String) --- number of selected rows <i>(optional value, default: 10)</i>
126
+ # - <b>tab</b> (String) --- the tab, is used for selected data <i>(optional value, default: first defined tab)</i>
127
+ #
128
+ # <b>Request example:</b>
129
+ # curl --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/frame
130
+ #
131
+ # <b>Results:</b>
132
+ #
133
+ # Resulting data returns in JSON format.
134
+ #
135
+ # <b>Examples:</b>
136
+ #
137
+ # <b>Success:</b> HTTP response code 200
138
+ # {
139
+ # "result": 0,
140
+ # "message": "Successful",
141
+ # "timestamp": 1563271417,
142
+ # "title": "Sailor soldiers",
143
+ # "short": "Soldiers",
144
+ # "mode": "soldiers",
145
+ # "access": "write",
146
+ # "tabs": [
147
+ # {
148
+ # "tab": "inner",
149
+ # "title": "Inner Senshi",
150
+ # "buttons": [
151
+ # {
152
+ # "key": "new",
153
+ # "mode": "single",
154
+ # "type": "primary"
155
+ # }
156
+ # ],
157
+ # "hint": "Shows only inner soldiers"
158
+ # },
159
+ # {
160
+ # "tab": "outer",
161
+ # "title": "Outer Senshi",
162
+ # "buttons": [
163
+ # {
164
+ # "key": "new",
165
+ # "mode": "single",
166
+ # "type": "primary"
167
+ # },
168
+ # {
169
+ # "key": "delete",
170
+ # "mode": "multiple",
171
+ # "type": "danger"
172
+ # }
173
+ # ]
174
+ # }
175
+ # ]
176
+ # }
177
+ #
178
+ #
179
+ # <b>Error (session expired):</b> HTTP response code 422
180
+ # {
181
+ # "result": -1,
182
+ # "message": "Session expired",
183
+ # "timestamp": 1563271417,
184
+ # "tab": "inner"
185
+ # }
186
+ def frame
187
+ self.output = Anoubis::Output::Frame.new
188
+ self.etc.data = Anoubis::Etc::Data.new unless etc.data
189
+ self.etc.action = 'frame'
190
+ if parent_model
191
+ self.output.result = -2 unless self.get_parent_data
192
+ end
193
+ setup_frame
194
+ before_output
195
+
196
+ render json: around_output(output.to_h)
197
+ end
198
+
199
+ ##
200
+ # <i>Show</i> action of {DataController}. Procedure outputs values for view form.
201
+ # Authorization bearer is required.
202
+ #
203
+ # <b>API request:</b>
204
+ # GET /api/<version>/<controller>/<id>
205
+ #
206
+ # <b>Request Header:</b>
207
+ # {
208
+ # "Authorization": "Bearer <Session token>"
209
+ # }
210
+ #
211
+ # <b>Parameters:</b>
212
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
213
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
214
+ #
215
+ # Resulting output title is took from translation file <lang>.yml at path:
216
+ # <lang>:
217
+ # <controller name divided by level>:
218
+ # edit:
219
+ # form:
220
+ # title: "Edit soldier %{title}"
221
+ #
222
+ # If this path isn't defined in translation file, then value is took from path:
223
+ # <lang>:
224
+ # anubis:
225
+ # form:
226
+ # titles:
227
+ # edit: "Edit element: %{title}"
228
+ #
229
+ # <b>Request example:</b>
230
+ # curl --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/<id>?tab=<tab>
231
+ #
232
+ # <b>Results:</b>
233
+ #
234
+ # Resulting data returns in JSON format.
235
+ #
236
+ # <b>Examples:</b>
237
+ #
238
+ # <b>Success:</b> HTTP response code 200
239
+ # {
240
+ # "result": 0,
241
+ # "message": "Successful",
242
+ # "timestamp": 1563271417,
243
+ # "tab": "inner",
244
+ # "title": "Edit soldier Sailor Mars",
245
+ # "values": {
246
+ # "id": 3,
247
+ # "title": "Sailor Mars",
248
+ # "name": "Rey Hino",
249
+ # "state_view": "Inner Senshi",
250
+ # "state": "inner"
251
+ # },
252
+ # "options": {
253
+ # "state": {
254
+ # "inner": "Inner Senshi",
255
+ # "outer": "Outer Senshi",
256
+ # "star": "Sailor Star"
257
+ # }
258
+ # }
259
+ # }
260
+ #
261
+ #
262
+ # <b>Error (incorrect request id):</b> HTTP response code 200
263
+ # {
264
+ # "result": -2,
265
+ # "message": "Incorrect request parameters",
266
+ # "timestamp": 1563271417,
267
+ # "tab": "inner"
268
+ # }
269
+ #
270
+ #
271
+ # <b>Error (session expired):</b> HTTP response code 422
272
+ # {
273
+ # "result": -1,
274
+ # "message": "Session expired",
275
+ # "timestamp": 1563271417,
276
+ # "tab": "inner"
277
+ # }
278
+ def show
279
+ self.output = Anoubis::Output::Edit.new
280
+ self.set_parent_model 'show'
281
+ self.output.tab = self.etc.tab.tab
282
+ if params.key?(:value) && params.key?(:field)
283
+ self.load_data_by_title params[:field], params[:value]
284
+ params[:id] = self.etc.data.data.id if self.etc.data.data
285
+ end
286
+ if params.key? :id
287
+ self.load_data_by_id params[:id] if !self.etc.data.data
288
+ if self.etc.data.data
289
+ self.output.values = self.get_data_row self.etc.data.data
290
+ if params.key? :time
291
+ self.output.options = self.get_data_options params[:time]
292
+ else
293
+ self.output.options = self.get_data_options 0
294
+ end
295
+ self.output.fields = self.get_fields_properties if self.etc.time == 0
296
+ else
297
+ self.output.result = -2
298
+ end
299
+ else
300
+ self.output.result = -2
301
+ end
302
+ if self.output.result == 0
303
+ self.output.title = I18n.t(format('%s.show.form.title', params[:controller].sub('/', '.')), title: self.output.values[:sys_title],
304
+ default: I18n.t('anoubis.form.titles.show', title: self.output.values[:sys_title]))
305
+ end
306
+ self.before_output
307
+
308
+ render json: around_output(output.to_h)
309
+ end
310
+
311
+ ##
312
+ # <i>New</i> action of {DataController}. Procedure outputs default values for create form.
313
+ # Authorization bearer is required.
314
+ #
315
+ # <b>API request:</b>
316
+ # GET /api/<version>/<controller>/new
317
+ #
318
+ # <b>Request Header:</b>
319
+ # {
320
+ # "Authorization": "Bearer <Session token>"
321
+ # }
322
+ #
323
+ # <b>Parameters:</b>
324
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
325
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
326
+ #
327
+ # Resulting output title is took from translation file <lang>.yml at path:
328
+ # <lang>:
329
+ # <controller name divided by level>:
330
+ # new:
331
+ # form:
332
+ # title: "Add new soldier"
333
+ #
334
+ # If this path isn't defined in translation file, then value is took from path:
335
+ # <lang>:
336
+ # anubis:
337
+ # form:
338
+ # titles:
339
+ # new: "Add new element"
340
+ #
341
+ # <b>Request example:</b>
342
+ # curl --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/new?tab=<tab>
343
+ #
344
+ # <b>Results:</b>
345
+ #
346
+ # Resulting data returns in JSON format.
347
+ #
348
+ # <b>Examples:</b>
349
+ #
350
+ # <b>Success:</b> HTTP response code 200
351
+ # {
352
+ # "result": 0,
353
+ # "message": "Successful",
354
+ # "timestamp": 1563271417,
355
+ # "tab": "inner",
356
+ # "title": "Add new soldier",
357
+ # "values": {
358
+ # "title": "",
359
+ # "name": "",
360
+ # "state_view": "Inner Senshi",
361
+ # "state": "inner"
362
+ # },
363
+ # "options": {
364
+ # "state": {
365
+ # "inner": "Inner Senshi",
366
+ # "outer": "Outer Senshi",
367
+ # "star": "Sailor Star"
368
+ # }
369
+ # }
370
+ # }
371
+ #
372
+ #
373
+ # <b>Error (session expired):</b> HTTP response code 422
374
+ # {
375
+ # "result": -1,
376
+ # "message": "Session expired",
377
+ # "timestamp": 1563271417,
378
+ # "tab": "inner"
379
+ # }
380
+ def new
381
+ new_action_skeleton 'new'
382
+ end
383
+
384
+ def new_action_skeleton(action)
385
+ self.output = Anoubis::Output::Edit.new
386
+ self.set_parent_model action
387
+ self.output.tab = self.etc.tab.tab
388
+ if self.etc.tab.buttons.key? action.to_sym
389
+ self.load_new_data action
390
+ if etc.data.data
391
+ self.output.values = get_data_row etc.data.data
392
+ if params.key? :time
393
+ self.output.options = get_data_options params[:time]
394
+ else
395
+ self.output.options = self.get_data_options 0
396
+ end
397
+ etc.action = 'new'
398
+ self.output.fields = self.get_fields_properties if self.etc.time == 0
399
+ etc.action = action
400
+ else
401
+ self.output.result = -2
402
+ end
403
+ else
404
+ self.output.result = -1
405
+ end
406
+ if self.output.result == 0
407
+ self.output.title = I18n.t(format('%s.%s.form.title', params[:controller].sub('/', '.'), action), default: I18n.t('anoubis.form.titles.new'))
408
+ end
409
+ self.before_output
410
+
411
+ render json: around_output(output.to_h)
412
+ end
413
+
414
+ ##
415
+ # <i>Create</i> action of {DataController}. Procedure inserts data into database.
416
+ # Authorization bearer is required.
417
+ #
418
+ # <b>API request:</b>
419
+ # POST /api/<version>/<controller>/new
420
+ #
421
+ # <b>Request Header:</b>
422
+ # {
423
+ # "Authorization": "Bearer <Session token>"
424
+ # }
425
+ #
426
+ # <b>Parameters:</b>
427
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
428
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
429
+ # - <b>data</b> (String) --- inserted data <i>(required value)</i>
430
+ #
431
+ # <b>Request example:</b>
432
+ # curl --request POST --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' --data='{"title": "Sailor Mars", "name": "Rey Hino", "state": "inner"}' http://<server>:<port>/api/<api-version>/<controller>/?tab=<tab>
433
+ #
434
+ # <b>Results:</b>
435
+ #
436
+ # Resulting data returns in JSON format.
437
+ #
438
+ # <b>Examples:</b>
439
+ #
440
+ # <b>Success:</b> HTTP response code 200
441
+ # {
442
+ # "result": 0,
443
+ # "message": "Successful",
444
+ # "timestamp": 1563271417,
445
+ # "tab": "inner",
446
+ # "values": {
447
+ # "id": 3,
448
+ # "sys_title": "Sailor Mars",
449
+ # "actions": {
450
+ # "edit": "Edit: Sailor Mars",
451
+ # "delete": "Delete: Sailor Mars"
452
+ # },
453
+ # "title": "Sailor Mars",
454
+ # "name": "Rey Hino",
455
+ # "state": "Inner Senshi",
456
+ # "raw_state": "inner"
457
+ # },
458
+ # "action": ""
459
+ # }
460
+ #
461
+ #
462
+ # <b>Error (data presents):</b> HTTP response code 200
463
+ # {
464
+ # "result": -3,
465
+ # "message": "Error update data",
466
+ # "timestamp": 1563271417,
467
+ # "tab": "inner",
468
+ # "errors": [
469
+ # "Title already presents"
470
+ # ]
471
+ # }
472
+ #
473
+ #
474
+ # <b>Error (session expired):</b> HTTP response code 422
475
+ # {
476
+ # "result": -1,
477
+ # "message": "Session expired",
478
+ # "timestamp": 1563271417,
479
+ # "tab": "inner"
480
+ # }
481
+ def create
482
+ self.output = Anoubis::Output::Update.new
483
+ self.set_parent_model 'create'
484
+ self.output.tab = self.etc.tab.tab
485
+ if params.key? :data
486
+ if self.etc.tab.buttons.key? :new
487
+ self.load_new_data
488
+ if self.etc.data.data
489
+ self.setup_fields
490
+ data = get_permited_params
491
+
492
+ data = self.before_create_data data
493
+
494
+ if data
495
+ data.each_key do |key|
496
+ self.convert_view_to_db_value key, data[key]
497
+ end
498
+
499
+ if self.etc.data.data.respond_to? :tenant_id
500
+ if self.current_user.respond_to? :tenant_id
501
+ self.etc.data.data.tenant_id = self.current_user.tenant_id if !self.etc.data.data.tenant_id
502
+ end
503
+ end
504
+
505
+ if self.etc.data.data.save
506
+ else
507
+ self.output.errors.concat self.etc.data.data.errors.full_messages
508
+ end
509
+ else
510
+ self.output.result = -4
511
+ end
512
+
513
+ if self.output.errors.length == 0
514
+ self.etc.data.fields = nil
515
+ self.set_new_action 'index'
516
+ self.output.values = self.get_data_row self.etc.data.data
517
+ self.set_new_action 'create'
518
+ self.after_create_data
519
+ else
520
+ self.output.result = -3
521
+ end
522
+ else
523
+ self.output.result = -2
524
+ end
525
+ else
526
+ self.output.result = -1
527
+ end
528
+ else
529
+ self.output.result = -2
530
+ end
531
+ self.before_output
532
+
533
+ render json: around_output(output.to_h)
534
+ end
535
+
536
+ ##
537
+ # <i>Edit</i> action of {DataController}. Procedure outputs values for edit form.
538
+ # Authorization bearer is required.
539
+ #
540
+ # <b>API request:</b>
541
+ # GET /api/<version>/<controller>/<id>/edit
542
+ #
543
+ # <b>Request Header:</b>
544
+ # {
545
+ # "Authorization": "Bearer <Session token>"
546
+ # }
547
+ #
548
+ # <b>Parameters:</b>
549
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
550
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
551
+ #
552
+ # Resulting output title is took from translation file <lang>.yml at path:
553
+ # <lang>:
554
+ # <controller name divided by level>:
555
+ # edit:
556
+ # form:
557
+ # title: "Edit soldier %{title}"
558
+ #
559
+ # If this path isn't defined in translation file, then value is took from path:
560
+ # <lang>:
561
+ # anubis:
562
+ # form:
563
+ # titles:
564
+ # edit: "Edit element: %{title}"
565
+ #
566
+ # <b>Request example:</b>
567
+ # curl --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/<id>/edit?tab=<tab>
568
+ #
569
+ # <b>Results:</b>
570
+ #
571
+ # Resulting data returns in JSON format.
572
+ #
573
+ # <b>Examples:</b>
574
+ #
575
+ # <b>Success:</b> HTTP response code 200
576
+ # {
577
+ # "result": 0,
578
+ # "message": "Successful",
579
+ # "timestamp": 1563271417,
580
+ # "tab": "inner",
581
+ # "title": "Edit soldier Sailor Mars",
582
+ # "values": {
583
+ # "id": 3,
584
+ # "title": "Sailor Mars",
585
+ # "name": "Rey Hino",
586
+ # "state_view": "Inner Senshi",
587
+ # "state": "inner"
588
+ # },
589
+ # "options": {
590
+ # "state": {
591
+ # "inner": "Inner Senshi",
592
+ # "outer": "Outer Senshi",
593
+ # "star": "Sailor Star"
594
+ # }
595
+ # }
596
+ # }
597
+ #
598
+ #
599
+ # <b>Error (incorrect request id):</b> HTTP response code 200
600
+ # {
601
+ # "result": -2,
602
+ # "message": "Incorrect request parameters",
603
+ # "timestamp": 1563271417,
604
+ # "tab": "inner"
605
+ # }
606
+ #
607
+ #
608
+ # <b>Error (session expired):</b> HTTP response code 422
609
+ # {
610
+ # "result": -1,
611
+ # "message": "Session expired",
612
+ # "timestamp": 1563271417,
613
+ # "tab": "inner"
614
+ # }
615
+ def edit
616
+ self.output = Anoubis::Output::Edit.new
617
+ self.set_parent_model 'edit'
618
+ self.output.tab = self.etc.tab.tab
619
+ if self.table_actions.include?('edit')
620
+ if params.key?(:value) && params.key?(:field)
621
+ self.load_data_by_title params[:field], params[:value]
622
+ params[:id] = self.etc.data.data.id if self.etc.data.data
623
+ end
624
+ if params.key? :id
625
+ self.load_data_by_id params[:id] if !self.etc.data.data
626
+ if self.etc.data.data
627
+ self.output.values = self.get_data_row self.etc.data.data
628
+ if params.key? :time
629
+ self.output.options = self.get_data_options params[:time]
630
+ else
631
+ self.output.options = self.get_data_options 0
632
+ end
633
+ self.output.fields = self.get_fields_properties if self.etc.time == 0
634
+ else
635
+ self.output.result = -2
636
+ end
637
+ else
638
+ self.output.result = -2
639
+ end
640
+ else
641
+ self.output.result = -1
642
+ end
643
+ if self.output.result == 0
644
+ self.output.title = I18n.t(format('%s.edit.form.title', params[:controller].sub('/', '.')), title: self.output.values[:sys_title],
645
+ default: I18n.t('anoubis.form.titles.edit', title: self.output.values[:sys_title]))
646
+ end
647
+ self.before_output
648
+
649
+ render json: around_output(output.to_h)
650
+ end
651
+
652
+ ##
653
+ # <i>Update</i> action of {DataController}. Procedure updates data in database.
654
+ # Authorization bearer is required.
655
+ #
656
+ # <b>API request:</b>
657
+ # PUT /api/<version>/<controller>/<id>/
658
+ #
659
+ # <b>Request Header:</b>
660
+ # {
661
+ # "Authorization": "Bearer <Session token>"
662
+ # }
663
+ #
664
+ # <b>Parameters:</b>
665
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
666
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
667
+ # - <b>data</b> (String) --- inserted data <i>(required value)</i>
668
+ #
669
+ # <b>Request example:</b>
670
+ # curl --request PUT --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' --data='{"title": "Sailor Mars", "name": "Rey Hino", "state": "inner"}' http://<server>:<port>/api/<api-version>/<controller>/<id>/?tab=<tab>
671
+ #
672
+ # <b>Results:</b>
673
+ #
674
+ # Resulting data returns in JSON format.
675
+ #
676
+ # <b>Examples:</b>
677
+ #
678
+ # <b>Success:</b> HTTP response code 200
679
+ # {
680
+ # "result": 0,
681
+ # "message": "Successful",
682
+ # "timestamp": 1563271417,
683
+ # "tab": "inner",
684
+ # "values": {
685
+ # "id": 3,
686
+ # "sys_title": "Sailor Mars",
687
+ # "actions": {
688
+ # "edit": "Edit: Sailor Mars",
689
+ # "delete": "Delete: Sailor Mars"
690
+ # },
691
+ # "title": "Sailor Mars",
692
+ # "name": "Rey Hino",
693
+ # "state": "Inner Senshi",
694
+ # "raw_state": "inner"
695
+ # },
696
+ # "action": ""
697
+ # }
698
+ #
699
+ #
700
+ # <b>Error (data presents):</b> HTTP response code 200
701
+ # {
702
+ # "result": -3,
703
+ # "message": "Error update data",
704
+ # "timestamp": 1563271417,
705
+ # "tab": "inner",
706
+ # "errors": [
707
+ # "Title already presents"
708
+ # ]
709
+ # }
710
+ #
711
+ #
712
+ # <b>Error (session expired):</b> HTTP response code 422
713
+ # {
714
+ # "result": -1,
715
+ # "message": "Session expired",
716
+ # "timestamp": 1563271417,
717
+ # "tab": "inner"
718
+ # }
719
+ def update
720
+ self.output = Anoubis::Output::Update.new
721
+ self.set_parent_model 'update'
722
+ self.output.tab = self.etc.tab.tab
723
+ if self.table_actions.include?('edit')
724
+ if params.key?(:id) && params.key?(:data)
725
+ self.load_data_by_id params[:id]
726
+ if self.etc.data.data
727
+ self.setup_fields
728
+ data = get_permited_params
729
+
730
+ data = self.before_update_data data
731
+
732
+ if data
733
+ data.each_key do |key|
734
+ self.convert_view_to_db_value key, data[key]
735
+ end
736
+
737
+ if self.etc.data.data.save
738
+ else
739
+ self.output.errors.concat self.etc.data.data.errors.full_messages
740
+ end
741
+
742
+ if self.output.errors.length == 0
743
+ self.etc.data.fields = nil
744
+ self.set_new_action 'index'
745
+ self.output.values = self.get_data_row self.etc.data.data
746
+ self.output.action = 'refresh' if self.etc.data.data.need_refresh
747
+ self.set_new_action 'update'
748
+ self.after_update_data
749
+ else
750
+ self.output.result = -3
751
+ end
752
+ end
753
+ else
754
+ self.output.result = -4
755
+ end
756
+ else
757
+ self.output.result = -2
758
+ end
759
+ else
760
+ self.output.result = -1
761
+ end
762
+ self.before_output
763
+
764
+ render json: around_output(output.to_h)
765
+ end
766
+
767
+ ##
768
+ # <i>Destroy</i> action of {DataController}. Procedure deletes data from database.
769
+ # Authorization bearer is required.
770
+ #
771
+ # <b>API request:</b>
772
+ # DELETE /api/<version>/<controller>/<id>/
773
+ #
774
+ # <b>Request Header:</b>
775
+ # {
776
+ # "Authorization": "Bearer <Session token>"
777
+ # }
778
+ #
779
+ # <b>Parameters:</b>
780
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
781
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
782
+ #
783
+ # <b>Request example:</b>
784
+ # curl --request DELETE --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/<id>/?tab=<tab>
785
+ #
786
+ # <b>Results:</b>
787
+ #
788
+ # Resulting data returns in JSON format.
789
+ #
790
+ # <b>Examples:</b>
791
+ #
792
+ # <b>Success:</b> HTTP response code 200
793
+ # {
794
+ # "result": 0,
795
+ # "message": "Successful",
796
+ # "timestamp": 1563271417,
797
+ # "tab": "inner"
798
+ # }
799
+ #
800
+ #
801
+ # <b>Error (incorrect request id):</b> HTTP response code 200
802
+ # {
803
+ # "result": -2,
804
+ # "message": "Incorrect request parameters",
805
+ # "timestamp": 1563271417,
806
+ # "tab": "inner"
807
+ # }
808
+ #
809
+ #
810
+ # <b>Error (session expired):</b> HTTP response code 422
811
+ # {
812
+ # "result": -1,
813
+ # "message": "Session expired",
814
+ # "timestamp": 1563271417,
815
+ # "tab": "inner"
816
+ # }
817
+ def destroy
818
+ self.output = Anoubis::Output::Delete.new
819
+ self.set_parent_model 'destroy'
820
+ self.output.tab = self.etc.tab.tab
821
+ if self.etc.tab.buttons.key? :delete
822
+ if params.key?(:value) && params.key?(:field)
823
+ self.load_data_by_title params[:field], params[:value]
824
+ params[:id] = self.etc.data.data.id if self.etc.data.data
825
+ end
826
+ if params.key?(:id)
827
+ self.load_data_by_id params[:id] if !self.etc.data.data
828
+ if self.etc.data.data
829
+ self.output.id = self.etc.data.data.id
830
+ if self.etc.data.data.can_delete( { tab: self.etc.tab } )
831
+ self.destroy_data
832
+ else
833
+ self.output.result = -1
834
+ end
835
+ else
836
+ self.output.result = -2
837
+ end
838
+ else
839
+ self.output.result = -2
840
+ end
841
+ else
842
+ self.output.result = -1
843
+ end
844
+ self.before_output
845
+
846
+ render json: around_output(output.to_h)
847
+ end
848
+
849
+ ##
850
+ # Returns autocomplete data
851
+ def autocomplete
852
+ self.output = Anoubis::Output::Autocomplete.new
853
+ self.output.result = -1
854
+ self.set_parent_model 'autocomplete'
855
+ self.output.tab = self.etc.tab.tab
856
+ if params.key?(:field) && params.key?(:value)
857
+ self.setup_fields
858
+ if self.etc.data.fields
859
+ if self.etc.data.fields.key? params[:field].to_s.to_sym
860
+ field = self.etc.data.fields[params[:field].to_s.to_sym]
861
+ #puts 'autocomplete'
862
+ #puts field.to_h
863
+ if field.autocomplete
864
+ self.output.result = 0
865
+ self.get_autocomplete_data field, params[:value]
866
+ #puts field.to_h
867
+ end
868
+ end
869
+ end
870
+ end
871
+ self.before_output
872
+
873
+ render json: around_output(output.to_h)
874
+ end
875
+
876
+ ##
877
+ # Export data from database
878
+ def export
879
+ self.etc.data = Anoubis::Etc::Data.new
880
+ self.set_parent_model 'export'
881
+ self.output = Anoubis::Output::Data.new
882
+ if self.etc.tab.export
883
+ self.output.count = self.get_table_data_count
884
+ count = (self.output.count / 40).to_i + 1
885
+ self.setup_fields
886
+
887
+ self.exports = Anoubis::Export.new format: self.export_format, fields: self.get_fields_properties
888
+
889
+ case self.exports.format
890
+ when 'xls'
891
+ headers['Content-Disposition'] = 'attachment; filename="export.xlsx" filename*="export.xlsx"'
892
+ end
893
+
894
+ self.etc.data.limit = 40
895
+ self.etc.data.offset = 0
896
+ self.output.data = self.get_table_data
897
+ self.after_get_table_data
898
+ self.before_output
899
+ self.exports.add self.output.data
900
+
901
+ if count > 1
902
+ for i in 2..count
903
+ self.etc.data.offset = (i-1)*40
904
+ self.output.data = self.get_table_data
905
+ self.after_get_table_data
906
+ self.before_output
907
+ self.exports.add self.output.data
908
+ end
909
+ end
910
+
911
+ respond_to do |format|
912
+ case self.exports.format
913
+ when 'xls'
914
+ format.xlsx {
915
+ send_data self.render_xls_file, type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
916
+ }
917
+ end
918
+ end
919
+ else
920
+ respond_to do |format|
921
+ format.any { render json: {result: -1}, status: :unprocessable_entity }
922
+ end
923
+
924
+ end
925
+ end
926
+
927
+ ##
928
+ # Returns rendered xlsx data
929
+ def render_xls_file
930
+ Axlsx::Package.new do |p|
931
+ wb = p.workbook
932
+ wb.styles do |s|
933
+ default = s.add_style :sz => 11, :font_name => "Calibri", :alignment => {:vertical => :center}
934
+ default_bold = s.add_style :sz => 11, :font_name => "Calibri", :alignment => {:vertical => :center}, :b => true
935
+ wb.add_worksheet(name: 'Data') do |sheet|
936
+ sheet.add_row self.exports.title, :style => default_bold
937
+ self.exports.data.each do |data|
938
+ sheet.add_row data, :style => default
939
+ end
940
+ end
941
+ end
942
+ return p.to_stream().read
943
+ end
944
+ end
945
+ end
946
+ end
947
+ end