feat(build): scaffold working admin controllers with model, AdminFS and delete (quick-261028-pq8)

- make:admin-controller edits the singular of its name (--model overrides):
  ModelName, modelClass and the YAML under models/<model>/
- NewRecord returns &models.<Model>{} when the model exists; otherwise the
  nil body stays and the command says to run make:model
- config_list.yaml offers create and delete with showCheckboxes
- registry.gen.go embeds controllers/*/*.yaml and models/*/*.yaml once they
  exist and exposes generatedAdminFS(); scaffolded plugins implement
  pact.AdminAssets through it, and a plugin without AdminFS gets a note
- blog example moves its YAML to models/post; docs and help text follow
This commit is contained in:
Jakub Zych
2026-10-28 19:15:23 +01:00
parent 70e4b4183c
commit 8bc18c90b6
22 changed files with 491 additions and 84 deletions

View File

@@ -90,9 +90,14 @@ func {{.Func}}() pact.Job {
{{define "admin_controller.go"}}//summer:make admin-controller
package controllers
{{if .ModelImport}}
import (
{{printf "%q" .ModelImport}}
"git.golem15.com/golem15/summercms/modules/pact"
)
{{else}}
import "git.golem15.com/golem15/summercms/modules/pact"
{{end}}
var (
_ pact.AdminController = {{.Worker}}{}
_ pact.AdminRecordSource = {{.Worker}}{}
@@ -102,13 +107,17 @@ var (
type {{.Worker}} struct{}
func ({{.Worker}}) ID() string { return {{printf "%q" .Kind}} }
func ({{.Worker}}) ModelName() string { return {{printf "%q" .Ident}} }
func ({{.Worker}}) ModelName() string { return {{printf "%q" .ModelIdent}} }
func ({{.Worker}}) ConfigDir() string { return {{printf "%q" .ConfigDir}} }
// NewRecord returns the model the generic admin handlers query and fill.
// TODO: return a pointer to the plugin's model, such as &models.{{.Ident}}{}.
{{- if .ModelImport}}
func ({{.Worker}}) NewRecord() any { return &models.{{.ModelIdent}}{} }
{{- else}}
// TODO: run summer make:model for {{.ModelIdent}}, then return &models.{{.ModelIdent}}{}.
// Until then the list and every write answer 500, so nothing is exposed.
func ({{.Worker}}) NewRecord() any { return nil }
{{- end}}
// RequiredPermissions is checked before any schema or record is served. Keep
// it: a controller without one is open to every administrator. Declare the
@@ -122,9 +131,9 @@ func ({{.Worker}}) RequiredPermissions() []string {
func {{.Func}}() pact.AdminController { return {{.Worker}}{} }
{{end}}
{{define "config_form.yaml"}}name: {{.Ident}}
form: ~/plugins/{{.PluginPath}}/models/{{.Snake}}/fields.yaml
modelClass: {{.Ident}}
{{define "config_form.yaml"}}name: {{.ModelIdent}}
form: ~/plugins/{{.PluginPath}}/models/{{.ModelSnake}}/fields.yaml
modelClass: {{.ModelIdent}}
defaultRedirect: {{.PluginPath}}/{{.Snake}}
create:
@@ -136,12 +145,13 @@ update:
redirectClose: {{.PluginPath}}/{{.Snake}}
{{end}}
{{define "config_list.yaml"}}list: ~/plugins/{{.PluginPath}}/models/{{.Snake}}/columns.yaml
modelClass: {{.Ident}}
{{define "config_list.yaml"}}list: ~/plugins/{{.PluginPath}}/models/{{.ModelSnake}}/columns.yaml
modelClass: {{.ModelIdent}}
title: {{.Ident}}
recordsPerPage: 20
showCheckboxes: true
toolbar:
buttons: [create]
buttons: [create, delete]
search:
prompt: backend::lang.list.search_prompt
{{end}}

View File

@@ -20,6 +20,7 @@ var (
_ pact.HasCommands = (*Plugin)(nil)
_ pact.HasJobs = (*Plugin)(nil)
_ pact.HasAdminControllers = (*Plugin)(nil)
_ pact.AdminAssets = (*Plugin)(nil)
)
//go:embed config
@@ -65,6 +66,9 @@ func (p *Plugin) AdminControllers() []pact.AdminController {
return generatedAdminControllers()
}
// AdminFS serves the admin YAML that registry.gen.go embeds.
func (p *Plugin) AdminFS() fs.FS { return generatedAdminFS() }
func init() {
party.Register(&Plugin{})
}

View File

@@ -3,6 +3,9 @@
package {{.Package}}
import (
"embed"
"io/fs"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/pact"
@@ -79,4 +82,19 @@ func generatedAdminControllers() []pact.AdminController {
return nil
{{- end}}
}
// generatedAdminTree holds the admin YAML of the plugin: the controller
// configuration and the model fields and columns. Go evaluates the go:embed
// globs at compile time, so YAML files added later in these directories are
// included; the next make: command rewrites the line when a glob starts or
// stops matching a file.
{{- if .AdminPatterns}}
//
//go:embed{{range .AdminPatterns}} {{.}}{{end}}
{{- end}}
var generatedAdminTree embed.FS
// generatedAdminFS returns the admin YAML for pact.AdminAssets: an empty tree
// until the first admin controller is scaffolded.
func generatedAdminFS() fs.FS { return generatedAdminTree }
{{end}}