anoubis 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/anoubis/application_controller.rb +4 -0
  3. data/app/controllers/anoubis/data/actions.rb +956 -0
  4. data/app/controllers/anoubis/data/callbacks.rb +66 -0
  5. data/app/controllers/anoubis/data/convert.rb +405 -0
  6. data/app/controllers/anoubis/data/defaults.rb +215 -0
  7. data/app/controllers/anoubis/data/get.rb +529 -0
  8. data/app/controllers/anoubis/data/load.rb +87 -0
  9. data/app/controllers/anoubis/data/set.rb +47 -0
  10. data/app/controllers/anoubis/data/setup.rb +102 -0
  11. data/app/controllers/anoubis/data_controller.rb +17 -1
  12. data/app/controllers/anoubis/output/basic.rb +1 -1
  13. data/app/controllers/anoubis/sso/server/login_controller.rb +5 -5
  14. data/app/controllers/anoubis/sso/server/user_controller.rb +2 -2
  15. data/app/models/anoubis/application_record.rb +7 -0
  16. data/config/locales/ru.yml +3 -2
  17. data/lib/anoubis/version.rb +1 -1
  18. metadata +11 -30
  19. data/app/controllers/anoubis/core/data/actions.rb +0 -962
  20. data/app/controllers/anoubis/core/data/callbacks.rb +0 -68
  21. data/app/controllers/anoubis/core/data/convert.rb +0 -407
  22. data/app/controllers/anoubis/core/data/defaults.rb +0 -217
  23. data/app/controllers/anoubis/core/data/get.rb +0 -531
  24. data/app/controllers/anoubis/core/data/load.rb +0 -89
  25. data/app/controllers/anoubis/core/data/set.rb +0 -49
  26. data/app/controllers/anoubis/core/data/setup.rb +0 -104
  27. data/app/controllers/anoubis/core/data_controller.rb +0 -28
  28. data/app/controllers/anoubis/sso/client/data/actions.rb +0 -5
  29. data/app/controllers/anoubis/sso/client/data/callbacks.rb +0 -5
  30. data/app/controllers/anoubis/sso/client/data/convert.rb +0 -5
  31. data/app/controllers/anoubis/sso/client/data/defaults.rb +0 -5
  32. data/app/controllers/anoubis/sso/client/data/get.rb +0 -5
  33. data/app/controllers/anoubis/sso/client/data/load.rb +0 -26
  34. data/app/controllers/anoubis/sso/client/data/set.rb +0 -5
  35. data/app/controllers/anoubis/sso/client/data/setup.rb +0 -5
  36. data/app/controllers/anoubis/sso/client/data_controller.rb +0 -21
  37. data/app/controllers/anoubis/tenant/data/actions.rb +0 -11
  38. data/app/controllers/anoubis/tenant/data/callbacks.rb +0 -11
  39. data/app/controllers/anoubis/tenant/data/convert.rb +0 -11
  40. data/app/controllers/anoubis/tenant/data/defaults.rb +0 -11
  41. data/app/controllers/anoubis/tenant/data/get.rb +0 -11
  42. data/app/controllers/anoubis/tenant/data/load.rb +0 -52
  43. data/app/controllers/anoubis/tenant/data/set.rb +0 -11
  44. data/app/controllers/anoubis/tenant/data/setup.rb +0 -11
  45. data/app/controllers/anoubis/tenant/data_controller.rb +0 -28
@@ -0,0 +1,956 @@
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(self.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: self.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('anubis.form.titles.show', title: self.output.values[:sys_title]))
305
+ end
306
+ self.before_output
307
+ respond_to do |format|
308
+ format.json { render json: self.output.to_h }
309
+ end
310
+ end
311
+
312
+ ##
313
+ # <i>New</i> action of {DataController}. Procedure outputs default values for create form.
314
+ # Authorization bearer is required.
315
+ #
316
+ # <b>API request:</b>
317
+ # GET /api/<version>/<controller>/new
318
+ #
319
+ # <b>Request Header:</b>
320
+ # {
321
+ # "Authorization": "Bearer <Session token>"
322
+ # }
323
+ #
324
+ # <b>Parameters:</b>
325
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
326
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
327
+ #
328
+ # Resulting output title is took from translation file <lang>.yml at path:
329
+ # <lang>:
330
+ # <controller name divided by level>:
331
+ # new:
332
+ # form:
333
+ # title: "Add new soldier"
334
+ #
335
+ # If this path isn't defined in translation file, then value is took from path:
336
+ # <lang>:
337
+ # anubis:
338
+ # form:
339
+ # titles:
340
+ # new: "Add new element"
341
+ #
342
+ # <b>Request example:</b>
343
+ # curl --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/new?tab=<tab>
344
+ #
345
+ # <b>Results:</b>
346
+ #
347
+ # Resulting data returns in JSON format.
348
+ #
349
+ # <b>Examples:</b>
350
+ #
351
+ # <b>Success:</b> HTTP response code 200
352
+ # {
353
+ # "result": 0,
354
+ # "message": "Successful",
355
+ # "timestamp": 1563271417,
356
+ # "tab": "inner",
357
+ # "title": "Add new soldier",
358
+ # "values": {
359
+ # "title": "",
360
+ # "name": "",
361
+ # "state_view": "Inner Senshi",
362
+ # "state": "inner"
363
+ # },
364
+ # "options": {
365
+ # "state": {
366
+ # "inner": "Inner Senshi",
367
+ # "outer": "Outer Senshi",
368
+ # "star": "Sailor Star"
369
+ # }
370
+ # }
371
+ # }
372
+ #
373
+ #
374
+ # <b>Error (session expired):</b> HTTP response code 422
375
+ # {
376
+ # "result": -1,
377
+ # "message": "Session expired",
378
+ # "timestamp": 1563271417,
379
+ # "tab": "inner"
380
+ # }
381
+ def new
382
+ new_action_skeleton 'new'
383
+ end
384
+
385
+ def new_action_skeleton(action)
386
+ self.output = Anoubis::Output::Edit.new
387
+ self.set_parent_model action
388
+ self.output.tab = self.etc.tab.tab
389
+ if self.etc.tab.buttons.key? action.to_sym
390
+ self.load_new_data action
391
+ if etc.data.data
392
+ self.output.values = get_data_row etc.data.data
393
+ if params.key? :time
394
+ self.output.options = get_data_options params[:time]
395
+ else
396
+ self.output.options = self.get_data_options 0
397
+ end
398
+ etc.action = 'new'
399
+ self.output.fields = self.get_fields_properties if self.etc.time == 0
400
+ etc.action = action
401
+ else
402
+ self.output.result = -2
403
+ end
404
+ else
405
+ self.output.result = -1
406
+ end
407
+ if self.output.result == 0
408
+ self.output.title = I18n.t(format('%s.%s.form.title', params[:controller].sub('/', '.'), action), default: I18n.t('anubis.form.titles.new'))
409
+ end
410
+ self.before_output
411
+
412
+
413
+ respond_to do |format|
414
+ format.json { render json: self.output.to_h }
415
+ end
416
+ end
417
+
418
+ ##
419
+ # <i>Create</i> action of {DataController}. Procedure inserts data into database.
420
+ # Authorization bearer is required.
421
+ #
422
+ # <b>API request:</b>
423
+ # POST /api/<version>/<controller>/new
424
+ #
425
+ # <b>Request Header:</b>
426
+ # {
427
+ # "Authorization": "Bearer <Session token>"
428
+ # }
429
+ #
430
+ # <b>Parameters:</b>
431
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
432
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
433
+ # - <b>data</b> (String) --- inserted data <i>(required value)</i>
434
+ #
435
+ # <b>Request example:</b>
436
+ # 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>
437
+ #
438
+ # <b>Results:</b>
439
+ #
440
+ # Resulting data returns in JSON format.
441
+ #
442
+ # <b>Examples:</b>
443
+ #
444
+ # <b>Success:</b> HTTP response code 200
445
+ # {
446
+ # "result": 0,
447
+ # "message": "Successful",
448
+ # "timestamp": 1563271417,
449
+ # "tab": "inner",
450
+ # "values": {
451
+ # "id": 3,
452
+ # "sys_title": "Sailor Mars",
453
+ # "actions": {
454
+ # "edit": "Edit: Sailor Mars",
455
+ # "delete": "Delete: Sailor Mars"
456
+ # },
457
+ # "title": "Sailor Mars",
458
+ # "name": "Rey Hino",
459
+ # "state": "Inner Senshi",
460
+ # "raw_state": "inner"
461
+ # },
462
+ # "action": ""
463
+ # }
464
+ #
465
+ #
466
+ # <b>Error (data presents):</b> HTTP response code 200
467
+ # {
468
+ # "result": -3,
469
+ # "message": "Error update data",
470
+ # "timestamp": 1563271417,
471
+ # "tab": "inner",
472
+ # "errors": [
473
+ # "Title already presents"
474
+ # ]
475
+ # }
476
+ #
477
+ #
478
+ # <b>Error (session expired):</b> HTTP response code 422
479
+ # {
480
+ # "result": -1,
481
+ # "message": "Session expired",
482
+ # "timestamp": 1563271417,
483
+ # "tab": "inner"
484
+ # }
485
+ def create
486
+ self.output = Anoubis::Output::Update.new
487
+ self.set_parent_model 'create'
488
+ self.output.tab = self.etc.tab.tab
489
+ if params.key? :data
490
+ if self.etc.tab.buttons.key? :new
491
+ self.load_new_data
492
+ if self.etc.data.data
493
+ self.setup_fields
494
+ data = get_permited_params
495
+
496
+ data = self.before_create_data data
497
+
498
+ if data
499
+ data.each_key do |key|
500
+ self.convert_view_to_db_value key, data[key]
501
+ end
502
+
503
+ if self.etc.data.data.respond_to? :tenant_id
504
+ if self.current_user.respond_to? :tenant_id
505
+ self.etc.data.data.tenant_id = self.current_user.tenant_id if !self.etc.data.data.tenant_id
506
+ end
507
+ end
508
+
509
+ if self.etc.data.data.save
510
+ else
511
+ self.output.errors.concat self.etc.data.data.errors.full_messages
512
+ end
513
+ else
514
+ self.output.result = -4
515
+ end
516
+
517
+ if self.output.errors.length == 0
518
+ self.etc.data.fields = nil
519
+ self.set_new_action 'index'
520
+ self.output.values = self.get_data_row self.etc.data.data
521
+ self.set_new_action 'create'
522
+ self.after_create_data
523
+ else
524
+ self.output.result = -3
525
+ end
526
+ else
527
+ self.output.result = -2
528
+ end
529
+ else
530
+ self.output.result = -1
531
+ end
532
+ else
533
+ self.output.result = -2
534
+ end
535
+ self.before_output
536
+ respond_to do |format|
537
+ format.json { render json: self.output.to_h }
538
+ end
539
+ end
540
+
541
+ ##
542
+ # <i>Edit</i> action of {DataController}. Procedure outputs values for edit form.
543
+ # Authorization bearer is required.
544
+ #
545
+ # <b>API request:</b>
546
+ # GET /api/<version>/<controller>/<id>/edit
547
+ #
548
+ # <b>Request Header:</b>
549
+ # {
550
+ # "Authorization": "Bearer <Session token>"
551
+ # }
552
+ #
553
+ # <b>Parameters:</b>
554
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
555
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
556
+ #
557
+ # Resulting output title is took from translation file <lang>.yml at path:
558
+ # <lang>:
559
+ # <controller name divided by level>:
560
+ # edit:
561
+ # form:
562
+ # title: "Edit soldier %{title}"
563
+ #
564
+ # If this path isn't defined in translation file, then value is took from path:
565
+ # <lang>:
566
+ # anubis:
567
+ # form:
568
+ # titles:
569
+ # edit: "Edit element: %{title}"
570
+ #
571
+ # <b>Request example:</b>
572
+ # curl --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/<id>/edit?tab=<tab>
573
+ #
574
+ # <b>Results:</b>
575
+ #
576
+ # Resulting data returns in JSON format.
577
+ #
578
+ # <b>Examples:</b>
579
+ #
580
+ # <b>Success:</b> HTTP response code 200
581
+ # {
582
+ # "result": 0,
583
+ # "message": "Successful",
584
+ # "timestamp": 1563271417,
585
+ # "tab": "inner",
586
+ # "title": "Edit soldier Sailor Mars",
587
+ # "values": {
588
+ # "id": 3,
589
+ # "title": "Sailor Mars",
590
+ # "name": "Rey Hino",
591
+ # "state_view": "Inner Senshi",
592
+ # "state": "inner"
593
+ # },
594
+ # "options": {
595
+ # "state": {
596
+ # "inner": "Inner Senshi",
597
+ # "outer": "Outer Senshi",
598
+ # "star": "Sailor Star"
599
+ # }
600
+ # }
601
+ # }
602
+ #
603
+ #
604
+ # <b>Error (incorrect request id):</b> HTTP response code 200
605
+ # {
606
+ # "result": -2,
607
+ # "message": "Incorrect request parameters",
608
+ # "timestamp": 1563271417,
609
+ # "tab": "inner"
610
+ # }
611
+ #
612
+ #
613
+ # <b>Error (session expired):</b> HTTP response code 422
614
+ # {
615
+ # "result": -1,
616
+ # "message": "Session expired",
617
+ # "timestamp": 1563271417,
618
+ # "tab": "inner"
619
+ # }
620
+ def edit
621
+ self.output = Anoubis::Output::Edit.new
622
+ self.set_parent_model 'edit'
623
+ self.output.tab = self.etc.tab.tab
624
+ if self.table_actions.include?('edit')
625
+ if params.key?(:value) && params.key?(:field)
626
+ self.load_data_by_title params[:field], params[:value]
627
+ params[:id] = self.etc.data.data.id if self.etc.data.data
628
+ end
629
+ if params.key? :id
630
+ self.load_data_by_id params[:id] if !self.etc.data.data
631
+ if self.etc.data.data
632
+ self.output.values = self.get_data_row self.etc.data.data
633
+ if params.key? :time
634
+ self.output.options = self.get_data_options params[:time]
635
+ else
636
+ self.output.options = self.get_data_options 0
637
+ end
638
+ self.output.fields = self.get_fields_properties if self.etc.time == 0
639
+ else
640
+ self.output.result = -2
641
+ end
642
+ else
643
+ self.output.result = -2
644
+ end
645
+ else
646
+ self.output.result = -1
647
+ end
648
+ if self.output.result == 0
649
+ self.output.title = I18n.t(format('%s.edit.form.title', params[:controller].sub('/', '.')), title: self.output.values[:sys_title],
650
+ default: I18n.t('anubis.form.titles.edit', title: self.output.values[:sys_title]))
651
+ end
652
+ self.before_output
653
+ respond_to do |format|
654
+ format.json { render json: self.output.to_h }
655
+ end
656
+ end
657
+
658
+ ##
659
+ # <i>Update</i> action of {DataController}. Procedure updates data in database.
660
+ # Authorization bearer is required.
661
+ #
662
+ # <b>API request:</b>
663
+ # PUT /api/<version>/<controller>/<id>/
664
+ #
665
+ # <b>Request Header:</b>
666
+ # {
667
+ # "Authorization": "Bearer <Session token>"
668
+ # }
669
+ #
670
+ # <b>Parameters:</b>
671
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
672
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
673
+ # - <b>data</b> (String) --- inserted data <i>(required value)</i>
674
+ #
675
+ # <b>Request example:</b>
676
+ # 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>
677
+ #
678
+ # <b>Results:</b>
679
+ #
680
+ # Resulting data returns in JSON format.
681
+ #
682
+ # <b>Examples:</b>
683
+ #
684
+ # <b>Success:</b> HTTP response code 200
685
+ # {
686
+ # "result": 0,
687
+ # "message": "Successful",
688
+ # "timestamp": 1563271417,
689
+ # "tab": "inner",
690
+ # "values": {
691
+ # "id": 3,
692
+ # "sys_title": "Sailor Mars",
693
+ # "actions": {
694
+ # "edit": "Edit: Sailor Mars",
695
+ # "delete": "Delete: Sailor Mars"
696
+ # },
697
+ # "title": "Sailor Mars",
698
+ # "name": "Rey Hino",
699
+ # "state": "Inner Senshi",
700
+ # "raw_state": "inner"
701
+ # },
702
+ # "action": ""
703
+ # }
704
+ #
705
+ #
706
+ # <b>Error (data presents):</b> HTTP response code 200
707
+ # {
708
+ # "result": -3,
709
+ # "message": "Error update data",
710
+ # "timestamp": 1563271417,
711
+ # "tab": "inner",
712
+ # "errors": [
713
+ # "Title already presents"
714
+ # ]
715
+ # }
716
+ #
717
+ #
718
+ # <b>Error (session expired):</b> HTTP response code 422
719
+ # {
720
+ # "result": -1,
721
+ # "message": "Session expired",
722
+ # "timestamp": 1563271417,
723
+ # "tab": "inner"
724
+ # }
725
+ def update
726
+ self.output = Anoubis::Output::Update.new
727
+ self.set_parent_model 'update'
728
+ self.output.tab = self.etc.tab.tab
729
+ if self.table_actions.include?('edit')
730
+ if params.key?(:id) && params.key?(:data)
731
+ self.load_data_by_id params[:id]
732
+ if self.etc.data.data
733
+ self.setup_fields
734
+ data = get_permited_params
735
+
736
+ data = self.before_update_data data
737
+
738
+ if data
739
+ data.each_key do |key|
740
+ self.convert_view_to_db_value key, data[key]
741
+ end
742
+
743
+ if self.etc.data.data.save
744
+ else
745
+ self.output.errors.concat self.etc.data.data.errors.full_messages
746
+ end
747
+
748
+ if self.output.errors.length == 0
749
+ self.etc.data.fields = nil
750
+ self.set_new_action 'index'
751
+ self.output.values = self.get_data_row self.etc.data.data
752
+ self.output.action = 'refresh' if self.etc.data.data.need_refresh
753
+ self.set_new_action 'update'
754
+ self.after_update_data
755
+ else
756
+ self.output.result = -3
757
+ end
758
+ end
759
+ else
760
+ self.output.result = -4
761
+ end
762
+ else
763
+ self.output.result = -2
764
+ end
765
+ else
766
+ self.output.result = -1
767
+ end
768
+ self.before_output
769
+ respond_to do |format|
770
+ format.json { render json: self.output.to_h }
771
+ end
772
+ end
773
+
774
+ ##
775
+ # <i>Destroy</i> action of {DataController}. Procedure deletes data from database.
776
+ # Authorization bearer is required.
777
+ #
778
+ # <b>API request:</b>
779
+ # DELETE /api/<version>/<controller>/<id>/
780
+ #
781
+ # <b>Request Header:</b>
782
+ # {
783
+ # "Authorization": "Bearer <Session token>"
784
+ # }
785
+ #
786
+ # <b>Parameters:</b>
787
+ # - <b>locale</b> (String) --- the output language locale <i>(optional value)</i>
788
+ # - <b>tab</b> (String) --- the tab, is used for action <i>(optional value, default: first defined tab)</i>
789
+ #
790
+ # <b>Request example:</b>
791
+ # curl --request DELETE --header "Content-Type: application/json" --header 'Authorization: Bearer <session-token>' http://<server>:<port>/api/<api-version>/<controller>/<id>/?tab=<tab>
792
+ #
793
+ # <b>Results:</b>
794
+ #
795
+ # Resulting data returns in JSON format.
796
+ #
797
+ # <b>Examples:</b>
798
+ #
799
+ # <b>Success:</b> HTTP response code 200
800
+ # {
801
+ # "result": 0,
802
+ # "message": "Successful",
803
+ # "timestamp": 1563271417,
804
+ # "tab": "inner"
805
+ # }
806
+ #
807
+ #
808
+ # <b>Error (incorrect request id):</b> HTTP response code 200
809
+ # {
810
+ # "result": -2,
811
+ # "message": "Incorrect request parameters",
812
+ # "timestamp": 1563271417,
813
+ # "tab": "inner"
814
+ # }
815
+ #
816
+ #
817
+ # <b>Error (session expired):</b> HTTP response code 422
818
+ # {
819
+ # "result": -1,
820
+ # "message": "Session expired",
821
+ # "timestamp": 1563271417,
822
+ # "tab": "inner"
823
+ # }
824
+ def destroy
825
+ self.output = Anoubis::Output::Delete.new
826
+ self.set_parent_model 'destroy'
827
+ self.output.tab = self.etc.tab.tab
828
+ if self.etc.tab.buttons.key? :delete
829
+ if params.key?(:value) && params.key?(:field)
830
+ self.load_data_by_title params[:field], params[:value]
831
+ params[:id] = self.etc.data.data.id if self.etc.data.data
832
+ end
833
+ if params.key?(:id)
834
+ self.load_data_by_id params[:id] if !self.etc.data.data
835
+ if self.etc.data.data
836
+ self.output.id = self.etc.data.data.id
837
+ if self.etc.data.data.can_delete( { tab: self.etc.tab } )
838
+ self.destroy_data
839
+ else
840
+ self.output.result = -1
841
+ end
842
+ else
843
+ self.output.result = -2
844
+ end
845
+ else
846
+ self.output.result = -2
847
+ end
848
+ else
849
+ self.output.result = -1
850
+ end
851
+ self.before_output
852
+ respond_to do |format|
853
+ format.json { render json: self.output.to_h }
854
+ end
855
+ end
856
+
857
+ ##
858
+ # Returns autocomplete data
859
+ def autocomplete
860
+ self.output = Anoubis::Output::Autocomplete.new
861
+ self.output.result = -1
862
+ self.set_parent_model 'autocomplete'
863
+ self.output.tab = self.etc.tab.tab
864
+ if params.key?(:field) && params.key?(:value)
865
+ self.setup_fields
866
+ if self.etc.data.fields
867
+ if self.etc.data.fields.key? params[:field].to_s.to_sym
868
+ field = self.etc.data.fields[params[:field].to_s.to_sym]
869
+ #puts 'autocomplete'
870
+ #puts field.to_h
871
+ if field.autocomplete
872
+ self.output.result = 0
873
+ self.get_autocomplete_data field, params[:value]
874
+ #puts field.to_h
875
+ end
876
+ end
877
+ end
878
+ end
879
+ self.before_output
880
+ respond_to do |format|
881
+ format.json { render json: self.output.to_h }
882
+ end
883
+ end
884
+
885
+ ##
886
+ # Export data from database
887
+ def export
888
+ self.etc.data = Anoubis::Etc::Data.new
889
+ self.set_parent_model 'export'
890
+ self.output = Anoubis::Output::Data.new
891
+ if self.etc.tab.export
892
+ self.output.count = self.get_table_data_count
893
+ count = (self.output.count / 40).to_i + 1
894
+ self.setup_fields
895
+
896
+ self.exports = Anoubis::Export.new format: self.export_format, fields: self.get_fields_properties
897
+
898
+ case self.exports.format
899
+ when 'xls'
900
+ headers['Content-Disposition'] = 'attachment; filename="export.xlsx" filename*="export.xlsx"'
901
+ end
902
+
903
+ self.etc.data.limit = 40
904
+ self.etc.data.offset = 0
905
+ self.output.data = self.get_table_data
906
+ self.after_get_table_data
907
+ self.before_output
908
+ self.exports.add self.output.data
909
+
910
+ if count > 1
911
+ for i in 2..count
912
+ self.etc.data.offset = (i-1)*40
913
+ self.output.data = self.get_table_data
914
+ self.after_get_table_data
915
+ self.before_output
916
+ self.exports.add self.output.data
917
+ end
918
+ end
919
+
920
+ respond_to do |format|
921
+ case self.exports.format
922
+ when 'xls'
923
+ format.xlsx {
924
+ send_data self.render_xls_file, type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
925
+ }
926
+ end
927
+ end
928
+ else
929
+ respond_to do |format|
930
+ format.any { render json: {result: -1}, status: :unprocessable_entity }
931
+ end
932
+
933
+ end
934
+ end
935
+
936
+ ##
937
+ # Returns rendered xlsx data
938
+ def render_xls_file
939
+ Axlsx::Package.new do |p|
940
+ wb = p.workbook
941
+ wb.styles do |s|
942
+ default = s.add_style :sz => 11, :font_name => "Calibri", :alignment => {:vertical => :center}
943
+ default_bold = s.add_style :sz => 11, :font_name => "Calibri", :alignment => {:vertical => :center}, :b => true
944
+ wb.add_worksheet(name: 'Data') do |sheet|
945
+ sheet.add_row self.exports.title, :style => default_bold
946
+ self.exports.data.each do |data|
947
+ sheet.add_row data, :style => default
948
+ end
949
+ end
950
+ end
951
+ return p.to_stream().read
952
+ end
953
+ end
954
+ end
955
+ end
956
+ end