anoubis 1.0.0

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