Skip to content

Commit 933a629

Browse files
committed
fix: remove file after set attributes Attachment at null
1 parent 486e11d commit 933a629

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/mixins/attachmentable.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,22 @@ import type { BaseModel } from '@adonisjs/lucid/orm'
99
import type { NormalizeConstructor } from '@adonisjs/core/types/helpers'
1010
import type { AttributeOfModelWithAttachment } from '../types/mixin.js'
1111

12-
import { beforeSave, afterSave, beforeDelete, afterFind, afterFetch, afterPaginate } from '@adonisjs/lucid/orm'
13-
import { persistAttachment, commit, rollback, generateVariants, preComputeUrl } from '../utils/actions.js'
14-
import { clone, getAttachmentAttributeNames } from '../utils/helpers.js'
12+
import {
13+
beforeSave,
14+
afterSave,
15+
beforeDelete,
16+
afterFind,
17+
afterFetch,
18+
afterPaginate,
19+
} from '@adonisjs/lucid/orm'
20+
import {
21+
persistAttachment,
22+
commit,
23+
rollback,
24+
generateVariants,
25+
preComputeUrl,
26+
} from '../utils/actions.js'
27+
import { clone, getAttachmentAttributeNames, getDirtyAttachmentAttributeNames } from '../utils/helpers.js'
1528
import { defaultStateAttributeMixin } from '../utils/default_values.js'
1629

1730
export const Attachmentable = <Model extends NormalizeConstructor<typeof BaseModel>>(
@@ -39,7 +52,7 @@ export const Attachmentable = <Model extends NormalizeConstructor<typeof BaseMod
3952

4053
@beforeSave()
4154
static async beforeSaveHook(modelInstance: ModelWithAttachment) {
42-
const attachmentAttributeNames = getAttachmentAttributeNames(modelInstance)
55+
const attachmentAttributeNames = getDirtyAttachmentAttributeNames(modelInstance)
4356

4457
/**
4558
* Empty previous $attachments
@@ -49,11 +62,9 @@ export const Attachmentable = <Model extends NormalizeConstructor<typeof BaseMod
4962
/**
5063
* Set attributes Attachment type modified
5164
*/
52-
attachmentAttributeNames.forEach((attributeName) => {
53-
if (modelInstance.$dirty[attributeName]) {
54-
modelInstance.$attachments.attributesModified.push(attributeName)
55-
}
56-
})
65+
attachmentAttributeNames.forEach((attributeName) =>
66+
modelInstance.$attachments.attributesModified.push(attributeName)
67+
)
5768

5869
/**
5970
* Persist attachments before saving the model to the database. This

src/utils/helpers.ts

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export function getAttachmentAttributeNames(modelInstance: ModelWithAttachment)
2525
)
2626
}
2727

28+
export function getDirtyAttachmentAttributeNames(modelInstance: ModelWithAttachment) {
29+
return Object.keys(modelInstance.$dirty).filter(
30+
(attr) => modelInstance.$dirty[attr] instanceof Attachment || modelInstance.$original[attr] instanceof Attachment
31+
)
32+
}
33+
2834
export function getOptions(
2935
modelInstance: ModelWithAttachment,
3036
attributeName: string

0 commit comments

Comments
 (0)