@kaspernj/api-maker 1.0.2011 → 1.0.2012
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{.eslintrc.cjs → .eslintrc.js} +10 -1
- package/README.md +35 -0
- package/android/build.gradle +43 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/expo/modules/api_maker/ApiMakerModule.kt +50 -0
- package/android/src/main/java/expo/modules/api_maker/ApiMakerView.kt +30 -0
- package/build/api.js +120 -0
- package/build/attribute-not-loaded-error.js +2 -0
- package/build/base-component.js +5 -0
- package/build/base-error.js +33 -0
- package/build/base-model/attribute.js +25 -0
- package/build/base-model/column.js +11 -0
- package/build/base-model/reflection.js +14 -0
- package/build/base-model/scope.js +11 -0
- package/build/base-model.js +828 -0
- package/build/bootstrap/attribute-row.js +135 -0
- package/build/bootstrap/attribute-rows.js +31 -0
- package/build/bootstrap/card.js +138 -0
- package/build/bootstrap/checkbox.js +97 -0
- package/build/bootstrap/checkboxes.js +183 -0
- package/build/bootstrap/index.js +2 -0
- package/build/bootstrap/input.js +129 -0
- package/build/bootstrap/invalid-feedback.js +27 -0
- package/build/bootstrap/paginate.js +150 -0
- package/build/bootstrap/radio-buttons.js +72 -0
- package/build/bootstrap/select.js +88 -0
- package/build/bootstrap/sort-link.js +131 -0
- package/build/cable-connection-pool.js +128 -0
- package/build/cable-subscription-pool.js +207 -0
- package/build/cable-subscription.js +21 -0
- package/build/cache-key-generator.js +81 -0
- package/build/can-can.js +134 -0
- package/build/channels-consumer.js +9 -0
- package/build/collection-loader.js +48 -0
- package/build/collection.js +280 -0
- package/build/command-submit-data.js +81 -0
- package/build/commands-pool.js +195 -0
- package/build/compose.js +9 -0
- package/build/config.js +62 -0
- package/build/custom-error.js +5 -0
- package/build/data-set-to-attributes.js +10 -0
- package/build/deserializer.js +56 -0
- package/build/destroy-error.js +5 -0
- package/build/devise.js +112 -0
- package/build/draggable-sort/controller.js +136 -0
- package/build/draggable-sort/index.js +114 -0
- package/build/draggable-sort/item.js +188 -0
- package/build/error-logger.js +101 -0
- package/build/error-messages.js +13 -0
- package/build/event-connection.js +32 -0
- package/build/event-emitter-listener.js +15 -0
- package/build/event-model-class.js +24 -0
- package/build/events.js +5 -0
- package/build/flash-message.js +79 -0
- package/build/form.js +83 -0
- package/build/history-expo.js +20 -0
- package/build/history-react-native.js +20 -0
- package/build/history.js +3 -0
- package/build/index.js +3 -0
- package/build/inputs/attachment.js +133 -0
- package/build/inputs/auto-submit.js +36 -0
- package/build/inputs/checkbox.js +138 -0
- package/build/inputs/checkboxes.js +112 -0
- package/build/inputs/id-for-component.js +15 -0
- package/build/inputs/input-wrapper.js +19 -0
- package/build/inputs/input.js +243 -0
- package/build/inputs/money.js +179 -0
- package/build/inputs/name-for-component.js +13 -0
- package/build/inputs/select.js +93 -0
- package/build/instance-of-class-name.js +23 -0
- package/build/key-value-store.js +53 -0
- package/build/link.js +73 -0
- package/build/logger.js +30 -0
- package/build/modal.js +38 -0
- package/build/model-class-require.js +13 -0
- package/build/model-events.js +30 -0
- package/build/model-name.js +28 -0
- package/build/model-prop-type.js +101 -0
- package/build/model-recipes-loader.js +32 -0
- package/build/model-recipes-model-loader.js +357 -0
- package/build/models-response-reader.js +40 -0
- package/build/models.js +8 -0
- package/build/money-formatter.js +78 -0
- package/build/not-loaded-error.js +2 -0
- package/build/params.js +61 -0
- package/build/preloaded.js +38 -0
- package/build/resize-observer.js +11 -0
- package/build/result.js +13 -0
- package/build/router/route.js +270 -0
- package/build/router/switch.js +72 -0
- package/build/router.js +59 -0
- package/build/routes-native.js +202 -0
- package/build/routes.js +50 -0
- package/build/run-last.js +35 -0
- package/build/serializer.js +63 -0
- package/build/services.js +20 -0
- package/build/session-status-updater.js +134 -0
- package/build/source-maps-loader.js +184 -0
- package/build/super-admin/config-reader.js +85 -0
- package/build/super-admin/edit-page/edit-attribute-checkbox.js +85 -0
- package/build/super-admin/edit-page/edit-attribute-content.js +55 -0
- package/build/super-admin/edit-page/edit-attribute-input.js +79 -0
- package/build/super-admin/edit-page/edit-attribute.js +77 -0
- package/build/super-admin/edit-page.js +123 -0
- package/build/super-admin/has-edit-config.js +12 -0
- package/build/super-admin/index-page.js +25 -0
- package/build/super-admin/index.js +181 -0
- package/build/super-admin/layout/header/index.js +161 -0
- package/build/super-admin/layout/index.js +164 -0
- package/build/super-admin/layout/menu/index.js +206 -0
- package/build/super-admin/layout/menu/menu-content.js +46 -0
- package/build/super-admin/layout/menu/menu-item.js +84 -0
- package/build/super-admin/layout/no-access.js +27 -0
- package/build/super-admin/model-class-table.js +61 -0
- package/build/super-admin/models.js +8 -0
- package/build/super-admin/show-nav.js +51 -0
- package/build/super-admin/show-page/belongs-to-attribute-row.js +29 -0
- package/build/super-admin/show-page/index.js +141 -0
- package/build/super-admin/show-reflection-actions.js +47 -0
- package/build/super-admin/show-reflection-link.js +48 -0
- package/build/super-admin/show-reflection-page.js +48 -0
- package/build/table/column-content.js +117 -0
- package/build/table/column-identifier.js +18 -0
- package/build/table/column-visible.js +7 -0
- package/build/table/components/column.js +21 -0
- package/build/table/components/flat-list.js +21 -0
- package/build/table/components/header.js +26 -0
- package/build/table/components/row.js +26 -0
- package/build/table/filters/attribute-element.js +44 -0
- package/build/table/filters/filter-form.js +402 -0
- package/build/table/filters/filter.js +90 -0
- package/build/table/filters/index.js +184 -0
- package/build/table/filters/load-search-modal.js +175 -0
- package/build/table/filters/reflection-element.js +37 -0
- package/build/table/filters/save-search-modal.js +88 -0
- package/build/table/filters/scope-element.js +42 -0
- package/build/table/header-column-content.js +68 -0
- package/build/table/header-column.js +144 -0
- package/build/table/header-select.js +105 -0
- package/build/table/model-callback-args.js +10 -0
- package/build/table/model-column.js +97 -0
- package/build/table/model-row.js +193 -0
- package/build/table/select-calculator.js +63 -0
- package/build/table/settings/column-row.js +97 -0
- package/build/table/settings/download-action.js +79 -0
- package/build/table/settings/index.js +92 -0
- package/build/table/table-settings.js +219 -0
- package/build/table/table.js +972 -0
- package/build/table/use-sorting.js +34 -0
- package/build/table/widths.js +75 -0
- package/build/table/worker-plugins-check-all-checkbox.js +125 -0
- package/build/table/worker-plugins-checkbox.js +115 -0
- package/build/translated-attributes.js +10 -0
- package/build/translated-collections.js +12 -0
- package/build/updated-attribute.js +76 -0
- package/build/url-encode.js +15 -0
- package/build/use-breakpoint.js +62 -0
- package/build/use-can-can.js +55 -0
- package/build/use-collection.js +225 -0
- package/build/use-created-event.js +52 -0
- package/build/use-current-user.js +76 -0
- package/build/use-destroyed-event.js +60 -0
- package/build/use-event-emitter.js +13 -0
- package/build/use-event-listener.js +16 -0
- package/build/use-input.js +137 -0
- package/build/use-model-event.js +60 -0
- package/build/use-model.js +169 -0
- package/build/use-resize-observer.js +23 -0
- package/build/use-router.js +104 -0
- package/build/use-screen-layout.js +39 -0
- package/build/use-updated-event.js +60 -0
- package/build/use-validation-errors.js +30 -0
- package/build/utils/card.js +51 -0
- package/build/utils/checkbox.js +78 -0
- package/build/utils/checkboxes.js +163 -0
- package/build/utils/default-style.js +18 -0
- package/build/utils/icon.js +23 -0
- package/build/utils/invalid-feedback.js +19 -0
- package/build/utils/modal.js +62 -0
- package/build/utils/text.js +19 -0
- package/build/validation-error.js +28 -0
- package/build/validation-errors.js +98 -0
- package/build/with-api-maker.js +29 -0
- package/build/with-collection.js +14 -0
- package/build/with-current-user.js +10 -0
- package/build/with-model.js +15 -0
- package/build/with-router.js +22 -0
- package/expo-module.config.json +17 -0
- package/ios/ApiMaker.podspec +29 -0
- package/ios/ApiMakerModule.swift +48 -0
- package/ios/ApiMakerView.swift +38 -0
- package/link.sh +8 -0
- package/package.json +49 -46
- package/src/{api.mjs → api.js} +38 -37
- package/src/base-component.jsx +5 -0
- package/src/{base-error.mjs → base-error.js} +4 -6
- package/src/base-model/attribute.js +33 -0
- package/src/base-model/column.js +13 -0
- package/src/base-model/reflection.js +15 -0
- package/src/base-model/scope.js +12 -0
- package/src/{base-model.mjs → base-model.js} +302 -144
- package/src/bootstrap/attribute-row.jsx +135 -0
- package/src/bootstrap/attribute-rows.jsx +27 -0
- package/src/bootstrap/card.jsx +149 -0
- package/src/bootstrap/checkbox.jsx +86 -0
- package/src/bootstrap/checkboxes.jsx +185 -0
- package/src/bootstrap/index.js +0 -0
- package/src/bootstrap/input.jsx +173 -0
- package/src/bootstrap/invalid-feedback.jsx +31 -0
- package/src/bootstrap/paginate.jsx +187 -0
- package/src/bootstrap/radio-buttons.jsx +87 -0
- package/src/bootstrap/select.jsx +110 -0
- package/src/bootstrap/sort-link.jsx +106 -0
- package/src/{cable-connection-pool.mjs → cable-connection-pool.js} +16 -36
- package/src/{cable-subscription-pool.mjs → cable-subscription-pool.js} +26 -21
- package/src/{cable-subscription.mjs → cable-subscription.js} +6 -4
- package/src/cache-key-generator.js +100 -0
- package/src/{can-can.mjs → can-can.js} +13 -12
- package/src/channels-consumer.js +10 -0
- package/src/collection-loader.jsx +58 -224
- package/src/{collection.mjs → collection.js} +98 -44
- package/src/{command-submit-data.mjs → command-submit-data.js} +2 -7
- package/src/{commands-pool.mjs → commands-pool.js} +57 -41
- package/src/compose.js +11 -0
- package/src/{config.mjs → config.js} +6 -2
- package/src/{custom-error.mjs → custom-error.js} +1 -1
- package/src/data-set-to-attributes.js +13 -0
- package/src/{deserializer.mjs → deserializer.js} +3 -3
- package/src/destroy-error.js +7 -0
- package/src/devise.js +129 -0
- package/src/draggable-sort/controller.js +137 -0
- package/src/draggable-sort/index.jsx +108 -0
- package/src/draggable-sort/item.jsx +174 -0
- package/src/{error-logger.mjs → error-logger.js} +12 -7
- package/src/{error-messages.mjs → error-messages.js} +1 -0
- package/src/event-connection.jsx +1 -1
- package/src/event-emitter-listener.jsx +8 -27
- package/src/event-model-class.jsx +2 -4
- package/src/events.js +7 -0
- package/src/flash-message.js +70 -0
- package/src/form.jsx +91 -0
- package/src/history-expo.js +23 -0
- package/src/history-react-native.js +25 -0
- package/src/history.js +3 -0
- package/src/index.js +3 -0
- package/src/inputs/attachment.jsx +108 -0
- package/src/inputs/auto-submit.js +37 -0
- package/src/inputs/checkbox.jsx +125 -0
- package/src/inputs/checkboxes.jsx +116 -0
- package/src/inputs/id-for-component.js +15 -0
- package/src/inputs/input-wrapper.jsx +16 -0
- package/src/inputs/input.jsx +260 -0
- package/src/inputs/money.jsx +187 -0
- package/src/inputs/name-for-component.js +15 -0
- package/src/inputs/select.jsx +104 -0
- package/src/{key-value-store.mjs → key-value-store.js} +1 -1
- package/src/link.jsx +54 -9
- package/src/logger.js +38 -0
- package/src/modal.jsx +37 -0
- package/src/model-class-require.js +17 -0
- package/src/{model-events.mjs → model-events.js} +1 -1
- package/src/{model-name.mjs → model-name.js} +8 -8
- package/src/{model-recipes-loader.mjs → model-recipes-loader.js} +1 -1
- package/src/{model-recipes-model-loader.mjs → model-recipes-model-loader.js} +5 -5
- package/src/{models-response-reader.mjs → models-response-reader.js} +3 -4
- package/src/models.js +7 -0
- package/src/{models.mjs.erb → models.js.erb} +6 -3
- package/src/{money-formatter.mjs → money-formatter.js} +5 -4
- package/src/{params.mjs → params.js} +17 -7
- package/src/{preloaded.mjs → preloaded.js} +2 -2
- package/src/resize-observer.jsx +10 -0
- package/src/result.js +13 -0
- package/src/router/route.jsx +246 -0
- package/src/router/switch.jsx +76 -0
- package/src/router.jsx +23 -21
- package/src/{routes-native.mjs → routes-native.js} +12 -6
- package/src/{routes.mjs → routes.js} +1 -1
- package/src/run-last.js +39 -0
- package/src/{serializer.mjs → serializer.js} +2 -2
- package/src/{services.mjs → services.js} +1 -1
- package/src/session-status-updater.js +172 -0
- package/src/{source-maps-loader.mjs → source-maps-loader.js} +54 -28
- package/src/super-admin/config-reader.jsx +93 -0
- package/src/super-admin/edit-page/edit-attribute-checkbox.jsx +81 -0
- package/src/super-admin/edit-page/edit-attribute-content.jsx +57 -0
- package/src/super-admin/edit-page/edit-attribute-input.jsx +71 -0
- package/src/super-admin/edit-page/edit-attribute.jsx +81 -0
- package/src/super-admin/edit-page.jsx +117 -0
- package/src/super-admin/has-edit-config.js +15 -0
- package/src/super-admin/index-page.jsx +23 -0
- package/src/super-admin/index.jsx +197 -0
- package/src/super-admin/layout/header/index.jsx +144 -0
- package/src/super-admin/layout/header/style.scss +45 -0
- package/src/super-admin/layout/index.jsx +154 -0
- package/src/super-admin/layout/menu/index.jsx +171 -0
- package/src/super-admin/layout/menu/menu-content.jsx +43 -0
- package/src/super-admin/layout/menu/menu-item.jsx +70 -0
- package/src/super-admin/layout/menu/style.scss +11 -0
- package/src/super-admin/layout/no-access.jsx +26 -0
- package/src/super-admin/model-class-table.jsx +66 -0
- package/src/super-admin/models.js +11 -0
- package/src/super-admin/show-nav.jsx +44 -0
- package/src/super-admin/show-page/belongs-to-attribute-row.jsx +30 -0
- package/src/super-admin/show-page/index.jsx +141 -0
- package/src/super-admin/show-reflection-actions.jsx +49 -0
- package/src/super-admin/show-reflection-link.jsx +40 -0
- package/src/super-admin/show-reflection-page.jsx +47 -0
- package/src/super-admin/stylesheets/variables.scss +11 -0
- package/src/table/column-content.jsx +122 -0
- package/src/table/column-identifier.js +23 -0
- package/src/table/column-visible.js +7 -0
- package/src/table/components/column.jsx +19 -0
- package/src/table/components/flat-list.jsx +19 -0
- package/src/table/components/header.jsx +21 -0
- package/src/table/components/row.jsx +23 -0
- package/src/table/filters/attribute-element.jsx +47 -0
- package/src/table/filters/filter-form.jsx +407 -0
- package/src/table/filters/filter.jsx +70 -0
- package/src/table/filters/index.jsx +170 -0
- package/src/table/filters/load-search-modal.jsx +146 -0
- package/src/table/filters/reflection-element.jsx +38 -0
- package/src/table/filters/save-search-modal.jsx +74 -0
- package/src/table/filters/scope-element.jsx +44 -0
- package/src/table/header-column-content.jsx +55 -0
- package/src/table/header-column.jsx +129 -0
- package/src/table/header-select.jsx +73 -0
- package/src/table/model-callback-args.js +10 -0
- package/src/table/model-column.jsx +82 -0
- package/src/table/model-row.jsx +136 -0
- package/src/table/select-calculator.js +65 -0
- package/src/table/settings/column-row.jsx +93 -0
- package/src/table/settings/download-action.jsx +68 -0
- package/src/table/settings/index.jsx +65 -0
- package/src/table/table-settings.js +263 -0
- package/src/table/table.jsx +950 -0
- package/src/table/use-sorting.js +35 -0
- package/src/table/variables.scss +11 -0
- package/src/table/widths.jsx +87 -0
- package/src/table/worker-plugins-check-all-checkbox.jsx +112 -0
- package/src/table/worker-plugins-checkbox.jsx +104 -0
- package/src/translated-attributes.js +11 -0
- package/src/{translated-collections.mjs → translated-collections.js} +3 -2
- package/src/updated-attribute.jsx +1 -1
- package/src/url-encode.js +18 -0
- package/src/use-breakpoint.js +76 -0
- package/src/use-can-can.js +55 -0
- package/src/use-collection.js +280 -0
- package/src/use-created-event.js +55 -0
- package/src/use-current-user.js +88 -0
- package/src/use-destroyed-event.js +60 -0
- package/src/use-event-emitter.js +15 -0
- package/src/use-event-listener.js +19 -0
- package/src/use-input.js +157 -0
- package/src/use-model-event.js +60 -0
- package/src/use-model.js +189 -0
- package/src/use-resize-observer.js +24 -0
- package/src/use-router.jsx +128 -0
- package/src/use-screen-layout.js +49 -0
- package/src/use-updated-event.js +60 -0
- package/src/use-validation-errors.js +33 -0
- package/src/utils/card.jsx +49 -0
- package/src/utils/checkbox.jsx +80 -0
- package/src/utils/checkboxes.jsx +158 -0
- package/src/utils/default-style.jsx +25 -0
- package/src/utils/icon.jsx +21 -0
- package/src/utils/invalid-feedback.jsx +20 -0
- package/src/utils/modal.jsx +60 -0
- package/src/utils/text.jsx +16 -0
- package/src/{validation-error.mjs → validation-error.js} +9 -5
- package/src/{validation-errors.mjs → validation-errors.js} +15 -27
- package/src/with-api-maker.jsx +31 -0
- package/src/with-collection.jsx +17 -0
- package/src/with-current-user.jsx +7 -25
- package/src/with-model.jsx +16 -0
- package/src/with-router.jsx +7 -129
- package/__tests__/base-model.test.js +0 -71
- package/__tests__/cable-connection-pool.test.js +0 -227
- package/__tests__/cable-subscription-pool.test.js +0 -26
- package/__tests__/can-can.test.js +0 -34
- package/__tests__/collection.test.js +0 -51
- package/__tests__/custom-error.test.js +0 -13
- package/__tests__/model-name.test.js +0 -34
- package/__tests__/model-prop-type.test.js +0 -113
- package/__tests__/params.test.js +0 -40
- package/__tests__/routes-native.test.js +0 -103
- package/__tests__/routes.test.js +0 -46
- package/__tests__/serializer.test.js +0 -30
- package/__tests__/support/task.js +0 -27
- package/__tests__/support/user.js +0 -32
- package/index.js +0 -1
- package/jest.config.js +0 -4
- package/src/can-can-loader.jsx +0 -54
- package/src/channels-consumer.mjs +0 -3
- package/src/destroy-error.mjs +0 -7
- package/src/devise.mjs +0 -122
- package/src/event-created.jsx +0 -65
- package/src/event-destroyed.jsx +0 -29
- package/src/event-listener.jsx +0 -38
- package/src/event-updated.jsx +0 -74
- package/src/logger.mjs +0 -25
- package/src/model-class-require.mjs +0 -10
- package/src/model-load-wrapper.jsx +0 -116
- package/src/result.mjs +0 -29
- package/src/session-status-updater.mjs +0 -116
- package/webpack.config.js +0 -15
- /package/src/{attribute-not-loaded-error.mjs → attribute-not-loaded-error.js} +0 -0
- /package/src/{instance-of-class-name.mjs → instance-of-class-name.js} +0 -0
- /package/src/{model-prop-type.mjs → model-prop-type.js} +0 -0
- /package/src/{not-loaded-error.mjs → not-loaded-error.js} +0 -0
- /package/src/{translated-collections-data.mjs.erb → translated-collections-data.js.erb} +0 -0
|
@@ -1,20 +1,61 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import Attribute from "./base-model/attribute"
|
|
2
|
+
import AttributeNotLoadedError from "./attribute-not-loaded-error"
|
|
3
|
+
import CacheKeyGenerator from "./cache-key-generator"
|
|
4
|
+
import Collection from "./collection"
|
|
5
|
+
import CommandsPool from "./commands-pool"
|
|
6
|
+
import Config from "./config"
|
|
7
|
+
import CustomError from "./custom-error"
|
|
6
8
|
import {digg} from "diggerize"
|
|
7
9
|
import FormDataObjectizer from "form-data-objectizer"
|
|
8
|
-
import inflection from "inflection"
|
|
9
|
-
import ModelName from "./model-name
|
|
10
|
-
import NotLoadedError from "./not-loaded-error
|
|
10
|
+
import * as inflection from "inflection"
|
|
11
|
+
import ModelName from "./model-name"
|
|
12
|
+
import NotLoadedError from "./not-loaded-error"
|
|
11
13
|
import objectToFormData from "object-to-formdata"
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
14
|
+
import Reflection from "./base-model/reflection"
|
|
15
|
+
import Scope from "./base-model/scope"
|
|
16
|
+
import Services from "./services"
|
|
17
|
+
import ValidationError from "./validation-error"
|
|
18
|
+
import {ValidationErrors} from "./validation-errors"
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
const objectToUnderscore = (object) => {
|
|
21
|
+
const newObject = {}
|
|
22
|
+
|
|
23
|
+
for (const key in object) {
|
|
24
|
+
const underscoreKey = inflection.underscore(key)
|
|
25
|
+
|
|
26
|
+
newObject[underscoreKey] = object[key]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return newObject
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default class BaseModel {
|
|
33
|
+
static apiMakerType = "BaseModel"
|
|
34
|
+
|
|
35
|
+
static attributes() {
|
|
36
|
+
const attributes = digg(this.modelClassData(), "attributes")
|
|
37
|
+
const result = []
|
|
38
|
+
|
|
39
|
+
for (const attributeKey in attributes) {
|
|
40
|
+
const attributeData = attributes[attributeKey]
|
|
41
|
+
const attribute = new Attribute(attributeData)
|
|
42
|
+
|
|
43
|
+
result.push(attribute)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return result
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static hasAttribute(attributeName) {
|
|
50
|
+
const attributes = digg(this.modelClassData(), "attributes")
|
|
51
|
+
const lowerCaseAttributeName = inflection.underscore(attributeName)
|
|
52
|
+
|
|
53
|
+
if (lowerCaseAttributeName in attributes) return true
|
|
54
|
+
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static modelClassData() {
|
|
18
59
|
throw new Error("modelClassData should be overriden by child")
|
|
19
60
|
}
|
|
20
61
|
|
|
@@ -29,10 +70,10 @@ class BaseModel {
|
|
|
29
70
|
}
|
|
30
71
|
}
|
|
31
72
|
|
|
32
|
-
static async find
|
|
33
|
-
const primaryKeyName = this.modelClassData().primaryKey
|
|
73
|
+
static async find(id) {
|
|
34
74
|
const query = {}
|
|
35
|
-
|
|
75
|
+
|
|
76
|
+
query[`${this.primaryKey()}_eq`] = id
|
|
36
77
|
|
|
37
78
|
const model = await this.ransack(query).first()
|
|
38
79
|
|
|
@@ -43,7 +84,7 @@ class BaseModel {
|
|
|
43
84
|
}
|
|
44
85
|
}
|
|
45
86
|
|
|
46
|
-
static async findOrCreateBy
|
|
87
|
+
static async findOrCreateBy(findOrCreateByArgs, args = {}) {
|
|
47
88
|
const result = await Services.current().sendRequest("Models::FindOrCreateBy", {
|
|
48
89
|
additional_data: args.additionalData,
|
|
49
90
|
find_or_create_by_args: findOrCreateByArgs,
|
|
@@ -54,43 +95,119 @@ class BaseModel {
|
|
|
54
95
|
return model
|
|
55
96
|
}
|
|
56
97
|
|
|
57
|
-
static modelName
|
|
98
|
+
static modelName() {
|
|
58
99
|
return new ModelName({modelClassData: this.modelClassData()})
|
|
59
100
|
}
|
|
60
101
|
|
|
61
|
-
static
|
|
102
|
+
static primaryKey() {
|
|
103
|
+
return digg(this.modelClassData(), "primaryKey")
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static ransack(query = {}) {
|
|
62
107
|
return new Collection({modelClass: this}, {ransack: query})
|
|
63
108
|
}
|
|
64
109
|
|
|
65
|
-
|
|
110
|
+
static select(select) {
|
|
111
|
+
return this.ransack().select(select)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static ransackableAssociations() {
|
|
115
|
+
const relationships = digg(this.modelClassData(), "ransackable_associations")
|
|
116
|
+
const reflections = []
|
|
117
|
+
|
|
118
|
+
for (const relationshipData of relationships) {
|
|
119
|
+
reflections.push(new Reflection(relationshipData))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return reflections
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static ransackableAttributes() {
|
|
126
|
+
const attributes = digg(this.modelClassData(), "ransackable_attributes")
|
|
127
|
+
const result = []
|
|
128
|
+
|
|
129
|
+
for (const attributeData of attributes) {
|
|
130
|
+
result.push(new Attribute(attributeData))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return result
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static ransackableScopes() {
|
|
137
|
+
const ransackableScopes = digg(this.modelClassData(), "ransackable_scopes")
|
|
138
|
+
const result = []
|
|
139
|
+
|
|
140
|
+
for (const scopeData of ransackableScopes) {
|
|
141
|
+
const scope = new Scope(scopeData)
|
|
142
|
+
|
|
143
|
+
result.push(scope)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return result
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static reflections() {
|
|
150
|
+
const relationships = digg(this.modelClassData(), "relationships")
|
|
151
|
+
const reflections = []
|
|
152
|
+
|
|
153
|
+
for (const relationshipData of relationships) {
|
|
154
|
+
const reflection = new Reflection(relationshipData)
|
|
155
|
+
|
|
156
|
+
reflections.push(reflection)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return reflections
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
static reflection(name) {
|
|
163
|
+
const foundReflection = this.reflections().find((reflection) => reflection.name() == name)
|
|
164
|
+
|
|
165
|
+
if (!foundReflection) {
|
|
166
|
+
throw new Error(`No such reflection: ${name} in ${this.reflections().map((reflection) => reflection.name()).join(", ")}`)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return foundReflection
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static _token() {
|
|
173
|
+
const csrfTokenElement = document.querySelector("meta[name='csrf-token']")
|
|
174
|
+
|
|
175
|
+
if (csrfTokenElement) {
|
|
176
|
+
return csrfTokenElement.getAttribute("content")
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
constructor(args = {}) {
|
|
66
181
|
this.changes = {}
|
|
67
182
|
this.newRecord = args.isNewRecord
|
|
68
183
|
this.relationshipsCache = {}
|
|
184
|
+
this.relationships = {}
|
|
69
185
|
|
|
70
186
|
if (args && args.data && args.data.a) {
|
|
71
187
|
this._readModelDataFromArgs(args)
|
|
72
188
|
} else if (args.a) {
|
|
73
189
|
this.abilities = args.b || {}
|
|
74
|
-
this.modelData = args.a
|
|
190
|
+
this.modelData = objectToUnderscore(args.a)
|
|
75
191
|
} else if (args) {
|
|
76
192
|
this.abilities = {}
|
|
77
|
-
this.modelData = args
|
|
193
|
+
this.modelData = objectToUnderscore(args)
|
|
78
194
|
} else {
|
|
79
195
|
this.abilities = {}
|
|
80
196
|
this.modelData = {}
|
|
81
197
|
}
|
|
82
198
|
}
|
|
83
199
|
|
|
84
|
-
assignAttributes
|
|
200
|
+
assignAttributes(newAttributes) {
|
|
85
201
|
for (const key in newAttributes) {
|
|
86
202
|
const newValue = newAttributes[key]
|
|
203
|
+
const attributeUnderscore = inflection.underscore(key)
|
|
87
204
|
|
|
88
205
|
let applyChange = true
|
|
89
206
|
let deleteChange = false
|
|
90
207
|
|
|
91
208
|
if (this.isAttributeLoaded(key)) {
|
|
92
|
-
const oldValue = this.
|
|
93
|
-
const originalValue = this.modelData[
|
|
209
|
+
const oldValue = this.readAttribute(key)
|
|
210
|
+
const originalValue = this.modelData[attributeUnderscore]
|
|
94
211
|
|
|
95
212
|
if (newValue == oldValue) {
|
|
96
213
|
applyChange = false
|
|
@@ -101,18 +218,28 @@ class BaseModel {
|
|
|
101
218
|
}
|
|
102
219
|
|
|
103
220
|
if (applyChange) {
|
|
104
|
-
this.changes[
|
|
221
|
+
this.changes[attributeUnderscore] = newValue
|
|
105
222
|
} else if (deleteChange) {
|
|
106
|
-
delete this.changes[
|
|
223
|
+
delete this.changes[attributeUnderscore]
|
|
107
224
|
}
|
|
108
225
|
}
|
|
109
226
|
}
|
|
110
227
|
|
|
111
|
-
attributes
|
|
112
|
-
|
|
228
|
+
attributes() {
|
|
229
|
+
const result = {}
|
|
230
|
+
|
|
231
|
+
for (const key in this.modelData) {
|
|
232
|
+
result[key] = this.modelData[key]
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
for (const key in this.changes) {
|
|
236
|
+
result[key] = this.changes[key]
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return result
|
|
113
240
|
}
|
|
114
241
|
|
|
115
|
-
can
|
|
242
|
+
can(givenAbilityName) {
|
|
116
243
|
const abilityName = inflection.underscore(givenAbilityName)
|
|
117
244
|
|
|
118
245
|
if (!(abilityName in this.abilities)) {
|
|
@@ -122,17 +249,18 @@ class BaseModel {
|
|
|
122
249
|
return this.abilities[abilityName]
|
|
123
250
|
}
|
|
124
251
|
|
|
125
|
-
clone
|
|
252
|
+
clone() {
|
|
126
253
|
const clone = new this.constructor()
|
|
127
254
|
|
|
128
255
|
clone.abilities = {...this.abilities}
|
|
129
256
|
clone.modelData = {...this.modelData}
|
|
257
|
+
clone.relationships = {...this.relationships}
|
|
130
258
|
clone.relationshipsCache = {...this.relationshipsCache}
|
|
131
259
|
|
|
132
260
|
return clone
|
|
133
261
|
}
|
|
134
262
|
|
|
135
|
-
cacheKey
|
|
263
|
+
cacheKey() {
|
|
136
264
|
if (this.isPersisted()) {
|
|
137
265
|
const keyParts = [
|
|
138
266
|
this.modelClassData().paramKey,
|
|
@@ -157,11 +285,23 @@ class BaseModel {
|
|
|
157
285
|
}
|
|
158
286
|
}
|
|
159
287
|
|
|
160
|
-
|
|
288
|
+
localCacheKey() {
|
|
289
|
+
const cacheKeyGenerator = new CacheKeyGenerator(this)
|
|
290
|
+
|
|
291
|
+
return cacheKeyGenerator.local()
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
fullCacheKey() {
|
|
295
|
+
const cacheKeyGenerator = new CacheKeyGenerator(this)
|
|
296
|
+
|
|
297
|
+
return cacheKeyGenerator.cacheKey()
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
static all() {
|
|
161
301
|
return this.ransack()
|
|
162
302
|
}
|
|
163
303
|
|
|
164
|
-
async create
|
|
304
|
+
async create(attributes, options) {
|
|
165
305
|
if (attributes) this.assignAttributes(attributes)
|
|
166
306
|
const paramKey = this.modelClassData().paramKey
|
|
167
307
|
const modelData = this.getAttributes()
|
|
@@ -195,7 +335,7 @@ class BaseModel {
|
|
|
195
335
|
return {model: this, response}
|
|
196
336
|
}
|
|
197
337
|
|
|
198
|
-
async createRaw
|
|
338
|
+
async createRaw(rawData, options = {}) {
|
|
199
339
|
const objectData = BaseModel._objectDataFromGivenRawData(rawData, options)
|
|
200
340
|
|
|
201
341
|
let response
|
|
@@ -226,7 +366,7 @@ class BaseModel {
|
|
|
226
366
|
return {model: this, response}
|
|
227
367
|
}
|
|
228
368
|
|
|
229
|
-
async destroy
|
|
369
|
+
async destroy() {
|
|
230
370
|
const response = await CommandsPool.addCommand(
|
|
231
371
|
{
|
|
232
372
|
args: {query_params: this.collection && this.collection.params()},
|
|
@@ -250,7 +390,7 @@ class BaseModel {
|
|
|
250
390
|
}
|
|
251
391
|
}
|
|
252
392
|
|
|
253
|
-
async ensureAbilities
|
|
393
|
+
async ensureAbilities(listOfAbilities) {
|
|
254
394
|
// Populate an array with a list of abilities currently not loaded
|
|
255
395
|
const abilitiesToLoad = []
|
|
256
396
|
|
|
@@ -262,7 +402,7 @@ class BaseModel {
|
|
|
262
402
|
|
|
263
403
|
// Load the missing abilities if any
|
|
264
404
|
if (abilitiesToLoad.length > 0) {
|
|
265
|
-
const primaryKeyName = this.
|
|
405
|
+
const primaryKeyName = this.constructor.primaryKey()
|
|
266
406
|
const ransackParams = {}
|
|
267
407
|
ransackParams[`${primaryKeyName}_eq`] = this.primaryKey()
|
|
268
408
|
|
|
@@ -285,27 +425,32 @@ class BaseModel {
|
|
|
285
425
|
}
|
|
286
426
|
}
|
|
287
427
|
|
|
288
|
-
getAttributes ()
|
|
289
|
-
return Object.assign(this.modelData, this.changes)
|
|
290
|
-
}
|
|
428
|
+
getAttributes = () => Object.assign(this.modelData, this.changes)
|
|
291
429
|
|
|
292
|
-
handleResponseError
|
|
430
|
+
handleResponseError(response) {
|
|
293
431
|
BaseModel.parseValidationErrors({model: this, response})
|
|
294
432
|
throw new new CustomError("Response wasn't successful", {model: this, response})
|
|
295
433
|
}
|
|
296
434
|
|
|
297
|
-
identifierKey
|
|
435
|
+
identifierKey() {
|
|
298
436
|
if (!this._identifierKey) this._identifierKey = this.isPersisted() ? this.primaryKey() : this.uniqueKey()
|
|
299
437
|
|
|
300
438
|
return this._identifierKey
|
|
301
439
|
}
|
|
302
440
|
|
|
303
|
-
isAssociationLoaded (associationName)
|
|
304
|
-
|
|
441
|
+
isAssociationLoaded = (associationName) => this.isAssociationLoadedUnderscore(inflection.underscore(associationName))
|
|
442
|
+
isAssociationLoadedUnderscore (associationNameUnderscore) {
|
|
443
|
+
if (associationNameUnderscore in this.relationshipsCache) return true
|
|
444
|
+
return false
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
isAssociationPresent(associationName) {
|
|
448
|
+
if (this.isAssociationLoaded(associationName)) return true
|
|
449
|
+
if (associationName in this.relationships) return true
|
|
305
450
|
return false
|
|
306
451
|
}
|
|
307
452
|
|
|
308
|
-
static parseValidationErrors
|
|
453
|
+
static parseValidationErrors({error, model, options}) {
|
|
309
454
|
if (!(error instanceof ValidationError)) return
|
|
310
455
|
if (!error.args.response.validation_errors) return
|
|
311
456
|
|
|
@@ -317,11 +462,11 @@ class BaseModel {
|
|
|
317
462
|
BaseModel.sendValidationErrorsEvent(validationErrors, options)
|
|
318
463
|
|
|
319
464
|
if (!options || options.throwValidationError != false) {
|
|
320
|
-
throw
|
|
465
|
+
throw error
|
|
321
466
|
}
|
|
322
467
|
}
|
|
323
468
|
|
|
324
|
-
static humanAttributeName
|
|
469
|
+
static humanAttributeName(attributeName) {
|
|
325
470
|
const keyName = digg(this.modelClassData(), "i18nKey")
|
|
326
471
|
const i18n = Config.getI18n()
|
|
327
472
|
|
|
@@ -330,7 +475,7 @@ class BaseModel {
|
|
|
330
475
|
return inflection.humanize(attributeName)
|
|
331
476
|
}
|
|
332
477
|
|
|
333
|
-
isAttributeChanged
|
|
478
|
+
isAttributeChanged(attributeName) {
|
|
334
479
|
const attributeNameUnderscore = inflection.underscore(attributeName)
|
|
335
480
|
const attributeData = this.modelClassData().attributes.find((attribute) => digg(attribute, "name") == attributeNameUnderscore)
|
|
336
481
|
|
|
@@ -353,7 +498,7 @@ class BaseModel {
|
|
|
353
498
|
return changedMethod(oldValue, newValue)
|
|
354
499
|
}
|
|
355
500
|
|
|
356
|
-
isChanged
|
|
501
|
+
isChanged() {
|
|
357
502
|
const keys = Object.keys(this.changes)
|
|
358
503
|
|
|
359
504
|
if (keys.length > 0) {
|
|
@@ -363,7 +508,7 @@ class BaseModel {
|
|
|
363
508
|
}
|
|
364
509
|
}
|
|
365
510
|
|
|
366
|
-
isNewRecord
|
|
511
|
+
isNewRecord() {
|
|
367
512
|
if (this.newRecord !== undefined) {
|
|
368
513
|
return this.newRecord
|
|
369
514
|
} else if ("id" in this.modelData && this.modelData.id) {
|
|
@@ -373,15 +518,11 @@ class BaseModel {
|
|
|
373
518
|
}
|
|
374
519
|
}
|
|
375
520
|
|
|
376
|
-
isPersisted ()
|
|
377
|
-
return !this.isNewRecord()
|
|
378
|
-
}
|
|
521
|
+
isPersisted = () => !this.isNewRecord()
|
|
379
522
|
|
|
380
|
-
static snakeCase (string)
|
|
381
|
-
return inflection.underscore(string)
|
|
382
|
-
}
|
|
523
|
+
static snakeCase = (string) => inflection.underscore(string)
|
|
383
524
|
|
|
384
|
-
savedChangeToAttribute
|
|
525
|
+
savedChangeToAttribute(attributeName) {
|
|
385
526
|
if (!this.previousModelData)
|
|
386
527
|
return false
|
|
387
528
|
|
|
@@ -407,24 +548,34 @@ class BaseModel {
|
|
|
407
548
|
return changedMethod(oldValue, newValue)
|
|
408
549
|
}
|
|
409
550
|
|
|
410
|
-
setNewModel
|
|
551
|
+
setNewModel(model) {
|
|
411
552
|
this.setNewModelData(model)
|
|
412
|
-
|
|
553
|
+
|
|
554
|
+
for(const relationshipName in model.relationships) {
|
|
555
|
+
this.relationships[relationshipName] = model.relationships[relationshipName]
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
for(const relationshipCacheName in model.relationshipsCache) {
|
|
559
|
+
this.relationshipsCache[relationshipCacheName] = model.relationshipsCache[name]
|
|
560
|
+
}
|
|
413
561
|
}
|
|
414
562
|
|
|
415
|
-
setNewModelData
|
|
563
|
+
setNewModelData(model) {
|
|
416
564
|
if (!("modelData" in model)) throw new Error(`No modelData in model: ${JSON.stringify(model)}`)
|
|
417
565
|
|
|
418
|
-
this.previousModelData = digg(this, "modelData")
|
|
419
|
-
|
|
566
|
+
this.previousModelData = Object.assign({}, digg(this, "modelData"))
|
|
567
|
+
|
|
568
|
+
for(const attributeName in model.modelData) {
|
|
569
|
+
this.modelData[attributeName] = model.modelData[attributeName]
|
|
570
|
+
}
|
|
420
571
|
}
|
|
421
572
|
|
|
422
|
-
_isDateChanged
|
|
573
|
+
_isDateChanged(oldValue, newValue) {
|
|
423
574
|
if (Date.parse(oldValue) != Date.parse(newValue))
|
|
424
575
|
return true
|
|
425
576
|
}
|
|
426
577
|
|
|
427
|
-
_isIntegerChanged
|
|
578
|
+
_isIntegerChanged(oldValue, newValue) {
|
|
428
579
|
if (parseInt(oldValue, 10) != parseInt(newValue, 10))
|
|
429
580
|
return true
|
|
430
581
|
}
|
|
@@ -437,15 +588,12 @@ class BaseModel {
|
|
|
437
588
|
return true
|
|
438
589
|
}
|
|
439
590
|
|
|
440
|
-
modelClassData ()
|
|
441
|
-
return this.constructor.modelClassData()
|
|
442
|
-
}
|
|
591
|
+
modelClassData = () => this.constructor.modelClassData()
|
|
443
592
|
|
|
444
|
-
async reload
|
|
593
|
+
async reload() {
|
|
445
594
|
const params = this.collection && this.collection.params()
|
|
446
|
-
const primaryKeyName = this.modelClassData().primaryKey
|
|
447
595
|
const ransackParams = {}
|
|
448
|
-
ransackParams[`${
|
|
596
|
+
ransackParams[`${this.constructor.primaryKey()}_eq`] = this.primaryKey()
|
|
449
597
|
|
|
450
598
|
let query = this.constructor.ransack(ransackParams)
|
|
451
599
|
|
|
@@ -468,7 +616,7 @@ class BaseModel {
|
|
|
468
616
|
this.changes = {}
|
|
469
617
|
}
|
|
470
618
|
|
|
471
|
-
save
|
|
619
|
+
save() {
|
|
472
620
|
if (this.isNewRecord()) {
|
|
473
621
|
return this.create()
|
|
474
622
|
} else {
|
|
@@ -476,7 +624,7 @@ class BaseModel {
|
|
|
476
624
|
}
|
|
477
625
|
}
|
|
478
626
|
|
|
479
|
-
saveRaw
|
|
627
|
+
saveRaw(rawData, options = {}) {
|
|
480
628
|
if (this.isNewRecord()) {
|
|
481
629
|
return this.createRaw(rawData, options)
|
|
482
630
|
} else {
|
|
@@ -484,9 +632,10 @@ class BaseModel {
|
|
|
484
632
|
}
|
|
485
633
|
}
|
|
486
634
|
|
|
487
|
-
async update
|
|
488
|
-
if (newAttributes)
|
|
635
|
+
async update(newAttributes, options) {
|
|
636
|
+
if (newAttributes) {
|
|
489
637
|
this.assignAttributes(newAttributes)
|
|
638
|
+
}
|
|
490
639
|
|
|
491
640
|
if (Object.keys(this.changes).length == 0) {
|
|
492
641
|
return {model: this}
|
|
@@ -529,7 +678,7 @@ class BaseModel {
|
|
|
529
678
|
}
|
|
530
679
|
}
|
|
531
680
|
|
|
532
|
-
_refreshModelFromResponse
|
|
681
|
+
_refreshModelFromResponse(response) {
|
|
533
682
|
let newModel = digg(response, "model")
|
|
534
683
|
|
|
535
684
|
if (Array.isArray(newModel)) newModel = newModel[0]
|
|
@@ -537,7 +686,7 @@ class BaseModel {
|
|
|
537
686
|
this.setNewModel(newModel)
|
|
538
687
|
}
|
|
539
688
|
|
|
540
|
-
_refreshModelDataFromResponse
|
|
689
|
+
_refreshModelDataFromResponse(response) {
|
|
541
690
|
let newModel = digg(response, "model")
|
|
542
691
|
|
|
543
692
|
if (Array.isArray(newModel)) newModel = newModel[0]
|
|
@@ -545,7 +694,7 @@ class BaseModel {
|
|
|
545
694
|
this.setNewModelData(newModel)
|
|
546
695
|
}
|
|
547
696
|
|
|
548
|
-
static _objectDataFromGivenRawData
|
|
697
|
+
static _objectDataFromGivenRawData(rawData, options) {
|
|
549
698
|
if (rawData instanceof FormData || rawData.nodeName == "FORM") {
|
|
550
699
|
const formData = FormDataObjectizer.formDataFromObject(rawData, options)
|
|
551
700
|
|
|
@@ -555,7 +704,7 @@ class BaseModel {
|
|
|
555
704
|
return rawData
|
|
556
705
|
}
|
|
557
706
|
|
|
558
|
-
async updateRaw
|
|
707
|
+
async updateRaw(rawData, options = {}) {
|
|
559
708
|
const objectData = BaseModel._objectDataFromGivenRawData(rawData, options)
|
|
560
709
|
let response
|
|
561
710
|
|
|
@@ -564,7 +713,8 @@ class BaseModel {
|
|
|
564
713
|
{
|
|
565
714
|
args: {
|
|
566
715
|
query_params: this.collection && this.collection.params(),
|
|
567
|
-
save: objectData
|
|
716
|
+
save: objectData,
|
|
717
|
+
simple_model_errors: options?.simpleModelErrors
|
|
568
718
|
},
|
|
569
719
|
command: `${this.modelClassData().collectionName}-update`,
|
|
570
720
|
collectionName: this.modelClassData().collectionName,
|
|
@@ -586,11 +736,11 @@ class BaseModel {
|
|
|
586
736
|
return {response, model: this}
|
|
587
737
|
}
|
|
588
738
|
|
|
589
|
-
isValid
|
|
739
|
+
isValid() {
|
|
590
740
|
throw new Error("Not implemented yet")
|
|
591
741
|
}
|
|
592
742
|
|
|
593
|
-
async isValidOnServer
|
|
743
|
+
async isValidOnServer() {
|
|
594
744
|
const modelData = this.getAttributes()
|
|
595
745
|
const paramKey = this.modelClassData().paramKey
|
|
596
746
|
const dataToUse = {}
|
|
@@ -612,15 +762,20 @@ class BaseModel {
|
|
|
612
762
|
return {valid: response.valid, errors: response.errors}
|
|
613
763
|
}
|
|
614
764
|
|
|
615
|
-
modelClass ()
|
|
616
|
-
return this.constructor
|
|
617
|
-
}
|
|
765
|
+
modelClass = () => this.constructor
|
|
618
766
|
|
|
619
|
-
preloadRelationship
|
|
767
|
+
preloadRelationship(relationshipName, model) {
|
|
620
768
|
this.relationshipsCache[BaseModel.snakeCase(relationshipName)] = model
|
|
769
|
+
this.relationships[BaseModel.snakeCase(relationshipName)] = model
|
|
621
770
|
}
|
|
622
771
|
|
|
623
|
-
|
|
772
|
+
markForDestruction() {
|
|
773
|
+
this._markedForDestruction = true
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
markedForDestruction = () => this._markedForDestruction
|
|
777
|
+
|
|
778
|
+
uniqueKey() {
|
|
624
779
|
if (!this.uniqueKeyValue) {
|
|
625
780
|
const min = 5000000000000000
|
|
626
781
|
const max = 9007199254740991
|
|
@@ -631,25 +786,29 @@ class BaseModel {
|
|
|
631
786
|
return this.uniqueKeyValue
|
|
632
787
|
}
|
|
633
788
|
|
|
634
|
-
static async _callCollectionCommand
|
|
789
|
+
static async _callCollectionCommand(args, commandArgs) {
|
|
635
790
|
const formOrDataObject = args.args
|
|
636
791
|
|
|
637
792
|
try {
|
|
638
793
|
return await CommandsPool.addCommand(args, commandArgs)
|
|
639
794
|
} catch (error) {
|
|
640
|
-
|
|
641
|
-
|
|
795
|
+
let form
|
|
796
|
+
|
|
797
|
+
if (commandArgs.form) {
|
|
798
|
+
form = commandArgs.form
|
|
799
|
+
} else if (formOrDataObject?.nodeName == "FORM") {
|
|
800
|
+
form = formOrDataObject
|
|
642
801
|
}
|
|
643
802
|
|
|
803
|
+
if (form) BaseModel.parseValidationErrors({error, options: {form}})
|
|
804
|
+
|
|
644
805
|
throw error
|
|
645
806
|
}
|
|
646
807
|
}
|
|
647
808
|
|
|
648
|
-
_callMemberCommand (args, commandArgs)
|
|
649
|
-
return CommandsPool.addCommand(args, commandArgs)
|
|
650
|
-
}
|
|
809
|
+
_callMemberCommand = (args, commandArgs) => CommandsPool.addCommand(args, commandArgs)
|
|
651
810
|
|
|
652
|
-
static _postDataFromArgs
|
|
811
|
+
static _postDataFromArgs(args) {
|
|
653
812
|
let postData
|
|
654
813
|
|
|
655
814
|
if (args) {
|
|
@@ -665,13 +824,13 @@ class BaseModel {
|
|
|
665
824
|
return postData
|
|
666
825
|
}
|
|
667
826
|
|
|
668
|
-
readAttribute
|
|
827
|
+
readAttribute(attributeName) {
|
|
669
828
|
const attributeNameUnderscore = inflection.underscore(attributeName)
|
|
670
829
|
|
|
671
830
|
return this.readAttributeUnderscore(attributeNameUnderscore)
|
|
672
831
|
}
|
|
673
832
|
|
|
674
|
-
readAttributeUnderscore
|
|
833
|
+
readAttributeUnderscore(attributeName) {
|
|
675
834
|
if (attributeName in this.changes) {
|
|
676
835
|
return this.changes[attributeName]
|
|
677
836
|
} else if (attributeName in this.modelData) {
|
|
@@ -688,7 +847,7 @@ class BaseModel {
|
|
|
688
847
|
}
|
|
689
848
|
}
|
|
690
849
|
|
|
691
|
-
isAttributeLoaded
|
|
850
|
+
isAttributeLoaded(attributeName) {
|
|
692
851
|
const attributeNameUnderscore = inflection.underscore(attributeName)
|
|
693
852
|
|
|
694
853
|
if (attributeNameUnderscore in this.changes) return true
|
|
@@ -696,7 +855,7 @@ class BaseModel {
|
|
|
696
855
|
return false
|
|
697
856
|
}
|
|
698
857
|
|
|
699
|
-
_isPresent
|
|
858
|
+
_isPresent(value) {
|
|
700
859
|
if (!value) {
|
|
701
860
|
return false
|
|
702
861
|
} else if (typeof value == "string" && value.match(/^\s*$/)) {
|
|
@@ -706,8 +865,10 @@ class BaseModel {
|
|
|
706
865
|
return true
|
|
707
866
|
}
|
|
708
867
|
|
|
709
|
-
async _loadBelongsToReflection
|
|
710
|
-
if (args.reflectionName in this.
|
|
868
|
+
async _loadBelongsToReflection(args, queryArgs = {}) {
|
|
869
|
+
if (args.reflectionName in this.relationships) {
|
|
870
|
+
return this.relationships[args.reflectionName]
|
|
871
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
711
872
|
return this.relationshipsCache[args.reflectionName]
|
|
712
873
|
} else {
|
|
713
874
|
const collection = new Collection(args, queryArgs)
|
|
@@ -717,22 +878,25 @@ class BaseModel {
|
|
|
717
878
|
}
|
|
718
879
|
}
|
|
719
880
|
|
|
720
|
-
_readBelongsToReflection
|
|
721
|
-
if (
|
|
722
|
-
|
|
723
|
-
|
|
881
|
+
_readBelongsToReflection({reflectionName}) {
|
|
882
|
+
if (reflectionName in this.relationships) {
|
|
883
|
+
return this.relationships[reflectionName]
|
|
884
|
+
} else if (reflectionName in this.relationshipsCache) {
|
|
885
|
+
return this.relationshipsCache[reflectionName]
|
|
886
|
+
}
|
|
724
887
|
|
|
725
|
-
|
|
726
|
-
const modelClassName = digg(this.modelClassData(), "name")
|
|
888
|
+
if (this.isNewRecord()) return null
|
|
727
889
|
|
|
728
|
-
|
|
729
|
-
|
|
890
|
+
const loadedRelationships = Object.keys(this.relationshipsCache)
|
|
891
|
+
const modelClassName = digg(this.modelClassData(), "name")
|
|
730
892
|
|
|
731
|
-
|
|
893
|
+
throw new NotLoadedError(`${modelClassName}#${reflectionName} hasn't been loaded yet. Only these were loaded: ${loadedRelationships.join(", ")}`)
|
|
732
894
|
}
|
|
733
895
|
|
|
734
|
-
async _loadHasManyReflection
|
|
735
|
-
if (args.reflectionName in this.
|
|
896
|
+
async _loadHasManyReflection(args, queryArgs = {}) {
|
|
897
|
+
if (args.reflectionName in this.relationships) {
|
|
898
|
+
return this.relationships[args.reflectionName]
|
|
899
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
736
900
|
return this.relationshipsCache[args.reflectionName]
|
|
737
901
|
}
|
|
738
902
|
|
|
@@ -744,8 +908,10 @@ class BaseModel {
|
|
|
744
908
|
return models
|
|
745
909
|
}
|
|
746
910
|
|
|
747
|
-
async _loadHasOneReflection
|
|
748
|
-
if (args.reflectionName in this.
|
|
911
|
+
async _loadHasOneReflection(args, queryArgs = {}) {
|
|
912
|
+
if (args.reflectionName in this.relationships) {
|
|
913
|
+
return this.relationships[args.reflectionName]
|
|
914
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
749
915
|
return this.relationshipsCache[args.reflectionName]
|
|
750
916
|
} else {
|
|
751
917
|
const collection = new Collection(args, queryArgs)
|
|
@@ -757,28 +923,31 @@ class BaseModel {
|
|
|
757
923
|
}
|
|
758
924
|
}
|
|
759
925
|
|
|
760
|
-
_readHasOneReflection
|
|
761
|
-
if (
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
const modelClassName = digg(this.modelClassData(), "name")
|
|
926
|
+
_readHasOneReflection({reflectionName}) {
|
|
927
|
+
if (reflectionName in this.relationships) {
|
|
928
|
+
return this.relationships[reflectionName]
|
|
929
|
+
} else if (reflectionName in this.relationshipsCache) {
|
|
930
|
+
return this.relationshipsCache[reflectionName]
|
|
931
|
+
}
|
|
767
932
|
|
|
768
|
-
|
|
933
|
+
if (this.isNewRecord()) {
|
|
934
|
+
return null
|
|
769
935
|
}
|
|
770
936
|
|
|
771
|
-
|
|
937
|
+
const loadedRelationships = Object.keys(this.relationshipsCache)
|
|
938
|
+
const modelClassName = digg(this.modelClassData(), "name")
|
|
939
|
+
|
|
940
|
+
throw new NotLoadedError(`${modelClassName}#${reflectionName} hasn't been loaded yet. Only these were loaded: ${loadedRelationships.join(", ")}`)
|
|
772
941
|
}
|
|
773
942
|
|
|
774
|
-
_readModelDataFromArgs
|
|
943
|
+
_readModelDataFromArgs(args) {
|
|
775
944
|
this.abilities = args.data.b || {}
|
|
776
945
|
this.collection = args.collection
|
|
777
|
-
this.modelData = args.data.a
|
|
946
|
+
this.modelData = objectToUnderscore(args.data.a)
|
|
778
947
|
this.preloadedRelationships = args.data.r
|
|
779
948
|
}
|
|
780
949
|
|
|
781
|
-
_readPreloadedRelationships
|
|
950
|
+
_readPreloadedRelationships(preloaded) {
|
|
782
951
|
if (!this.preloadedRelationships) {
|
|
783
952
|
return
|
|
784
953
|
}
|
|
@@ -808,36 +977,25 @@ class BaseModel {
|
|
|
808
977
|
|
|
809
978
|
if (!relationshipData) {
|
|
810
979
|
this.relationshipsCache[relationshipName] = null
|
|
980
|
+
this.relationships[relationshipName] = null
|
|
811
981
|
} else if (Array.isArray(relationshipData)) {
|
|
812
|
-
|
|
982
|
+
this.relationshipsCache[relationshipName] = []
|
|
983
|
+
this.relationships[relationshipName] = []
|
|
813
984
|
|
|
814
985
|
for (const relationshipId of relationshipData) {
|
|
815
986
|
const model = preloaded.getModel(relationshipType, relationshipId)
|
|
816
987
|
|
|
817
|
-
|
|
988
|
+
this.relationshipsCache[relationshipName].push(model)
|
|
989
|
+
this.relationships[relationshipName].push(model)
|
|
818
990
|
}
|
|
819
|
-
|
|
820
|
-
this.relationshipsCache[relationshipName] = result
|
|
821
991
|
} else {
|
|
822
992
|
const model = preloaded.getModel(relationshipType, relationshipData)
|
|
993
|
+
|
|
823
994
|
this.relationshipsCache[relationshipName] = model
|
|
995
|
+
this.relationships[relationshipName] = model
|
|
824
996
|
}
|
|
825
997
|
}
|
|
826
998
|
}
|
|
827
999
|
|
|
828
|
-
primaryKey ()
|
|
829
|
-
return this.readAttributeUnderscore(digg(this.modelClassData(), "primaryKey"))
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
static _token () {
|
|
833
|
-
const csrfTokenElement = document.querySelector("meta[name='csrf-token']")
|
|
834
|
-
|
|
835
|
-
if (csrfTokenElement) {
|
|
836
|
-
return csrfTokenElement.getAttribute("content")
|
|
837
|
-
}
|
|
838
|
-
}
|
|
1000
|
+
primaryKey = () => this.readAttributeUnderscore(this.constructor.primaryKey())
|
|
839
1001
|
}
|
|
840
|
-
|
|
841
|
-
BaseModel.apiMakerType = "BaseModel"
|
|
842
|
-
|
|
843
|
-
export default BaseModel
|