Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document the order in which hooks are executed #330

Open
nilslice opened this issue Nov 27, 2019 · 0 comments
Open

document the order in which hooks are executed #330

nilslice opened this issue Nov 27, 2019 · 0 comments

Comments

@nilslice
Copy link
Contributor

nilslice commented Nov 27, 2019

there was an issue discussed in the #ponzu slack channel where knowing the order in which content type's item.Hookable methods are executed may have helped a lot. this helps users determine the state of the database and what is available at what time.

documentation would be great to mention this order (note the hook invocations):

ponzu/system/admin/handlers.go

Lines 1619 to 1710 in ec7b5b2

// run hooks
hook, ok := post.(item.Hookable)
if !ok {
log.Println("Type", t, "does not implement item.Hookable or embed item.Item.")
res.WriteHeader(http.StatusBadRequest)
errView, err := Error400()
if err != nil {
return
}
res.Write(errView)
return
}
// check if we have a Mergeable
m, ok := post.(editor.Mergeable)
if !ok {
log.Println("Content type", t, "must implement editor.Mergeable before it can be approved.")
res.WriteHeader(http.StatusBadRequest)
errView, err := Error400()
if err != nil {
return
}
res.Write(errView)
return
}
dec := schema.NewDecoder()
dec.IgnoreUnknownKeys(true)
dec.SetAliasTag("json")
err = dec.Decode(post, req.Form)
if err != nil {
log.Println("Error decoding post form for content approval:", t, err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}
res.Write(errView)
return
}
err = hook.BeforeApprove(res, req)
if err != nil {
log.Println("Error running BeforeApprove hook in approveContentHandler for:", t, err)
return
}
// call its Approve method
err = m.Approve(res, req)
if err != nil {
log.Println("Error running Approve method in approveContentHandler for:", t, err)
return
}
err = hook.AfterApprove(res, req)
if err != nil {
log.Println("Error running AfterApprove hook in approveContentHandler for:", t, err)
return
}
err = hook.BeforeSave(res, req)
if err != nil {
log.Println("Error running BeforeSave hook in approveContentHandler for:", t, err)
return
}
// Store the content in the bucket t
id, err := db.SetContent(t+":-1", req.Form)
if err != nil {
log.Println("Error storing content in approveContentHandler for:", t, err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}
res.Write(errView)
return
}
// set the target in the context so user can get saved value from db in hook
ctx := context.WithValue(req.Context(), "target", fmt.Sprintf("%s:%d", t, id))
req = req.WithContext(ctx)
err = hook.AfterSave(res, req)
if err != nil {
log.Println("Error running AfterSave hook in approveContentHandler for:", t, err)
return
}

these are not the only hooks called. other handler funcs have the other item.Hookable methods. those should be documented as well.

the documentation should live in https://github.com/ponzu-cms/docs and copied into https://github.com/ponzu-cms/ponzu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant