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.
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package cabana_test
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Ticket is a support ticket. OwnerEmail is not stored: the list's
|
|
// owner_email column shows what ListExtendRecords puts there.
|
|
type Ticket struct {
|
|
ID uint `gorm:"column:id;primaryKey"`
|
|
OwnerID uint `gorm:"column:owner_id"`
|
|
OwnerEmail string `gorm:"-:all;column:owner_email"`
|
|
}
|
|
|
|
// TicketsController is an admin controller with a computed column.
|
|
type TicketsController struct{}
|
|
|
|
var _ pact.ListExtendRecords = TicketsController{}
|
|
|
|
// ListExtendRecords fills the owner e-mails of one list page with one
|
|
// query. The framework calls it once per page, before the columns are read.
|
|
func (TicketsController) ListExtendRecords(ctx context.Context, db *gorm.DB, records []any) error {
|
|
ids := make([]uint, 0, len(records))
|
|
for _, record := range records {
|
|
ids = append(ids, record.(*Ticket).OwnerID)
|
|
}
|
|
var owners []struct {
|
|
ID uint
|
|
Email string
|
|
}
|
|
if err := db.WithContext(ctx).Table("acme_owners").Select("id, email").Where("id IN ?", ids).Scan(&owners).Error; err != nil {
|
|
return err
|
|
}
|
|
emails := make(map[uint]string, len(owners))
|
|
for _, owner := range owners {
|
|
emails[owner.ID] = owner.Email
|
|
}
|
|
for _, record := range records {
|
|
ticket := record.(*Ticket)
|
|
ticket.OwnerEmail = emails[ticket.OwnerID]
|
|
}
|
|
return nil
|
|
}
|