Files
summercms/modules/pact/record_events.go
Jakub Zych 1583651783 fix(32): WR-02 report the request locale's primary tag on record events
Admin saves copied the raw context locale into RecordSaved.Locale, which
could be a whole Accept-Language header, while reorder reported a
normalised code. Saves, child writes and reorder now report the primary tag
of the request locale. The pact doc says what Locale is and that per-locale
work keys on RecordChange.Locale, since an admin save writes the row in the
default content locale.
2026-10-27 15:32:11 +01:00

60 lines
2.6 KiB
Go

package pact
// RecordChange is one changed attribute of a saved record. Attribute is the
// column or form field name. Locale is "" for a value stored on the record's
// own row, and the locale code for a translated value stored elsewhere (a
// translatable field's non-default locales); a translated slug therefore
// appears once per changed locale. Old is nil on create and for a
// translation that did not exist before; New is the value after the save.
// Values are as the model holds them (string, number, bool, *time.Time and
// so on), not formatted.
type RecordChange struct {
Attribute string
Locale string
Old any
New any
}
// RecordSaved is fired through the application's event bus (app.Events)
// after a record is created or updated, once the write has committed: a
// rolled-back save fires nothing. It is fired and listened to as a pointer:
//
// app.Events.Listen("acme.audit", func(ctx context.Context, ev *pact.RecordSaved) error { ... })
//
// The admin framework fires it for every admin write; plugin code that
// writes records directly fires it through cabana.EmitRecordSaved. A
// listener's error is logged and never fails the write that caused it.
//
// Controller is the ID of the admin controller that saved the record, ""
// for a write outside the admin forms. Model is the record's morph type
// (lagoon.MorphType: the model's MorphName, else its table name), the same
// value a content type declares in ContentDescriptor.Model. ID is the
// primary key as text. Locale is the locale of the request that wrote the
// record, "" when it has none; the admin reports the primary tag of the
// administrator's request locale (the first tag of Accept-Language, or the
// preferred locale). It does not say where values were written: an admin
// save writes the record's row in the default content locale and reports
// each translated value with its own RecordChange.Locale, so key per-locale
// work on the changes. Created is true on create. Changes lists the attributes the save
// changed with their old and new values; on create it lists every written
// column with a nil Old. A save that changed nothing still fires, with an
// empty Changes, so subscribers may re-check what they derive from the
// record.
type RecordSaved struct {
Controller string
Model string
ID string
Locale string
Created bool
Changes []RecordChange
}
// RecordDeleted is fired like RecordSaved after a record is deleted and the
// delete has committed, once per record (a bulk delete fires one event per
// row). Controller, Model and ID follow RecordSaved.
type RecordDeleted struct {
Controller string
Model string
ID string
}