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:
@@ -218,6 +218,7 @@ func compileColumns(pluginID string, ctl pact.AdminController, fsys fs.FS, colPa
|
||||
return nil, bootErr(pluginID, ctl.ID(), colPath, err)
|
||||
}
|
||||
modelCols, modelRels, checkModel := listModelContract(ctl)
|
||||
unstored := unstoredModelColumns(ctl)
|
||||
compiled := make([]ListColumn, 0, len(cols.Columns))
|
||||
seen := map[string]struct{}{}
|
||||
for _, item := range cols.Columns {
|
||||
@@ -280,6 +281,14 @@ func compileColumns(pluginID string, ctl pact.AdminController, fsys fs.FS, colPa
|
||||
if spec.Sortable != nil {
|
||||
sortable = *spec.Sortable
|
||||
}
|
||||
// A column of a field gorm ignores (filled by pact.ListExtendRecords)
|
||||
// has no database column: it is never sorted or searched.
|
||||
if _, ok := unstored[key]; ok && spec.Relation == "" {
|
||||
if sortable && spec.Sortable != nil || spec.Searchable {
|
||||
return nil, bootErr(pluginID, ctl.ID(), colPath, fmt.Errorf("column %s is not stored: it cannot be sortable or searchable", key))
|
||||
}
|
||||
sortable = false
|
||||
}
|
||||
compiled = append(compiled, ListColumn{
|
||||
Key: key,
|
||||
Label: spec.Label,
|
||||
@@ -538,6 +547,43 @@ func listModelContract(ctl pact.AdminController) (map[string]string, map[string]
|
||||
return cols, rels, true
|
||||
}
|
||||
|
||||
// unstoredModelColumns names the model fields gorm ignores (a "-" tag), by
|
||||
// their column tag or field name: values a controller fills through
|
||||
// pact.ListExtendRecords.
|
||||
func unstoredModelColumns(ctl pact.AdminController) map[string]struct{} {
|
||||
src, ok := ctl.(pact.AdminRecordSource)
|
||||
if !ok || src == nil {
|
||||
return nil
|
||||
}
|
||||
model := src.NewRecord()
|
||||
if model == nil {
|
||||
return nil
|
||||
}
|
||||
out := map[string]struct{}{}
|
||||
for _, mf := range modelFields(reflect.TypeOf(model)) {
|
||||
if !gormIgnored(mf.Field) {
|
||||
continue
|
||||
}
|
||||
name := gormColumn(mf.Field)
|
||||
if name == "" {
|
||||
name = mf.Field.Name
|
||||
}
|
||||
out[name] = struct{}{}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// gormIgnored reports a gorm "-" (or "-:all") tag.
|
||||
func gormIgnored(field reflect.StructField) bool {
|
||||
for _, part := range strings.Split(field.Tag.Get("gorm"), ";") {
|
||||
switch strings.TrimSpace(part) {
|
||||
case "-", "-:all":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// listColumnKind is the Winter list type of a model field when columns.yaml
|
||||
// omits type: datetime for time.Time, date for lagoon.Date, time for
|
||||
// lagoon.TimeOfDay. Everything else stays text (the admin's default).
|
||||
|
||||
Reference in New Issue
Block a user