feat(quick-261016-nqk): ListExtendRecords and FormRecordTitle admin hooks (G-18.1-4)

pact.ListExtendRecords fills unstored fields of one list page before its
columns are read (WinterCMS listExtendRecords); such columns compile
unsortable and refuse sortable/searchable. pact.FormRecordTitle names a
record; the show, create and update responses send it as meta.title.
This commit is contained in:
Jakub Zych
2026-10-16 17:19:54 +02:00
parent 9890aff66d
commit bc5685eb93
18 changed files with 360 additions and 4 deletions

View File

@@ -56,6 +56,9 @@ type rosterPerson struct {
// whose contract sets WritableForeignKey, writes it.
OrganisationID *uint `gorm:"column:organisation_id"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at"`
// Badge is not stored: ListExtendRecords fills it for the list's badge
// column, once per page.
Badge string `gorm:"-:all;column:badge" json:"-"`
}
func (rosterPerson) TableName() string { return "roster_people" }
@@ -123,6 +126,8 @@ type rosterSpy struct {
record []pact.AdminRecordActionInput
// states counts ListRowStates calls and keeps the size of each page.
states []int
// extended keeps the size of each page ListExtendRecords completed.
extended []int
// virtual keeps what each Form hook read from the context.
virtual []rosterVirtual
}
@@ -156,6 +161,14 @@ func (s *rosterSpy) recordStates(n int) {
s.states = append(s.states, n)
}
func (s *rosterSpy) takeExtended() []int {
s.mu.Lock()
defer s.mu.Unlock()
out := s.extended
s.extended = nil
return out
}
func (s *rosterSpy) takeStates() []int {
s.mu.Lock()
defer s.mu.Unlock()
@@ -218,6 +231,10 @@ const (
rosterShort = "Short"
// rosterStateErr: ListRowStates fails with a plain error.
rosterStateErr = "StateErr"
// rosterExtendErr: ListExtendRecords fails with a plain error.
rosterExtendErr = "ExtendErr"
// rosterTitleErr: FormRecordTitle fails with a plain error.
rosterTitleErr = "TitleErr"
// rosterAppliesErr: the activate record action's Applies fails.
rosterAppliesErr = "AppliesErr"
// rosterCrash: the archive bulk action fails with a plain error.
@@ -413,6 +430,41 @@ func (c rosterController) ListRowStates(ctx context.Context, db *gorm.DB, record
return out, nil
}
// ListExtendRecords fills the unstored badge of one list page in a single
// pass: "<initial>#<id>", and "deleted" for a soft-deleted person.
func (c rosterController) ListExtendRecords(ctx context.Context, db *gorm.DB, records []any) error {
c.spy.mu.Lock()
c.spy.extended = append(c.spy.extended, len(records))
c.spy.mu.Unlock()
if _, inTx := cabana.TxFromContext(ctx); inTx || db == nil {
return fmt.Errorf("a list hook gets the list handle, not a transaction")
}
for _, record := range records {
person := record.(*rosterPerson)
if person.Name == rosterExtendErr {
return fmt.Errorf("the badge table said hunter2")
}
person.Badge = fmt.Sprintf("%.1s#%d", person.Name, person.ID)
if person.DeletedAt.Valid {
person.Badge += " deleted"
}
}
return nil
}
// FormRecordTitle names a person "Person #<id> (<name>)" on the form and
// preview screens; a person named rosterTitleErr fails the request.
func (rosterController) FormRecordTitle(_ context.Context, db *gorm.DB, record any) (string, error) {
person := record.(*rosterPerson)
if db == nil {
return "", fmt.Errorf("no database handle")
}
if person.Name == rosterTitleErr {
return "", fmt.Errorf("the title table said hunter2")
}
return fmt.Sprintf("Person #%d (%s)", person.ID, person.Name), nil
}
// rosterStatus is the curated view model of the preview status hint: the
// callout tone and the phrase keys of its title and text. It is empty when
// no state applies, and the template then renders nothing.