hippo-fw 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (535) hide show
  1. checksums.yaml +7 -0
  2. data/.dir-locals.el +8 -0
  3. data/.eslintrc.js +7 -0
  4. data/.flowconfig +2 -0
  5. data/.gitignore +22 -0
  6. data/.jshintrc +3 -0
  7. data/.rubocop.yml +9 -0
  8. data/.ruby-version +1 -0
  9. data/.travis.yml +24 -0
  10. data/Gemfile +17 -0
  11. data/Guardfile +19 -0
  12. data/LICENSE-MIT.txt +21 -0
  13. data/README.md +36 -0
  14. data/Rakefile +39 -0
  15. data/bin/hippo +5 -0
  16. data/client/extension.js +0 -0
  17. data/client/hippo/__mocks__/config.js +17 -0
  18. data/client/hippo/access/index.js +4 -0
  19. data/client/hippo/access/login-dialog.jsx +38 -0
  20. data/client/hippo/access/styles.scss +0 -0
  21. data/client/hippo/boot.jsx +41 -0
  22. data/client/hippo/components/asset.jsx +81 -0
  23. data/client/hippo/components/asset.scss +15 -0
  24. data/client/hippo/components/calendar/Calendar.jsx +25 -0
  25. data/client/hippo/components/calendar/index.js +3 -0
  26. data/client/hippo/components/calendar/styles.scss +3 -0
  27. data/client/hippo/components/data-list.jsx +105 -0
  28. data/client/hippo/components/data-table.jsx +243 -0
  29. data/client/hippo/components/data-table/header-cell.jsx +94 -0
  30. data/client/hippo/components/data-table/table-styles.scss +34 -0
  31. data/client/hippo/components/enabled.js.erb +5 -0
  32. data/client/hippo/components/field-validation.js +7 -0
  33. data/client/hippo/components/form.jsx +65 -0
  34. data/client/hippo/components/form/field-prop-type.js +16 -0
  35. data/client/hippo/components/form/fields.jsx +76 -0
  36. data/client/hippo/components/form/fields/checkbox-wrapper.jsx +21 -0
  37. data/client/hippo/components/form/fields/date-wrapper.jsx +49 -0
  38. data/client/hippo/components/form/fields/form-field.scss +4 -0
  39. data/client/hippo/components/form/fields/select-wrapper.jsx +31 -0
  40. data/client/hippo/components/form/fields/text-wrapper.jsx +20 -0
  41. data/client/hippo/components/form/model.js +95 -0
  42. data/client/hippo/components/form/validations.js +0 -0
  43. data/client/hippo/components/form/wrapper.jsx +40 -0
  44. data/client/hippo/components/grid/config.json +3 -0
  45. data/client/hippo/components/grid/editors.scss +78 -0
  46. data/client/hippo/components/grid/index.js +2 -0
  47. data/client/hippo/components/grid/row-editor.scss +74 -0
  48. data/client/hippo/components/grid/styles.scss +118 -0
  49. data/client/hippo/components/icon.jsx +70 -0
  50. data/client/hippo/components/index.js +3 -0
  51. data/client/hippo/components/modal/index.js +1 -0
  52. data/client/hippo/components/modal/styles.scss +12 -0
  53. data/client/hippo/components/network-activity-overlay.jsx +127 -0
  54. data/client/hippo/components/network-activity-overlay.scss +52 -0
  55. data/client/hippo/components/query-builder.jsx +157 -0
  56. data/client/hippo/components/record-finder.jsx +95 -0
  57. data/client/hippo/components/record-finder/config.json +3 -0
  58. data/client/hippo/components/record-finder/query-layer.jsx +74 -0
  59. data/client/hippo/components/record-finder/record-finder.scss +12 -0
  60. data/client/hippo/components/request-spinner/index.js +1 -0
  61. data/client/hippo/components/screen.jsx +34 -0
  62. data/client/hippo/components/select-field/index.js +2 -0
  63. data/client/hippo/components/select-field/styles.scss +27 -0
  64. data/client/hippo/components/shared/AssetsListing.jsx +23 -0
  65. data/client/hippo/components/shared/Checkbox.jsx +49 -0
  66. data/client/hippo/components/shared/CountBadge.jsx +13 -0
  67. data/client/hippo/components/shared/DateTime.jsx +58 -0
  68. data/client/hippo/components/shared/DisplayValue.jsx +15 -0
  69. data/client/hippo/components/shared/ErrorDisplay.jsx +37 -0
  70. data/client/hippo/components/shared/FieldMixin.jsx +254 -0
  71. data/client/hippo/components/shared/FieldSet.jsx +52 -0
  72. data/client/hippo/components/shared/FieldWrapper.jsx +94 -0
  73. data/client/hippo/components/shared/FormGroup.jsx +41 -0
  74. data/client/hippo/components/shared/GenericField.jsx +7 -0
  75. data/client/hippo/components/shared/IconButton.jsx +13 -0
  76. data/client/hippo/components/shared/ImageAsset.jsx +78 -0
  77. data/client/hippo/components/shared/IndeterminateCheckbox.jsx +31 -0
  78. data/client/hippo/components/shared/Input.jsx +16 -0
  79. data/client/hippo/components/shared/InputFieldMixin.jsx +78 -0
  80. data/client/hippo/components/shared/JobProgress.jsx +46 -0
  81. data/client/hippo/components/shared/NumberInput.jsx +37 -0
  82. data/client/hippo/components/shared/PanelHeader.jsx +15 -0
  83. data/client/hippo/components/shared/RadioField.jsx +33 -0
  84. data/client/hippo/components/shared/ResizeSensor.jsx +18 -0
  85. data/client/hippo/components/shared/ScreenWrapper.jsx +17 -0
  86. data/client/hippo/components/shared/TextArea.jsx +19 -0
  87. data/client/hippo/components/shared/Throbber.jsx +8 -0
  88. data/client/hippo/components/shared/ToggleField.jsx +2 -0
  89. data/client/hippo/components/shared/Tooltip.jsx +23 -0
  90. data/client/hippo/components/shared/fields.scss +58 -0
  91. data/client/hippo/components/shared/fieldset.scss +27 -0
  92. data/client/hippo/components/shared/image-asset.scss +53 -0
  93. data/client/hippo/components/shared/index.js +5 -0
  94. data/client/hippo/components/shared/overlay.scss +83 -0
  95. data/client/hippo/components/shared/resize-sensor.scss +30 -0
  96. data/client/hippo/components/shared/styles.scss +64 -0
  97. data/client/hippo/components/shared/throbber.scss +53 -0
  98. data/client/hippo/components/toolbar/changes-notification.scss +63 -0
  99. data/client/hippo/components/toolbar/index.js +3 -0
  100. data/client/hippo/components/toolbar/styles.scss +74 -0
  101. data/client/hippo/components/warning-notification.jsx +14 -0
  102. data/client/hippo/config.js +72 -0
  103. data/client/hippo/extensions/EarlyExtensions.js.erb +3 -0
  104. data/client/hippo/extensions/LateLoaded.js.erb +4 -0
  105. data/client/hippo/extensions/base.js +23 -0
  106. data/client/hippo/extensions/hippo.js +11 -0
  107. data/client/hippo/extensions/index.js +47 -0
  108. data/client/hippo/extensions/namespace-available.js.erb +3 -0
  109. data/client/hippo/fonts/fontawesome-webfont.woff +0 -0
  110. data/client/hippo/fonts/fontawesome-webfont.woff2 +0 -0
  111. data/client/hippo/index.js +1 -0
  112. data/client/hippo/index.scss.erb +31 -0
  113. data/client/hippo/lib/__mocks__/loader.js +11 -0
  114. data/client/hippo/lib/__mocks__/request-assets.js +6 -0
  115. data/client/hippo/lib/all.js +14 -0
  116. data/client/hippo/lib/bootstrap.js +45 -0
  117. data/client/hippo/lib/index.js.erb +6 -0
  118. data/client/hippo/lib/loader.js +67 -0
  119. data/client/hippo/lib/request-assets.js +38 -0
  120. data/client/hippo/lib/smooth-scroll.js +68 -0
  121. data/client/hippo/lib/util.js +101 -0
  122. data/client/hippo/models/PubSub.js +208 -0
  123. data/client/hippo/models/__mocks__/sync.js +17 -0
  124. data/client/hippo/models/asset.js +104 -0
  125. data/client/hippo/models/base.js +142 -0
  126. data/client/hippo/models/collection.js +58 -0
  127. data/client/hippo/models/decorators.js +72 -0
  128. data/client/hippo/models/index.js +10 -0
  129. data/client/hippo/models/query.js +116 -0
  130. data/client/hippo/models/query/array-result.js +188 -0
  131. data/client/hippo/models/query/clause.js +52 -0
  132. data/client/hippo/models/query/field.js +63 -0
  133. data/client/hippo/models/query/info.js +43 -0
  134. data/client/hippo/models/query/operator.js +21 -0
  135. data/client/hippo/models/query/result.js +21 -0
  136. data/client/hippo/models/query/types.js +5 -0
  137. data/client/hippo/models/sync.js +135 -0
  138. data/client/hippo/models/system-setting.js +26 -0
  139. data/client/hippo/react/DefaultComponentNotFound.jsx +23 -0
  140. data/client/hippo/react/Root.jsx +24 -0
  141. data/client/hippo/react/index.js +7 -0
  142. data/client/hippo/react/viewport-root.jsx +44 -0
  143. data/client/hippo/screens/all.js.erb +3 -0
  144. data/client/hippo/screens/definition.js +67 -0
  145. data/client/hippo/screens/group.js +35 -0
  146. data/client/hippo/screens/index.js +34 -0
  147. data/client/hippo/screens/instance.js +99 -0
  148. data/client/hippo/screens/mixins/index.js +0 -0
  149. data/client/hippo/screens/register.js.erb +14 -0
  150. data/client/hippo/screens/styles.scss +8 -0
  151. data/client/hippo/screens/system-settings.jsx +92 -0
  152. data/client/hippo/screens/system-settings/mailer-config.jsx +53 -0
  153. data/client/hippo/screens/system-settings/system-settings.scss +8 -0
  154. data/client/hippo/screens/user-management.jsx +67 -0
  155. data/client/hippo/screens/user-management/edit-form.jsx +94 -0
  156. data/client/hippo/screens/user-management/index.scss +7 -0
  157. data/client/hippo/styles/fonts.scss +23 -0
  158. data/client/hippo/styles/fonts/_animated.scss +34 -0
  159. data/client/hippo/styles/fonts/_bordered-pulled.scss +25 -0
  160. data/client/hippo/styles/fonts/_core.scss +12 -0
  161. data/client/hippo/styles/fonts/_fixed-width.scss +6 -0
  162. data/client/hippo/styles/fonts/_icons.scss +789 -0
  163. data/client/hippo/styles/fonts/_larger.scss +13 -0
  164. data/client/hippo/styles/fonts/_list.scss +19 -0
  165. data/client/hippo/styles/fonts/_mixins.scss +60 -0
  166. data/client/hippo/styles/fonts/_path.scss +10 -0
  167. data/client/hippo/styles/fonts/_rotated-flipped.scss +20 -0
  168. data/client/hippo/styles/fonts/_screen-reader.scss +5 -0
  169. data/client/hippo/styles/fonts/_stacked.scss +20 -0
  170. data/client/hippo/styles/fonts/_variables.scss +800 -0
  171. data/client/hippo/styles/fonts/font-awesome.scss +18 -0
  172. data/client/hippo/styles/fonts/index.scss +2 -0
  173. data/client/hippo/styles/global.scss +3 -0
  174. data/client/hippo/styles/global/fancy-header.scss +14 -0
  175. data/client/hippo/styles/global/mixins.scss +5 -0
  176. data/client/hippo/styles/global/styles.scss +6 -0
  177. data/client/hippo/styles/variables.scss +28 -0
  178. data/client/hippo/testing/index.js +6 -0
  179. data/client/hippo/testing/matchers.js +14 -0
  180. data/client/hippo/testing/mocks/fetch.js +54 -0
  181. data/client/hippo/testing/screens.js +64 -0
  182. data/client/hippo/testing/utils.js +1 -0
  183. data/client/hippo/user.js +93 -0
  184. data/client/hippo/workspace/content.jsx +22 -0
  185. data/client/hippo/workspace/foo.js +0 -0
  186. data/client/hippo/workspace/index.jsx +85 -0
  187. data/client/hippo/workspace/menu-group.jsx +39 -0
  188. data/client/hippo/workspace/menu-option.jsx +41 -0
  189. data/client/hippo/workspace/menu.jsx +71 -0
  190. data/client/hippo/workspace/navbar.jsx +46 -0
  191. data/client/hippo/workspace/screen.jsx +50 -0
  192. data/client/hippo/workspace/styles.scss +61 -0
  193. data/client/hippo/workspace/styles/forms.scss +4 -0
  194. data/client/hippo/workspace/styles/header.scss +69 -0
  195. data/client/hippo/workspace/styles/keybindings.scss +6 -0
  196. data/client/hippo/workspace/styles/layout.scss +230 -0
  197. data/client/hippo/workspace/styles/screens.scss +15 -0
  198. data/client/hippo/workspace/styles/tabs.scss +141 -0
  199. data/client/hippo/workspace/tabs.jsx +60 -0
  200. data/client/hippo/workspace/viewport.jsx +82 -0
  201. data/client/images/hippo/ajax-loader.gif +0 -0
  202. data/client/images/hippo/logo-sm.png +0 -0
  203. data/coffeelint.json +49 -0
  204. data/command-reference-files/initial/.babelrc +20 -0
  205. data/command-reference-files/initial/.eslintrc.js +7 -0
  206. data/command-reference-files/initial/.gitignore +4 -0
  207. data/command-reference-files/initial/.rubocop.yml +9 -0
  208. data/command-reference-files/initial/Gemfile +9 -0
  209. data/command-reference-files/initial/Guardfile +13 -0
  210. data/command-reference-files/initial/Rakefile +2 -0
  211. data/command-reference-files/initial/client/appy-app/components/.gitkeep +0 -0
  212. data/command-reference-files/initial/client/appy-app/extension.js +32 -0
  213. data/command-reference-files/initial/client/appy-app/index.js +6 -0
  214. data/command-reference-files/initial/client/appy-app/models/.gitkeep +0 -0
  215. data/command-reference-files/initial/client/appy-app/models/base.js +11 -0
  216. data/command-reference-files/initial/client/appy-app/screens/.gitkeep +0 -0
  217. data/command-reference-files/initial/client/appy-app/styles.scss +1 -0
  218. data/command-reference-files/initial/config.ru +7 -0
  219. data/command-reference-files/initial/config/database.yml +11 -0
  220. data/command-reference-files/initial/config/initialize.rb +10 -0
  221. data/command-reference-files/initial/config/jest.config.json +19 -0
  222. data/command-reference-files/initial/config/jest/babel-transform.js +47 -0
  223. data/command-reference-files/initial/config/routes.rb +4 -0
  224. data/command-reference-files/initial/config/screens.rb +10 -0
  225. data/command-reference-files/initial/config/webpack.config.js +106 -0
  226. data/command-reference-files/initial/db/.gitkeep +0 -0
  227. data/command-reference-files/initial/lib/appy-app.rb +15 -0
  228. data/command-reference-files/initial/lib/appy-app/extension.rb +22 -0
  229. data/command-reference-files/initial/lib/appy-app/model.rb +11 -0
  230. data/command-reference-files/initial/lib/appy-app/version.rb +3 -0
  231. data/command-reference-files/initial/package.json +6 -0
  232. data/command-reference-files/initial/spec/client/.eslintrc.js +20 -0
  233. data/command-reference-files/initial/spec/client/setup.js +17 -0
  234. data/command-reference-files/initial/spec/server/spec_helper.rb +8 -0
  235. data/command-reference-files/initial/views/index.html +69 -0
  236. data/command-reference-files/model/client/appy-app/models/test_test.js +12 -0
  237. data/command-reference-files/model/config/routes.rb +4 -0
  238. data/command-reference-files/model/db/migrate/20150218032025_create_test_tests.rb +10 -0
  239. data/command-reference-files/model/lib/appy-app/model.rb +12 -0
  240. data/command-reference-files/model/lib/appy-app/models/test_test.rb +7 -0
  241. data/command-reference-files/model/spec/client/models/test_test.spec.js +8 -0
  242. data/command-reference-files/model/spec/fixtures/appy-app/test_test.yml +11 -0
  243. data/command-reference-files/model/spec/server/test_test_spec.rb +10 -0
  244. data/command-reference-files/screen/client/appy-app/extension.js +32 -0
  245. data/command-reference-files/screen/client/appy-app/screens/ready-set-go.jsx +22 -0
  246. data/command-reference-files/screen/config/screens.rb +19 -0
  247. data/command-reference-files/screen/spec/client/screens/ready-set-go.spec.jsx +12 -0
  248. data/config.ru +5 -0
  249. data/config/database.yml +9 -0
  250. data/config/jest.config.json +27 -0
  251. data/config/jest/babel-transform.js +47 -0
  252. data/config/jest/style-mock.js +2 -0
  253. data/config/jest/yaml-transform.js +19 -0
  254. data/config/routes.rb +28 -0
  255. data/config/screens.rb +42 -0
  256. data/config/webpack.config.js +105 -0
  257. data/db/migrate/01_create_system_settings.rb +10 -0
  258. data/db/migrate/02_create_assets.rb +13 -0
  259. data/db/migrate/20140615031600_create_users.rb +12 -0
  260. data/db/seed.rb +1 -0
  261. data/docs/command.md +114 -0
  262. data/docs/model.md +217 -0
  263. data/docs/react.md +137 -0
  264. data/docs/todo-example-part-1.md +69 -0
  265. data/docs/welcome.md +0 -0
  266. data/hippo-fw.gemspec +79 -0
  267. data/lib/generators/hippo/migrations/install_generator.rb +42 -0
  268. data/lib/hippo-fw.rb +1 -0
  269. data/lib/hippo.rb +34 -0
  270. data/lib/hippo/access.rb +49 -0
  271. data/lib/hippo/access/authentication_provider.rb +79 -0
  272. data/lib/hippo/access/config/database.yml +9 -0
  273. data/lib/hippo/access/config/routes.rb +20 -0
  274. data/lib/hippo/access/locked_fields.rb +43 -0
  275. data/lib/hippo/access/public/files/1nty/7ebo/n7k0/8b2ac0bbd97f401951fe40546f977200.png +0 -0
  276. data/lib/hippo/access/public/files/6hgp/eiw1/8dua/ba944287e36e101713a9c1ad793353b8.png +0 -0
  277. data/lib/hippo/access/public/files/94bd/9agc/2ua3/33800e285d7145760650ac88d1c558fb.png +0 -0
  278. data/lib/hippo/access/public/files/cr1e/vfwc/fvrh/0e7fe6ef12d622bfb93e024883c2f81c.png +0 -0
  279. data/lib/hippo/access/public/files/kezo/fm8j/u6xl/dfc47658aedd8e546abff63366a7285d.png +0 -0
  280. data/lib/hippo/access/public/files/n5c4/uovf/jec6/7ee9a3519e2b60430e095160a23f1d77.png +0 -0
  281. data/lib/hippo/access/role.rb +86 -0
  282. data/lib/hippo/access/role_collection.rb +88 -0
  283. data/lib/hippo/access/roles/administrator.rb +32 -0
  284. data/lib/hippo/access/roles/basic_user.rb +13 -0
  285. data/lib/hippo/access/roles/support.rb +15 -0
  286. data/lib/hippo/access/test_fixture_extensions.rb +16 -0
  287. data/lib/hippo/access/track_modifications.rb +79 -0
  288. data/lib/hippo/access/version.rb +5 -0
  289. data/lib/hippo/api.rb +23 -0
  290. data/lib/hippo/api/cable.rb +57 -0
  291. data/lib/hippo/api/controller_base.rb +299 -0
  292. data/lib/hippo/api/default_routes.rb +38 -0
  293. data/lib/hippo/api/error_formatter.rb +37 -0
  294. data/lib/hippo/api/formatted_reply.rb +62 -0
  295. data/lib/hippo/api/generic_controller.rb +35 -0
  296. data/lib/hippo/api/handlers/asset.rb +38 -0
  297. data/lib/hippo/api/handlers/print.rb +15 -0
  298. data/lib/hippo/api/handlers/user_session.rb +42 -0
  299. data/lib/hippo/api/helper_methods.rb +58 -0
  300. data/lib/hippo/api/pub_sub.rb +36 -0
  301. data/lib/hippo/api/request_wrapper.rb +105 -0
  302. data/lib/hippo/api/root.rb +70 -0
  303. data/lib/hippo/api/routing.rb +111 -0
  304. data/lib/hippo/api/sprockets_extension.rb +105 -0
  305. data/lib/hippo/api/to_json.rb +7 -0
  306. data/lib/hippo/api/updates.rb +37 -0
  307. data/lib/hippo/asset.rb +18 -0
  308. data/lib/hippo/capistrano.rb +30 -0
  309. data/lib/hippo/cli.rb +50 -0
  310. data/lib/hippo/command.rb +43 -0
  311. data/lib/hippo/command/app.rb +90 -0
  312. data/lib/hippo/command/client_config.rb +69 -0
  313. data/lib/hippo/command/client_model_update.rb +65 -0
  314. data/lib/hippo/command/console.rb +23 -0
  315. data/lib/hippo/command/db.rb +36 -0
  316. data/lib/hippo/command/db.usage +1 -0
  317. data/lib/hippo/command/generate.rb +24 -0
  318. data/lib/hippo/command/generate_component.rb +28 -0
  319. data/lib/hippo/command/generate_component.usage +11 -0
  320. data/lib/hippo/command/generate_migration.rb +33 -0
  321. data/lib/hippo/command/generate_model.rb +91 -0
  322. data/lib/hippo/command/generate_model.usage +45 -0
  323. data/lib/hippo/command/generate_screen.rb +40 -0
  324. data/lib/hippo/command/generate_screen.usage +8 -0
  325. data/lib/hippo/command/guard.rb +18 -0
  326. data/lib/hippo/command/jest.rb +40 -0
  327. data/lib/hippo/command/migration_support.rb +29 -0
  328. data/lib/hippo/command/model_attribute.rb +193 -0
  329. data/lib/hippo/command/named_command.rb +33 -0
  330. data/lib/hippo/command/puma.rb +56 -0
  331. data/lib/hippo/command/server.rb +19 -0
  332. data/lib/hippo/command/server.usage +3 -0
  333. data/lib/hippo/command/update.rb +13 -0
  334. data/lib/hippo/command/update_model.rb +127 -0
  335. data/lib/hippo/command/update_model.usage +2 -0
  336. data/lib/hippo/command/webpack.rb +57 -0
  337. data/lib/hippo/command/webpack_view.rb +32 -0
  338. data/lib/hippo/concerns/all.rb +15 -0
  339. data/lib/hippo/concerns/api_path.rb +21 -0
  340. data/lib/hippo/concerns/asset_uploader.rb +38 -0
  341. data/lib/hippo/concerns/association_extensions.rb +94 -0
  342. data/lib/hippo/concerns/attr_accessor_with_default.rb +68 -0
  343. data/lib/hippo/concerns/code_identifier.rb +43 -0
  344. data/lib/hippo/concerns/export_associations.rb +52 -0
  345. data/lib/hippo/concerns/export_join_tables.rb +39 -0
  346. data/lib/hippo/concerns/export_methods.rb +104 -0
  347. data/lib/hippo/concerns/export_scope.rb +64 -0
  348. data/lib/hippo/concerns/exported_limit_evaluator.rb +17 -0
  349. data/lib/hippo/concerns/pub_sub.rb +127 -0
  350. data/lib/hippo/concerns/queries.rb +24 -0
  351. data/lib/hippo/concerns/random_identifier.rb +37 -0
  352. data/lib/hippo/concerns/sanitize_fields.rb +32 -0
  353. data/lib/hippo/concerns/set_attribute_data.rb +125 -0
  354. data/lib/hippo/concerns/sorting_expressions.rb +34 -0
  355. data/lib/hippo/configuration.rb +143 -0
  356. data/lib/hippo/db.rb +57 -0
  357. data/lib/hippo/db/migrations.rb +32 -0
  358. data/lib/hippo/environment.rb +22 -0
  359. data/lib/hippo/extension.rb +112 -0
  360. data/lib/hippo/extension/definition.rb +90 -0
  361. data/lib/hippo/guard_tasks.rb +62 -0
  362. data/lib/hippo/hippo_guard_plugin.rb +80 -0
  363. data/lib/hippo/job.rb +82 -0
  364. data/lib/hippo/job/failure_logger.rb +33 -0
  365. data/lib/hippo/logger.rb +64 -0
  366. data/lib/hippo/mailer.rb +28 -0
  367. data/lib/hippo/model.rb +24 -0
  368. data/lib/hippo/multi_server_boot.rb +26 -0
  369. data/lib/hippo/numbers.rb +72 -0
  370. data/lib/hippo/rails_engine.rb +5 -0
  371. data/lib/hippo/rake_tasks.rb +69 -0
  372. data/lib/hippo/redis.rb +13 -0
  373. data/lib/hippo/reloadable_sinatra.rb +24 -0
  374. data/lib/hippo/reloadable_view.rb +13 -0
  375. data/lib/hippo/screen.rb +152 -0
  376. data/lib/hippo/spec_helper.rb +141 -0
  377. data/lib/hippo/strings.rb +56 -0
  378. data/lib/hippo/system_settings.rb +72 -0
  379. data/lib/hippo/templates/base.rb +44 -0
  380. data/lib/hippo/templates/latex.rb +101 -0
  381. data/lib/hippo/templates/liquid.rb +28 -0
  382. data/lib/hippo/user.rb +152 -0
  383. data/lib/hippo/validators/all.rb +2 -0
  384. data/lib/hippo/validators/email.rb +17 -0
  385. data/lib/hippo/validators/set.rb +18 -0
  386. data/lib/hippo/version.rb +5 -0
  387. data/lib/hippo/workspace.rb +4 -0
  388. data/lib/hippo/workspace/config/screens.rb +6 -0
  389. data/package.json +82 -0
  390. data/spec/client/.eslintrc.js +20 -0
  391. data/spec/client/access/login-dialog.spec.jsx +28 -0
  392. data/spec/client/components/__snapshots__/asset.spec.jsx.snap +48 -0
  393. data/spec/client/components/__snapshots__/network-activity-overlay.spec.jsx.snap +35 -0
  394. data/spec/client/components/__snapshots__/query-builder.spec.jsx.snap +82 -0
  395. data/spec/client/components/asset.spec.jsx +16 -0
  396. data/spec/client/components/data-list.spec.jsx +36 -0
  397. data/spec/client/components/data-table.spec.jsx +55 -0
  398. data/spec/client/components/form.spec.jsx +63 -0
  399. data/spec/client/components/network-activity-overlay.spec.jsx +30 -0
  400. data/spec/client/components/query-builder.spec.jsx +45 -0
  401. data/spec/client/components/record-finder.spec.jsx +45 -0
  402. data/spec/client/extension/base.spec.js +15 -0
  403. data/spec/client/lib/util.spec.js +48 -0
  404. data/spec/client/models/asset.spec.js +50 -0
  405. data/spec/client/models/base.spec.js +95 -0
  406. data/spec/client/models/collection.spec.js +51 -0
  407. data/spec/client/models/query.spec.js +243 -0
  408. data/spec/client/models/sync.spec.js +42 -0
  409. data/spec/client/models/system-setting.spec.js +19 -0
  410. data/spec/client/screens/__snapshots__/system-settings.spec.jsx.snap +364 -0
  411. data/spec/client/screens/__snapshots__/tabs.spec.jsx.snap +127 -0
  412. data/spec/client/screens/definition.spec.js +24 -0
  413. data/spec/client/screens/group.spec.js +33 -0
  414. data/spec/client/screens/instance.spec.js +61 -0
  415. data/spec/client/screens/system-settings.spec.jsx +22 -0
  416. data/spec/client/screens/tabs.spec.jsx +36 -0
  417. data/spec/client/screens/user-management.spec.jsx +48 -0
  418. data/spec/client/setup.js +12 -0
  419. data/spec/client/test-logo.json +41 -0
  420. data/spec/client/test-models.js +94 -0
  421. data/spec/client/user.spec.js +19 -0
  422. data/spec/client/workspace/__snapshots__/menu.spec.jsx.snap +380 -0
  423. data/spec/client/workspace/menu.spec.jsx +52 -0
  424. data/spec/factories/user.rb +11 -0
  425. data/spec/fixtures/logo.png +0 -0
  426. data/spec/fixtures/system_settings.yml +8 -0
  427. data/spec/fixtures/test_printer.tex +22 -0
  428. data/spec/fixtures/user.yml +2 -0
  429. data/spec/hippo/components/grid/GridSpec.coffee +56 -0
  430. data/spec/hippo/components/grid/PopoverEditorSpec.coffee +47 -0
  431. data/spec/hippo/components/grid/RowEditorSpec.coffee +98 -0
  432. data/spec/hippo/components/select-field/SelectFieldSpec.coffee +106 -0
  433. data/spec/hippo/components/shared/NetworkActivityOverlaySpec.coffee +34 -0
  434. data/spec/hippo/helpers/.gitkeep +0 -0
  435. data/spec/hippo/helpers/hippo-helpers.coffee +5 -0
  436. data/spec/hippo/helpers/jasmine-matchers.js +1580 -0
  437. data/spec/hippo/helpers/mock-ajax.js +573 -0
  438. data/spec/hippo/models/AssociationMapSpec.coffee +85 -0
  439. data/spec/hippo/models/AssociationProxySpec.coffee +76 -0
  440. data/spec/hippo/models/BaseSpec.coffee +155 -0
  441. data/spec/hippo/models/CollectionSpec.coffee +32 -0
  442. data/spec/hippo/models/EnumMapSpec.coffee +26 -0
  443. data/spec/hippo/models/PubSubSpec.coffee +71 -0
  444. data/spec/hippo/models/QuerySpec.coffee +19 -0
  445. data/spec/hippo/models/SyncSpec.coffee +28 -0
  446. data/spec/hippo/models/UserSpec.coffee +17 -0
  447. data/spec/hippo/react/mixins/DataSpec.coffee +74 -0
  448. data/spec/hippo/screens/DefinitionsSpec.coffee +33 -0
  449. data/spec/hippo/views/BaseSpec.coffee +147 -0
  450. data/spec/hippo/views/FormBindingsSpec.coffee +32 -0
  451. data/spec/server/api/controller_base_spec.rb +101 -0
  452. data/spec/server/assertions.rb +11 -0
  453. data/spec/server/asset_spec.rb +42 -0
  454. data/spec/server/command_spec.rb +73 -0
  455. data/spec/server/concerns/api_path_spec.rb +20 -0
  456. data/spec/server/concerns/association_extensions_spec.rb +24 -0
  457. data/spec/server/concerns/attr_accessor_with_default_spec.rb +63 -0
  458. data/spec/server/concerns/export_methods_spec.rb +34 -0
  459. data/spec/server/concerns/export_scope_spec.rb +14 -0
  460. data/spec/server/concerns/exported_limits_spec.rb +51 -0
  461. data/spec/server/concerns/pub_sub_spec.rb +132 -0
  462. data/spec/server/concerns/set_attribute_data_spec.rb +76 -0
  463. data/spec/server/concerns/sorting_expressions_spec.rb +34 -0
  464. data/spec/server/concerns/track_modifications_spec.rb +18 -0
  465. data/spec/server/configuration_spec.rb +26 -0
  466. data/spec/server/job_spec.rb +54 -0
  467. data/spec/server/mailer_spec.rb +33 -0
  468. data/spec/server/numbers_spec.rb +25 -0
  469. data/spec/server/print/form_spec.rb +29 -0
  470. data/spec/server/spec_helper.rb +74 -0
  471. data/spec/server/strings_spec.rb +41 -0
  472. data/spec/server/system_settings_spec.rb +39 -0
  473. data/tasks/migrations.rake +22 -0
  474. data/tasks/publish.rake +8 -0
  475. data/templates/.babelrc +20 -0
  476. data/templates/.gitignore +4 -0
  477. data/templates/Gemfile +9 -0
  478. data/templates/Guardfile +13 -0
  479. data/templates/Rakefile +2 -0
  480. data/templates/client/components/.gitkeep +0 -0
  481. data/templates/client/components/BaseComponent.coffee +9 -0
  482. data/templates/client/components/Component.cjsx +4 -0
  483. data/templates/client/components/template.html +3 -0
  484. data/templates/client/extension.js +32 -0
  485. data/templates/client/index.js +6 -0
  486. data/templates/client/models/base.js +11 -0
  487. data/templates/client/models/model.js +17 -0
  488. data/templates/client/screens/screen.jsx +22 -0
  489. data/templates/client/styles.scss +1 -0
  490. data/templates/config.ru +7 -0
  491. data/templates/config/database.yml +11 -0
  492. data/templates/config/initialize.rb +10 -0
  493. data/templates/config/jest.config.json +19 -0
  494. data/templates/config/jest/babel-transform.js +47 -0
  495. data/templates/config/routes.rb +4 -0
  496. data/templates/config/screen.rb +9 -0
  497. data/templates/config/screens.rb +10 -0
  498. data/templates/config/webpack.config.js +106 -0
  499. data/templates/db/create_table_migration.rb +19 -0
  500. data/templates/gitignore +4 -0
  501. data/templates/js/config-data.js +10 -0
  502. data/templates/js/jest.config.json +11 -0
  503. data/templates/js/root-view.html +71 -0
  504. data/templates/js/screen-definitions.js +22 -0
  505. data/templates/lib/namespace.rb +15 -0
  506. data/templates/lib/namespace/base_model.rb +11 -0
  507. data/templates/lib/namespace/extension.rb +22 -0
  508. data/templates/lib/namespace/model.rb +7 -0
  509. data/templates/lib/namespace/version.rb +3 -0
  510. data/templates/public/.gitkeep +0 -0
  511. data/templates/spec/client/components/ComponentSpec.coffee +5 -0
  512. data/templates/spec/client/models/model.spec.js +8 -0
  513. data/templates/spec/client/screen.spec.jsx +12 -0
  514. data/templates/spec/client/setup.js +17 -0
  515. data/templates/spec/fixtures/namespace/model.yml +16 -0
  516. data/templates/spec/server/model_spec.rb +10 -0
  517. data/templates/spec/server/spec_helper.rb +8 -0
  518. data/test.js +7 -0
  519. data/views/hippo_root_view.erb +68 -0
  520. data/views/index.html +71 -0
  521. data/yard_ext/all.rb +9 -0
  522. data/yard_ext/code_identifier_handler.rb +33 -0
  523. data/yard_ext/concern_meta_methods.rb +60 -0
  524. data/yard_ext/config_options.rb +27 -0
  525. data/yard_ext/exported_scope.rb +4 -0
  526. data/yard_ext/immutable_handler.rb +17 -0
  527. data/yard_ext/json_attr_accessor.rb +22 -0
  528. data/yard_ext/locked_fields_handler.rb +21 -0
  529. data/yard_ext/templates/default/layout/html/layout.erb +20 -0
  530. data/yard_ext/templates/default/method_details/html/github_link.erb +1 -0
  531. data/yard_ext/templates/default/method_details/setup.rb +3 -0
  532. data/yard_ext/validators.rb +1 -0
  533. data/yard_ext/visible_id_handler.rb +38 -0
  534. data/yarn.lock +6562 -0
  535. metadata +1182 -0
@@ -0,0 +1,86 @@
1
+ module Hippo
2
+ module Access
3
+
4
+ class Role
5
+ class_attribute :read_types, :write_types, :delete_types
6
+
7
+ def initialize(user)
8
+ @user = user
9
+ end
10
+
11
+ def can_read?(model)
12
+ read_types.include?(model)
13
+ end
14
+
15
+ def can_write?(model)
16
+ write_types.include?(model)
17
+ end
18
+
19
+ def can_delete?(model)
20
+ delete_types.include?(model)
21
+ end
22
+
23
+ class << self
24
+ ALL=Array.new
25
+
26
+ def grant_global_access(types=:all, *klass)
27
+ unless types.is_a?(Symbol)
28
+ klass.unshift(types)
29
+ types=:all
30
+ end
31
+ types = [:read,:write,:delete] if :all == types
32
+ types = [*types]
33
+ ALL.each do | child |
34
+ types.each{ |type| child.send(type).concat(klass) }
35
+ end
36
+ end
37
+
38
+ def inherited(subklass)
39
+ ALL << subklass
40
+ subklass.read_types = []; subklass.write_types = []; subklass.delete_types = []
41
+ end
42
+
43
+ def grant( *klasses )
44
+ read_types.push(*klasses)
45
+ write_types.push(*klasses)
46
+ delete_types.push(*klasses)
47
+ end
48
+
49
+ def read( *klasses )
50
+ read_types.push(*klasses)
51
+ end
52
+
53
+ def write( *klasses )
54
+ write_types.push(*klasses)
55
+ end
56
+
57
+ def delete(*klasses )
58
+ delete_types.push(*klasses)
59
+ end
60
+
61
+ def lock(klass, *attributes)
62
+ attributes.each do | attr |
63
+ LockedFields.lock(klass, attr, self)
64
+ end
65
+ end
66
+
67
+ def lock_writes(klass, *attributes)
68
+ attributes.each do | attr |
69
+ LockedFields.lock(klass, attr, self, :write)
70
+ end
71
+ end
72
+
73
+ def all_available
74
+ ALL
75
+ end
76
+
77
+ # By default a role can only access if it's type is included in the
78
+ # array of acceptable roles. An Admin role may provide a custom implementation
79
+ def can_access_locked_roles?(roles)
80
+ roles.include?(self)
81
+ end
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,88 @@
1
+ module Hippo
2
+ module Access
3
+
4
+ class RoleCollection
5
+ include Enumerable
6
+
7
+ def initialize(user)
8
+ @role_names = user.role_names.clone
9
+ @role_names << 'basic_user'
10
+ @roles = @role_names.map{ |name|
11
+ "Hippo::Access::Roles::#{name.classify}".safe_constantize
12
+ }.compact.map{ |klass| klass.new(user) }
13
+ end
14
+
15
+ def exposed_data
16
+ @role_names
17
+ end
18
+
19
+ # @param role [String]
20
+ # @return [Boolean] Does a role with the given id exist?
21
+ def include?(role)
22
+ @role_names.include?(role)
23
+ end
24
+
25
+ # @param model [Hippo::Model]
26
+ # @param attribute [Symbol]
27
+ # @return [Boolean] Can the User view the model?
28
+ def can_read?(model, attribute = nil)
29
+ klass=model_to_class(model)
30
+ test_access(klass, attribute, :read){ |role| role.can_read?(klass) }
31
+ end
32
+
33
+ # @param model [Hippo::Model]
34
+ # @param attribute [Symbol]
35
+ # @return [Boolean] Can the User create and update the model?
36
+ def can_write?(model, attribute = nil)
37
+ klass=model_to_class(model)
38
+ test_access(klass, attribute, :write){ |role| role.can_write?(klass) }
39
+ end
40
+
41
+ # @param model [Hippo::Model]
42
+ # @param id [Fixnum] the id of the record to remove
43
+ # @return [Boolean] Can the User delete the model?
44
+ def can_delete?(model,id)
45
+ klass=model_to_class(model)
46
+ @roles.each{ |role| role.can_delete?(klass) }
47
+ end
48
+
49
+ # @return [Array<symbol>] list of roles
50
+ def to_sym
51
+ @roles.map{ |r| r.class.to_s.demodulize.downcase.to_sym }
52
+ end
53
+
54
+ def each
55
+ @roles.each{|r| yield r}
56
+ end
57
+
58
+ private
59
+
60
+ def role_types
61
+ @role_types ||= @roles.map(&:class)
62
+ end
63
+
64
+ def model_to_class(model)
65
+ model.is_a?(Class) ? model : model.class
66
+ end
67
+
68
+ # Test if the given roles grant access to the model
69
+ def test_access(model, attribute, access_type)
70
+ # Check if the attribute is locked
71
+ # If it is, the locks determine access, otherwise use the model's grants
72
+ locked_to_roles = LockedFields.roles_needed_for(model, attribute, access_type)
73
+ if locked_to_roles.none?
74
+ return @roles.detect{ |role| yield role }.present?
75
+ else
76
+ role_types.any?{|role| role.can_access_locked_roles?(locked_to_roles) }
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+ end
84
+
85
+
86
+ require_relative "roles/support"
87
+ require_relative "roles/basic_user"
88
+ require_relative "roles/administrator"
@@ -0,0 +1,32 @@
1
+ module Hippo
2
+ module Access
3
+
4
+ module Roles
5
+
6
+ class Administrator < Role
7
+
8
+ # The admin can access all the things
9
+ def self.can_access_locked_roles?(roles)
10
+ true
11
+ end
12
+
13
+ def can_read?(model)
14
+ true
15
+ end
16
+
17
+ def can_write?(model)
18
+ true
19
+ end
20
+
21
+ def can_delete?(model)
22
+ true
23
+ end
24
+
25
+ lock User, :password_digest
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ module Hippo
2
+ module Access
3
+
4
+ module Roles
5
+
6
+ class BasicUser < Role
7
+
8
+ end
9
+
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../../user'
2
+
3
+ module Hippo
4
+ module Access
5
+
6
+ module Roles
7
+
8
+ class Support < Role
9
+ self.read << ::Hippo::User
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module AccessFixtureTestPatches
2
+
3
+ def table_rows
4
+ results = super
5
+ if model_class && model_class < ActiveRecord::Base && model_class.record_modifications
6
+ results[ table_name ].each do | row |
7
+ # 135138680 is the 'admin' user
8
+ row['created_by_id'] ||= 135138680 if model_class.column_names.include?('created_by_id')
9
+ row['updated_by_id'] ||= 135138680 if model_class.column_names.include?('updated_by_id')
10
+ end
11
+ end
12
+ results
13
+ end
14
+ end
15
+
16
+ ActiveRecord::FixtureSet.prepend AccessFixtureTestPatches
@@ -0,0 +1,79 @@
1
+ module Hippo::Concerns
2
+
3
+ # Extends Rails updated_by and created_by timestamps to also track who created and updated the model.
4
+ # It reads the current user's id from User.current_id when saving and updating the record
5
+ # The class_name for the created_by and updated_by is set to {Hippo::Configuration#user_model}
6
+ module TrackModifications
7
+ extend ActiveSupport::Concern
8
+ ApiAttributeAccess::DEFAULT_BLACKLISTED.merge(
9
+ created_at: nil, updated_at: nil,
10
+ created_by_id: nil, updated_by_id: nil
11
+ )
12
+
13
+ module ClassMethods
14
+ def should_record_user_modifications?
15
+ record_user_modifications
16
+ end
17
+
18
+ def tracks_user_modifications
19
+ belongs_to :created_by, :class_name=>'Hippo::User'
20
+ belongs_to :updated_by, :class_name=>'Hippo::User'
21
+
22
+ before_update :record_update_modifications
23
+ before_create :record_create_modifications
24
+
25
+ self.export_scope :with_user_logins
26
+
27
+ class_attribute :record_user_modifications
28
+ self.record_user_modifications = true
29
+ end
30
+
31
+
32
+ def with_user_logins
33
+ q = self; t = table_name
34
+ if current_scope.nil? || current_scope.select_values.exclude?("#{t}.*")
35
+ q = q.select("#{t}.*")
36
+ end
37
+ if self.column_names.include?('created_by_id')
38
+ q = q.select("created_by_user.login as created_by_login")
39
+ .joins("left join hippo_users as created_by_user on " \
40
+ "created_by_user.id = #{t}.created_by_id")
41
+ end
42
+ if self.column_names.include?('updated_by_id')
43
+ q = q.select("updated_by_user.login as updated_by_login")
44
+ .joins("left join hippo_users as updated_by_user on " \
45
+ "updated_by_user.id = #{t}.updated_by_id")
46
+ end
47
+ q
48
+ end
49
+
50
+ end
51
+
52
+ private
53
+
54
+ def _record_user_to_column( column )
55
+ user_id = Hippo::User.current_id ? Hippo::User.current_id : 0
56
+ write_attribute( column, user_id ) if self.class.column_names.include?( column )
57
+ end
58
+
59
+ def record_create_modifications
60
+ if self.class.should_record_user_modifications?
61
+ _record_user_to_column('updated_by_id')
62
+ _record_user_to_column('created_by_id')
63
+ end
64
+ true
65
+ end
66
+
67
+ def record_update_modifications
68
+ if self.class.should_record_user_modifications? && should_record_timestamps?
69
+ _record_user_to_column('updated_by_id')
70
+ end
71
+ true
72
+ end
73
+
74
+ def change_tracking_fields
75
+ %w{updated_by_id created_by_id updated_at created_at}
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,5 @@
1
+ module Hippo
2
+ module Access
3
+ VERSION=0.2
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ require_relative '../hippo'
2
+ require_relative 'api/to_json'
3
+ require_relative 'api/request_wrapper'
4
+ require_relative 'api/error_formatter'
5
+ require_relative 'api/formatted_reply'
6
+ require_relative 'api/controller_base'
7
+ require_relative 'api/generic_controller'
8
+ require_relative 'api/cable'
9
+ require_relative 'api/sprockets_extension'
10
+ require_relative 'api/helper_methods'
11
+ require_relative 'api/pub_sub'
12
+ require_relative 'api/handlers/user_session'
13
+ require_relative 'api/handlers/asset'
14
+ require_relative 'api/handlers/print'
15
+ require_relative 'api/root'
16
+
17
+ module Hippo
18
+
19
+ module API
20
+ mattr_accessor :webpack
21
+ end
22
+
23
+ end
@@ -0,0 +1,57 @@
1
+ require 'action_cable'
2
+ # require 'action_cable/subscription_adapter/postgresql'
3
+
4
+ module Hippo
5
+ module API
6
+ module Cable
7
+ mattr_reader :server
8
+ mattr_reader :config
9
+
10
+ def self.handle_request(request)
11
+ @@server.call(request.env)
12
+ end
13
+
14
+ class Channel < ActionCable::Channel::Base
15
+ end
16
+
17
+ class Connection < ActionCable::Connection::Base
18
+ identified_by :current_user
19
+
20
+ def connect
21
+ unless cookies['user_id'] &&
22
+ self.current_user = Hippo::User
23
+ .where(id: cookies['user_id']).first
24
+ Hippo.logger.warn("Rejecting ws connection due to unauthorized access by user_id #{cookies['user_id']}")
25
+
26
+ reject_unauthorized_connection
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ def cookies
33
+ request.session
34
+ end
35
+ end
36
+
37
+ def self.configure
38
+
39
+ require_relative 'updates'
40
+ @@config = ActionCable::Server::Configuration.new
41
+ config.logger = Hippo.logger
42
+ config.cable = Hippo.config.cable
43
+ config.connection_class = -> { Connection }
44
+ config.allowed_request_origins = -> (host) {
45
+ host
46
+ }
47
+
48
+ ActionCable::Server::Base.config = config
49
+ @@server = ActionCable.server
50
+ Updates.relay!
51
+ end
52
+
53
+ end
54
+
55
+
56
+ end
57
+ end
@@ -0,0 +1,299 @@
1
+ require_relative './formatted_reply'
2
+
3
+ module Hippo
4
+ module API
5
+
6
+ # The Controller handles querying models
7
+ # using either pre-defined scopes or hash based queries;
8
+ # and also including optional associations with the reply
9
+ #
10
+ # It assigns the following meaning the these parameters.
11
+ # * f: (fields) Include the following fields (usually methods) with the reply
12
+ # * w: (with) Uses the defined scope to query and/or add extra data to the model
13
+ # * q: (query) Query the model using fields and values
14
+ # it is an array of clauses, which can be either forms
15
+ # { field: value }, or { field: { op: 'like', value: 'value%' } }
16
+ # * i: (include) Include associations along with the model in the reply
17
+ # * o: (order) Order by, { field => "ASC|DESC" }
18
+ # * l: (limit) Limit the returned rows to the count
19
+ # * s: (start) Start the query at the given offset (for paging)
20
+ # * df: (data format) Should data be returned as 'object' (default) or 'array'
21
+ # The parameters are deliberately shortened so they can be used in
22
+ # query parameters without blowing the URL up to an unacceptable length
23
+
24
+ class ControllerBase
25
+
26
+ attr_reader :model, :params, :data
27
+ include FormattedReply
28
+
29
+ def initialize(model, authentication, params, data={})
30
+ @model = model
31
+ @params = params
32
+ @data = data
33
+ @authentication = authentication
34
+ end
35
+
36
+ protected
37
+
38
+ def current_user
39
+ @current_user ||= @authentication.current_user
40
+ end
41
+
42
+ def perform_retrieval
43
+ query = build_query
44
+ query = add_scopes_to_query(query)
45
+ query = add_access_limits_to_query(query)
46
+ options = build_reply_options
47
+ if should_include_total_count?
48
+ options[:total_count] = count_query_records(query)
49
+ end
50
+ query = add_modifiers_to_query(query)
51
+ query = query.first! if params[:id]
52
+ std_api_reply(:retrieve, query, options)
53
+ end
54
+
55
+ def perform_single_destroy
56
+ query = model.where(id: params[:id])
57
+ query = add_access_limits_to_query(query)
58
+ record = query.first!
59
+ record.destroy
60
+ std_api_reply(:destroy, record, {})
61
+ end
62
+
63
+ def perform_multiple_destroy
64
+ query = model.where(id: data.map{|rec|rec['id']})
65
+ query = add_access_limits_to_query(query)
66
+ success = true
67
+ query.each do |record|
68
+ if current_user.can_delete?(record, record.id)
69
+ success = false unless record.destroy
70
+ end
71
+ end
72
+ options = build_reply_options.merge(success: success)
73
+ std_api_reply(:destroy, query, options)
74
+ end
75
+
76
+ def perform_multiple_updates
77
+ query = model.where(id: data.map {|rec|rec['id'] })
78
+ query = add_access_limits_to_query(query)
79
+ success = true
80
+ query.each do |record|
81
+ record_data = data.detect { |rd| rd['id'] == record.id }
82
+ next unless record_data
83
+ if current_user.can_write?(record, record.id)
84
+ record.set_attribute_data(record_data, current_user)
85
+ success = false unless record.save
86
+ end
87
+ end
88
+ options = build_reply_options.merge(success: success)
89
+ std_api_reply(:update, query, options)
90
+ end
91
+
92
+ def perform_single_update
93
+ query = build_query
94
+ query = add_access_limits_to_query(query)
95
+ record = query.first!
96
+ record.set_attribute_data(data, current_user)
97
+ options = build_reply_options.merge(success: record.save)
98
+ std_api_reply(:update, record, options)
99
+ end
100
+
101
+ # @return [Array<String>] The fields to include in query. May represent either an attribute or a method
102
+ def requested_fields
103
+ [*params[:f]]
104
+ end
105
+ def reply_with_array?
106
+ params[:df] == 'array'
107
+ end
108
+ def query_scopes
109
+ [*params[:w]]
110
+ end
111
+ def query_params
112
+ params[:q]
113
+ end
114
+ def include_associations
115
+ [*params[:i]]
116
+ end
117
+ def sort_order
118
+ params[:o]
119
+ end
120
+ def query_limit_size
121
+ limit = max_query_results_size
122
+ requested_limit ? [requested_limit, limit].min : limit
123
+ end
124
+ def query_offset
125
+ params[:s]
126
+ end
127
+ def requested_limit
128
+ params.key?(:l) ? params[:l].to_i : nil
129
+ end
130
+
131
+ # reply options
132
+
133
+ # Should the result include the total number of available records
134
+ def should_include_total_count?
135
+ requested_limit && params[:s] && !params[:id]
136
+ end
137
+
138
+ # Extract options that are suitable for use in 'as_json'
139
+ def build_reply_options
140
+ options = {}
141
+ if include_associations.any?
142
+ options[:include] = include_associations.each_with_object({}) do |association, includes|
143
+ includes.merge! build_allowed_associations(association)
144
+ end
145
+ end
146
+
147
+ if requested_fields.any?
148
+ options[:methods] = requested_fields.select do |f|
149
+ model.has_exported_method?(f, current_user)
150
+ end
151
+ end
152
+ options[:format] = reply_with_array? ? 'array' : 'object'
153
+ options
154
+ end
155
+
156
+ def build_allowed_associations(association, model_class = self.model)
157
+ includes = {}
158
+ if association.is_a?(Hash)
159
+ association.each do |include_name, sub_associations|
160
+ if model_class.has_exported_association?(include_name, current_user) &&
161
+ (reflection = model_class.reflect_on_association(include_name.to_sym))
162
+
163
+ sub_includes = includes[include_name.to_sym] = {}
164
+ allowed = build_allowed_associations(sub_associations, reflection.klass)
165
+ unless allowed.empty?
166
+ sub_includes[:include] ||= []
167
+ sub_includes[:include] << allowed
168
+ end
169
+ end
170
+ end
171
+ elsif association.is_a?(Array)
172
+ association.each do |sub_association|
173
+ if model_class.has_exported_association?(sub_association, current_user)
174
+ includes.merge! build_allowed_associations(sub_association, model_class)
175
+ end
176
+ end
177
+ else
178
+ includes[association.to_sym] = {} if model_class.has_exported_association?(association, current_user)
179
+ end
180
+ includes
181
+ end
182
+
183
+ # query options
184
+
185
+ def add_access_limits_to_query(query)
186
+ if model.respond_to?(:access_limits_for_query)
187
+ model.access_limits_for_query(query, current_user, params)
188
+ else
189
+ query
190
+ end
191
+ end
192
+
193
+ def build_query(query = model.all)
194
+ query = query.where(id: params[:id]) if params[:id]
195
+ if params[:nested_attribute]
196
+ query = query.where(params[:nested_attribute])
197
+ end
198
+ query = add_access_limits_to_query(query)
199
+ query = add_params_to_query(query) if query_params.present?
200
+ query
201
+ end
202
+
203
+ def count_query_records(query)
204
+ query.unscope(:select).count
205
+ end
206
+
207
+ def add_scopes_to_query(query)
208
+ query = add_scope_to_query(query) if query_scopes.present?
209
+ query
210
+ end
211
+
212
+ def add_modifiers_to_query(query)
213
+ query = query.limit(query_limit_size)
214
+ query = query.offset(query_offset.to_i) if query_offset.present?
215
+ if include_associations.any?
216
+ allowed_includes = include_associations.each_with_object([]) do |desired, results|
217
+ if desired.is_a?(Hash)
218
+ nested = {}
219
+ desired.each do |name, sub_associations|
220
+ nested[name.to_sym] = sub_associations if model.has_exported_association?(name, current_user)
221
+ end
222
+ results.push(nested) unless nested.empty?
223
+ else
224
+ results.push(desired.to_sym) if model.has_exported_association?(desired, current_user)
225
+ end
226
+ end
227
+ query = query.includes(allowed_includes) unless allowed_includes.empty?
228
+ end
229
+ if sort_order.present?
230
+ sort_order.each do |fld, dir|
231
+ query = model.append_sort_to_query(
232
+ query, fld, dir.downcase.to_sym
233
+ )
234
+ end
235
+ end
236
+ query
237
+ end
238
+
239
+ def max_query_results_size
240
+ 250 # should be enough for everybody, amirite?
241
+ end
242
+
243
+ def add_scope_to_query(query)
244
+ query_scopes.each do |name, arg|
245
+ next unless model.has_exported_scope?(name, current_user)
246
+ args = [name]
247
+ args.push(arg) unless arg.blank?
248
+ query = query.send(*args)
249
+ end
250
+ query
251
+ end
252
+
253
+ def add_params_to_query(query)
254
+ query_params.each do |field, value|
255
+ next unless (field = convert_field_to_arel(field))
256
+ if value.is_a?(Hash) && value.key?('value')
257
+ op = value['op']
258
+ value = value['value']
259
+ end
260
+ query = query.where(
261
+ field_to_predicate(field, value, op)
262
+ )
263
+ end
264
+ query
265
+ end
266
+
267
+ def convert_field_to_arel(field)
268
+ if field.include?('.')
269
+ (table_name, field_name) = field.split('.')
270
+ if model.has_exported_join_table?(table_name, current_user)
271
+ Arel::Table.new(table_name)[field_name]
272
+ else
273
+ nil
274
+ end
275
+ elsif model.attribute_method?(field)
276
+ model.arel_table[field]
277
+ else
278
+ Arel::Nodes::SqlLiteral.new(
279
+ model.connection.quote_column_name(field)
280
+ )
281
+ end
282
+ end
283
+
284
+ # complete list: https://github.com/rails/arel/blob/master/lib/arel/predications.rb
285
+ def field_to_predicate(field, value, op = nil)
286
+ case op
287
+ when nil, 'eq' then field.eq(value)
288
+ when 'like' then field.matches(value)
289
+ when 'ne' then field.not_eq(value)
290
+ when 'lt' then field.lt(value)
291
+ when 'in' then field.in(Range.new(*value))
292
+ when 'gt' then field.gt(value)
293
+ else
294
+ value =~ /%/ ? field.matches(value) : field.eq(value)
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end