From 7299f41527128af83fa0a44b41827212c7a50563 Mon Sep 17 00:00:00 2001 From: Shiran Pasternak Date: Thu, 28 Sep 2023 15:45:16 -0400 Subject: [PATCH] Sets proper name on record type for identification Also supports a custom name on the `@Model()` decorator. --- desktop/src/@batch-flask/core/record/decorators.ts | 7 ++++--- .../src/@batch-flask/core/record/record.spec.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/desktop/src/@batch-flask/core/record/decorators.ts b/desktop/src/@batch-flask/core/record/decorators.ts index 92f5c8f895..628d77f593 100644 --- a/desktop/src/@batch-flask/core/record/decorators.ts +++ b/desktop/src/@batch-flask/core/record/decorators.ts @@ -56,13 +56,12 @@ export function ListProp(type: any) { }; } -export function Model() { +export function Model(name?: string) { return {}>(ctr: T) => { if (!(ctr.prototype instanceof Record)) { throw new RecordMissingExtendsError(ctr); } - - return (class extends ctr { + const model = (class extends ctr { constructor(...args: any[]) { const [data] = args; if (data instanceof ctr) { @@ -72,5 +71,7 @@ export function Model() { (this as any)._completeInitialization(); } }); + Object.defineProperty(model, "name", { value: name || ctr.name }); + return model; }; } diff --git a/desktop/src/@batch-flask/core/record/record.spec.ts b/desktop/src/@batch-flask/core/record/record.spec.ts index 1634d61473..3cfab632f8 100644 --- a/desktop/src/@batch-flask/core/record/record.spec.ts +++ b/desktop/src/@batch-flask/core/record/record.spec.ts @@ -44,6 +44,12 @@ class InheritedTestRec extends SimpleTestRec { public d: number; } +@Model("CustomName") +class CustomNameRec extends Record { + @Prop() + public id: string = "default-id"; +} + describe("Record", () => { it("should throw an exeption when record doesn't extends Record class", () => { try { @@ -176,4 +182,12 @@ describe("Record", () => { expect(SimpleTestRec.isStaticMethod).not.toBeFalsy(); expect(SimpleTestRec.isStaticMethod()).toBe(true); }); + + it("should allow a custom record name to be set", () => { + const rec1 = new TestRec(); + expect(rec1.constructor.name).toEqual("TestRec"); + + const rec2 = new CustomNameRec(); + expect(rec2.constructor.name).toEqual("CustomName"); + }); });