feat(32-02): record events from every admin write path
- Delete and each bulk-delete row fire RecordDeleted after commit - tree reorder fires RecordSaved per moved record (parent/sort in model types) - relation-manager child create/update/delete fire events naming the controller that edits the child's model - TestRecordEvents covers create, update, translated slug, no-op, rollback (validation, FormBeforeUpdate, extension Save), failing listener, reorder, child writes, delete and bulk delete; extension Save sees the changes - docs: Record events in extending.md, admin-controllers notes, cabana README
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/lagoon"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"git.golem15.com/golem15/summercms/modules/towel"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@@ -192,6 +193,10 @@ func (s RelationService) CreateChild(ctx context.Context, cc *CompiledController
|
||||
if err := applyMLTranslations(ctx, tx, s.writer, child, translations); err != nil {
|
||||
return err
|
||||
}
|
||||
changes, err := recordChanges(ctx, tx, s.writer, child, nil, translations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case parent.unsaved():
|
||||
// The child exists now, unattached; the parent's first save
|
||||
@@ -213,6 +218,7 @@ func (s RelationService) CreateChild(ctx context.Context, cc *CompiledController
|
||||
return lifecycleFailure(cc, err)
|
||||
}
|
||||
}
|
||||
s.emitChildSaved(ctx, tx, child, true, changes)
|
||||
result, err = projectFullRecord(ctx, tx, cr.child, child)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -342,6 +348,10 @@ func (s RelationService) UpdateChild(ctx context.Context, cc *CompiledController
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
before, err := takeRecordSnapshot(ctx, tx, s.writer, child, translations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.fillChild(ctx, tx, cr.child, child, in.Body, "update"); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -356,6 +366,10 @@ func (s RelationService) UpdateChild(ctx context.Context, cc *CompiledController
|
||||
if err := applyMLTranslations(ctx, tx, s.writer, child, translations); err != nil {
|
||||
return err
|
||||
}
|
||||
changes, err := recordChanges(ctx, tx, s.writer, child, before, translations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.commitChildFiles(ctx, tx, cr, child, "update", in); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -364,6 +378,7 @@ func (s RelationService) UpdateChild(ctx context.Context, cc *CompiledController
|
||||
return lifecycleFailure(cc, err)
|
||||
}
|
||||
}
|
||||
s.emitChildSaved(ctx, tx, child, false, changes)
|
||||
result, err = projectFullRecord(ctx, tx, cr.child, child)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -455,9 +470,34 @@ func (s RelationService) deleteChild(ctx context.Context, tx *gorm.DB, cc *Compi
|
||||
return lifecycleFailure(cc, err)
|
||||
}
|
||||
}
|
||||
if s.app != nil && s.app.Events != nil {
|
||||
EmitRecordDeleted(ctx, s.app, tx, pact.RecordDeleted{
|
||||
Controller: s.reg.controllerForRecord(child),
|
||||
Model: recordMorphType(tx, child),
|
||||
ID: recordIDText(child),
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// emitChildSaved fires the RecordSaved of a child a relation manager wrote,
|
||||
// after tx commits. Controller is the controller that edits the child's
|
||||
// model, "" when no single one does.
|
||||
func (s RelationService) emitChildSaved(ctx context.Context, tx *gorm.DB, child any, created bool, changes []pact.RecordChange) {
|
||||
if s.app == nil || s.app.Events == nil {
|
||||
return
|
||||
}
|
||||
locale, _ := towel.Locale(ctx)
|
||||
EmitRecordSaved(ctx, s.app, tx, pact.RecordSaved{
|
||||
Controller: s.reg.controllerForRecord(child),
|
||||
Model: recordMorphType(tx, child),
|
||||
ID: recordIDText(child),
|
||||
Locale: locale,
|
||||
Created: created,
|
||||
Changes: changes,
|
||||
})
|
||||
}
|
||||
|
||||
// loadPivotRow loads this parent's pivot row for the related id, locked; a
|
||||
// missing row is recordNotFound.
|
||||
func loadPivotRow(ctx context.Context, tx *gorm.DB, cr *CompiledRelation, parent *relationParent, childID uint) (any, error) {
|
||||
|
||||
Reference in New Issue
Block a user