@kaspernj/api-maker 1.0.224 → 1.0.227

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/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.224",
19
+ "version": "1.0.227",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -6,6 +6,7 @@ import CustomError from "./custom-error.mjs"
6
6
  import {digg} from "diggerize"
7
7
  import FormDataObjectizer from "form-data-objectizer"
8
8
  import inflection from "inflection"
9
+ import modelClassRequire from "./model-class-require.mjs"
9
10
  import ModelName from "./model-name.mjs"
10
11
  import NotLoadedError from "./not-loaded-error.mjs"
11
12
  import objectToFormData from "object-to-formdata"
@@ -35,6 +36,26 @@ class ApiMakerAttribute {
35
36
  }
36
37
  }
37
38
 
39
+ class ApiMakerReflection {
40
+ constructor(reflectionData) {
41
+ this.reflectionData = reflectionData
42
+ }
43
+
44
+ macro() {
45
+ return digg(this, "reflectionData", "macro")
46
+ }
47
+
48
+ modelClass() {
49
+ const modelClass = modelClassRequire(inflection.singularize(inflection.camelize(digg(this, "reflectionData", "name"))))
50
+
51
+ return modelClass
52
+ }
53
+
54
+ name() {
55
+ return inflection.camelize(digg(this, "reflectionData", "name"), true)
56
+ }
57
+ }
58
+
38
59
  class BaseModel {
39
60
  static attributes() {
40
61
  const attributes = digg(this.modelClassData(), "attributes")
@@ -65,10 +86,10 @@ class BaseModel {
65
86
  }
66
87
  }
67
88
 
68
- static async find (id) {
69
- const primaryKeyName = this.modelClassData().primaryKey
89
+ static async find(id) {
70
90
  const query = {}
71
- query[`${primaryKeyName}_eq`] = id
91
+
92
+ query[`${this.primaryKey()}_eq`] = id
72
93
 
73
94
  const model = await this.ransack(query).first()
74
95
 
@@ -79,7 +100,7 @@ class BaseModel {
79
100
  }
80
101
  }
81
102
 
82
- static async findOrCreateBy (findOrCreateByArgs, args = {}) {
103
+ static async findOrCreateBy(findOrCreateByArgs, args = {}) {
83
104
  const result = await Services.current().sendRequest("Models::FindOrCreateBy", {
84
105
  additional_data: args.additionalData,
85
106
  find_or_create_by_args: findOrCreateByArgs,
@@ -90,7 +111,7 @@ class BaseModel {
90
111
  return model
91
112
  }
92
113
 
93
- static modelName () {
114
+ static modelName() {
94
115
  return new ModelName({modelClassData: this.modelClassData()})
95
116
  }
96
117
 
@@ -98,10 +119,23 @@ class BaseModel {
98
119
  return digg(this.modelClassData(), "primaryKey")
99
120
  }
100
121
 
101
- static ransack (query = {}) {
122
+ static ransack(query = {}) {
102
123
  return new Collection({modelClass: this}, {ransack: query})
103
124
  }
104
125
 
126
+ static reflections() {
127
+ const relationships = digg(this.modelClassData(), "relationships")
128
+ const reflections = []
129
+
130
+ for (const relationshipData of relationships) {
131
+ const reflection = new ApiMakerReflection(relationshipData)
132
+
133
+ reflections.push(reflection)
134
+ }
135
+
136
+ return reflections
137
+ }
138
+
105
139
  constructor (args = {}) {
106
140
  this.changes = {}
107
141
  this.newRecord = args.isNewRecord
@@ -302,7 +336,7 @@ class BaseModel {
302
336
 
303
337
  // Load the missing abilities if any
304
338
  if (abilitiesToLoad.length > 0) {
305
- const primaryKeyName = this.modelClassData().primaryKey
339
+ const primaryKeyName = this.constructor.primaryKey()
306
340
  const ransackParams = {}
307
341
  ransackParams[`${primaryKeyName}_eq`] = this.primaryKey()
308
342
 
@@ -483,9 +517,8 @@ class BaseModel {
483
517
 
484
518
  async reload () {
485
519
  const params = this.collection && this.collection.params()
486
- const primaryKeyName = this.modelClassData().primaryKey
487
520
  const ransackParams = {}
488
- ransackParams[`${primaryKeyName}_eq`] = this.primaryKey()
521
+ ransackParams[`${this.constructor.primaryKey()}_eq`] = this.primaryKey()
489
522
 
490
523
  let query = this.constructor.ransack(ransackParams)
491
524
 
@@ -866,7 +899,7 @@ class BaseModel {
866
899
  }
867
900
 
868
901
  primaryKey () {
869
- return this.readAttributeUnderscore(digg(this.modelClassData(), "primaryKey"))
902
+ return this.readAttributeUnderscore(this.constructor.primaryKey())
870
903
  }
871
904
 
872
905
  static _token () {
@@ -24,10 +24,7 @@ export default class CollectionLoader extends React.PureComponent {
24
24
  abilities: PropTypes.object,
25
25
  appHistory: PropTypes.object,
26
26
  className: PropTypes.string,
27
- collection: PropTypes.oneOfType([
28
- instanceOfClassName("ApiMakerCollection"),
29
- PropTypes.instanceOf(Collection)
30
- ]),
27
+ collection: PropTypes.instanceOf(Collection),
31
28
  component: PropTypes.object,
32
29
  defaultParams: PropTypes.object,
33
30
  groupBy: PropTypes.array,
@@ -22,9 +22,11 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
22
22
  }
23
23
 
24
24
  resolveModelClass(modelClassArg) {
25
- const {queryParams} = digs(this.props, "queryParams")
25
+ if (typeof modelClassArg == "object") {
26
+ const {queryParams} = digs(this.props, "queryParams")
26
27
 
27
- if (typeof modelClassArg == "object") return modelClassArg.callback({queryParams})
28
+ return modelClassArg.callback({queryParams})
29
+ }
28
30
 
29
31
  return modelClassArg
30
32
  }
@@ -77,6 +79,7 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
77
79
 
78
80
  async loadNewModel() {
79
81
  const params = Params.parse()
82
+ const ModelClass = digg(this, "modelClass")
80
83
  const paramKey = ModelClass.modelName().paramKey()
81
84
  const modelDataFromParams = params[paramKey] || {}
82
85
 
@@ -10,7 +10,10 @@ const result = loader.load()
10
10
  <% model_class_names = [] %>
11
11
  <% ApiMaker::ModelClassesJavaScriptGeneratorService.new.resources.each do |resource| %>
12
12
  <% model_class_names << resource.short_name %>
13
- const <%= resource.short_name %> = digg(result, "<%= resource.short_name %>")
13
+
14
+ const <%= resource.short_name %>ModelClass = digg(result, "<%= resource.short_name %>")
15
+
16
+ class <%= resource.short_name %> extends <%= resource.short_name %>ModelClass {}
14
17
  <% end %>
15
18
 
16
19
  export {<%= model_class_names.join(", ") %>}
@@ -0,0 +1,24 @@
1
+ import {digs} from "diggerize"
2
+ import ModelClassTable from "./model-class-table"
3
+ import PropTypes from "prop-types"
4
+ import React from "react"
5
+
6
+ export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
7
+ static propTypes = {
8
+ currentUser: PropTypes.object,
9
+ modelClass: PropTypes.func.isRequired,
10
+ queryParams: PropTypes.object.isRequired
11
+ }
12
+
13
+ render() {
14
+ const {currentUser, modelClass, queryParams} = digs(this.props, "currentUser", "modelClass", "queryParams")
15
+
16
+ return (
17
+ <ModelClassTable
18
+ currentUser={currentUser}
19
+ modelClass={modelClass}
20
+ queryParams={queryParams}
21
+ />
22
+ )
23
+ }
24
+ }
@@ -4,11 +4,13 @@ import Layout from "./layout"
4
4
  import * as modelsModule from "@kaspernj/api-maker/src/models.mjs.erb"
5
5
  import PropTypes from "prop-types"
6
6
  import ShowPage from "./show-page"
7
+ import ShowReflectionPage from "./show-reflection-page"
7
8
  import withQueryParams from "on-location-changed/src/with-query-params"
8
9
 
9
10
  class ApiMakerSuperAdmin extends React.PureComponent {
10
11
  static propTypes = {
11
- currentUser: PropTypes.object
12
+ currentUser: PropTypes.object,
13
+ queryParams: PropTypes.object.isRequired
12
14
  }
13
15
 
14
16
  render() {
@@ -31,9 +33,19 @@ class ApiMakerSuperAdmin extends React.PureComponent {
31
33
  }
32
34
  {pageToShow == "show" &&
33
35
  <ShowPage
34
- key={`show-page-${digg(modelClass.modelClassData(), "name")}`}
36
+ key={`show-page-${digg(modelClass.modelClassData(), "name")}-${queryParams.modelId}`}
35
37
  modelClass={modelClass}
36
38
  modelId={queryParams.modelId}
39
+ queryParams={queryParams}
40
+ />
41
+ }
42
+ {pageToShow == "show_reflection" &&
43
+ <ShowReflectionPage
44
+ currentUser={currentUser}
45
+ key={`show-reflection-page-${digg(modelClass.modelClassData(), "name")}-${queryParams.modelId}`}
46
+ modelClass={modelClass}
47
+ modelId={queryParams.modelId}
48
+ queryParams={queryParams}
37
49
  />
38
50
  }
39
51
  </Layout>
@@ -43,7 +55,9 @@ class ApiMakerSuperAdmin extends React.PureComponent {
43
55
  pageToShow() {
44
56
  const {queryParams} = digs(this.props, "queryParams")
45
57
 
46
- if (queryParams.model && queryParams.model_id) {
58
+ if (queryParams.model && queryParams.model_id && queryParams.model_reflection) {
59
+ return "show_reflection"
60
+ } else if (queryParams.model && queryParams.model_id) {
47
61
  return "show"
48
62
  } else if (queryParams.model) {
49
63
  return "index"
@@ -1,11 +1,11 @@
1
- import ConfigReader from "../config-reader"
1
+ import ConfigReader from "./config-reader"
2
2
  import {digg, digs} from "diggerize"
3
- import Params from "../../params"
3
+ import Params from "../params"
4
4
  import PropTypes from "prop-types"
5
5
  import React from "react"
6
- import Table from "../../table/table"
6
+ import Table from "../table/table"
7
7
 
8
- export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
8
+ export default class ApiMakerSuperAdminModelClassTable extends React.PureComponent {
9
9
  static propTypes = {
10
10
  currentUser: PropTypes.object,
11
11
  modelClass: PropTypes.func.isRequired,
@@ -13,7 +13,7 @@ export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
13
13
  }
14
14
 
15
15
  render() {
16
- const {currentUser, modelClass} = digs(this.props, "currentUser", "modelClass")
16
+ const {currentUser, modelClass, queryParams, ...restProps} = this.props
17
17
 
18
18
  return (
19
19
  <Table
@@ -21,6 +21,7 @@ export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
21
21
  currentUser={currentUser}
22
22
  modelClass={modelClass}
23
23
  viewModelPath={digg(this, "viewModelPath")}
24
+ {...restProps}
24
25
  />
25
26
  )
26
27
  }
@@ -0,0 +1,35 @@
1
+ import {digg, digs} from "diggerize"
2
+ import Link from "../link"
3
+ import PropTypes from "prop-types"
4
+ import PropTypesExact from "prop-types-exact"
5
+ import React from "react"
6
+
7
+ export default class ApiMakerSuperAdminShowNav extends React.PureComponent {
8
+ static propTypes = PropTypesExact({
9
+ model: PropTypes.object.isRequired,
10
+ modelClass: PropTypes.func.isRequired,
11
+ queryParams: PropTypes.object.isRequired
12
+ })
13
+
14
+ render() {
15
+ const {model, modelClass, queryParams} = digs(this.props, "model", "modelClass", "queryParams")
16
+ const reflections = modelClass.reflections()
17
+
18
+ return (
19
+ <div>
20
+ <div>
21
+ <Link to={Params.withParams({model: modelClass.modelClassData().name, model_id: queryParams.model_id})}>
22
+ {I18n.t("js.api_maker.suprt_admin.show_reflection_page.general", {defaultValue: "General"})}
23
+ </Link>
24
+ </div>
25
+ {model && reflections.filter((reflection) => reflection.macro() == "has_many").map((reflection) =>
26
+ <div key={reflection.name()}>
27
+ <Link to={Params.withParams({model: digg(modelClass.modelClassData(), "name"), model_id: model.primaryKey(), model_reflection: reflection.name()})}>
28
+ {modelClass.humanAttributeName(reflection.name())}
29
+ </Link>
30
+ </div>
31
+ )}
32
+ </div>
33
+ )
34
+ }
35
+ }
@@ -2,15 +2,26 @@ import AttributeRows from "../bootstrap/attribute-rows"
2
2
  import ConfigReader from "./config-reader"
3
3
  import {digg, digs} from "diggerize"
4
4
  import modelLoadWrapper from "../model-load-wrapper"
5
+ import PropTypes from "prop-types"
5
6
  import React from "react"
7
+ import ShowNav from "./show-nav"
6
8
 
7
9
  class ApiMakerSuperAdminShowPage extends React.PureComponent {
10
+ static propTypes = {
11
+ modelClass: PropTypes.func.isRequired,
12
+ queryParams: PropTypes.object.isRequired
13
+ }
14
+
8
15
  render() {
16
+ const {modelClass, queryParams} = digs(this.props, "modelClass", "queryParams")
9
17
  const attributes = this.attributes()
10
18
  const model = this.model()
11
19
 
12
20
  return (
13
21
  <div className="super-admin--show-page">
22
+ {model &&
23
+ <ShowNav model={model} modelClass={modelClass} queryParams={queryParams} />
24
+ }
14
25
  {attributes && model &&
15
26
  <AttributeRows attributes={attributes} model={model} />
16
27
  }
@@ -0,0 +1,64 @@
1
+ import {digg, digs} from "diggerize"
2
+ import modelLoadWrapper from "../model-load-wrapper"
3
+ import PropTypes from "prop-types"
4
+ import React from "react"
5
+ import ModelClassTable from "./model-class-table"
6
+ import ShowNav from "./show-nav"
7
+
8
+ class ApiMakerSuperAdminShowReflectionPage extends React.PureComponent {
9
+ static propTypes = {
10
+ currentUser: PropTypes.object,
11
+ modelClass: PropTypes.func.isRequired,
12
+ queryParams: PropTypes.object.isRequired
13
+ }
14
+
15
+ render() {
16
+ const {currentUser, modelClass, queryParams} = digs(this.props, "currentUser", "modelClass", "queryParams")
17
+ const model = this.model()
18
+ const reflections = modelClass.reflections()
19
+ const reflection = reflections.find((reflectionI) => reflectionI.name() == queryParams.model_reflection)
20
+ const reflectionModelClass = reflection.modelClass()
21
+ let collection
22
+
23
+ if (model) collection = model[reflection.name()]()
24
+
25
+ return (
26
+ <div className="super-admin--show-page">
27
+ {model &&
28
+ <ShowNav model={model} modelClass={modelClass} queryParams={queryParams} />
29
+ }
30
+ {collection &&
31
+ <ModelClassTable
32
+ collection={collection}
33
+ currentUser={currentUser}
34
+ key={reflectionModelClass.modelName().human()}
35
+ modelClass={reflectionModelClass}
36
+ queryParams={queryParams}
37
+ />
38
+ }
39
+ </div>
40
+ )
41
+ }
42
+
43
+ model() {
44
+ const {modelClass} = digs(this.props, "modelClass")
45
+ const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
46
+
47
+ return digg(this, "props", camelizedLower)
48
+ }
49
+ }
50
+
51
+ const modelClassResolver = {callback: ({queryParams}) => {
52
+ const modelClassName = digg(queryParams, "model")
53
+ const modelClass = digg(require("../models.mjs.erb"), modelClassName)
54
+
55
+ return modelClass
56
+ }}
57
+
58
+ export default modelLoadWrapper(
59
+ ApiMakerSuperAdminShowReflectionPage,
60
+ modelClassResolver,
61
+ {
62
+ loadByQueryParam: ({props}) => props.queryParams.model_id
63
+ }
64
+ )
@@ -39,10 +39,7 @@ class ApiMakerTable extends React.PureComponent {
39
39
  appHistory: PropTypes.object,
40
40
  card: PropTypes.bool.isRequired,
41
41
  className: PropTypes.string,
42
- collection: PropTypes.oneOfType([
43
- instanceOfClassName("ApiMakerCollection"),
44
- PropTypes.instanceOf(Collection)
45
- ]),
42
+ collection: PropTypes.instanceOf(Collection),
46
43
  columns: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
47
44
  columnsContent: PropTypes.func,
48
45
  controls: PropTypes.func,
@@ -134,7 +131,7 @@ class ApiMakerTable extends React.PureComponent {
134
131
 
135
132
  render () {
136
133
  const {modelClass, noRecordsAvailableContent, noRecordsFoundContent} = digs(this.props, "modelClass", "noRecordsAvailableContent", "noRecordsFoundContent")
137
- const {collection, defaultParams, select, selectColumns} = this.props
134
+ const {collection, defaultParams, selectColumns} = this.props
138
135
  const {
139
136
  overallCount,
140
137
  preload,