feat: generate random name for actions/views/tabs

This commit is contained in:
chenos 2020-12-07 23:24:43 +08:00
parent ae6ea41c52
commit ed43bfe86d
3 changed files with 30 additions and 1 deletions

View File

@ -0,0 +1,5 @@
import { BaseModel } from '../models';
export default async function (model: BaseModel) {
model.generateNameIfNull();
}

View File

@ -3,6 +3,7 @@ import collectionsAfterCreate from './collections-after-create';
import fieldsBeforeValidate from './fields-before-validate';
import fieldsAfterCreate from './fields-after-create';
import generateName from './generateName';
export default {
collections: {
@ -12,5 +13,14 @@ export default {
fields: {
beforeValidate: fieldsBeforeValidate,
afterCreate: fieldsAfterCreate
}
},
actions: {
beforeValidate: generateName
},
views: {
beforeValidate: generateName
},
tabs: {
beforeValidate: generateName
},
};

View File

@ -2,8 +2,22 @@ import _ from 'lodash';
import { getDataTypeKey, Model } from '@nocobase/database';
import { Utils } from 'sequelize';
export function generateName(title?: string): string {
return `${Math.random().toString(36).replace('0.', '').slice(-4).padStart(4, '0')}`;
}
export class BaseModel extends Model {
generateName() {
this.set('name', generateName());
}
generateNameIfNull() {
if (!this.get('name')) {
this.generateName();
}
}
get additionalAttribute() {
const tableOptions = this.database.getTable(this.constructor.name).getOptions();
return _.get(tableOptions, 'additionalAttribute') || 'options';