Skip to content
Permalink
Browse files

Do not generate "/edit" route during API generation fixes #1562 (#1590)

  • Loading branch information...
markbates committed Feb 19, 2019
1 parent caad851 commit 2e8376a9aef30973dc2ca35c458d7f8b260d92e4
@@ -14,22 +14,22 @@ func (as *ActionSuite) Test_WidgetsResource_Show() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_WidgetsResource_New() {
func (as *ActionSuite) Test_WidgetsResource_Create() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_WidgetsResource_Create() {
func (as *ActionSuite) Test_WidgetsResource_Update() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_WidgetsResource_Edit() {
func (as *ActionSuite) Test_WidgetsResource_Destroy() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_WidgetsResource_Update() {
func (as *ActionSuite) Test_WidgetsResource_New() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_WidgetsResource_Destroy() {
func (as *ActionSuite) Test_WidgetsResource_Edit() {
as.Fail("Not Implemented!")
}
@@ -14,22 +14,22 @@ func (as *ActionSuite) Test_AdminWidgetsResource_Show() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_AdminWidgetsResource_New() {
func (as *ActionSuite) Test_AdminWidgetsResource_Create() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_AdminWidgetsResource_Create() {
func (as *ActionSuite) Test_AdminWidgetsResource_Update() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_AdminWidgetsResource_Edit() {
func (as *ActionSuite) Test_AdminWidgetsResource_Destroy() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_AdminWidgetsResource_Update() {
func (as *ActionSuite) Test_AdminWidgetsResource_New() {
as.Fail("Not Implemented!")
}

func (as *ActionSuite) Test_AdminWidgetsResource_Destroy() {
func (as *ActionSuite) Test_AdminWidgetsResource_Edit() {
as.Fail("Not Implemented!")
}
@@ -9,16 +9,6 @@ import (
"github.com/pkg/errors"
)

var actions = []name.Ident{
name.New("list"),
name.New("show"),
name.New("new"),
name.New("create"),
name.New("edit"),
name.New("update"),
name.New("destroy"),
}

func addResource(pres presenter) genny.RunFn {
return func(r *genny.Runner) error {
f, err := r.FindFile("actions/app.go")
@@ -33,3 +23,19 @@ func addResource(pres presenter) genny.RunFn {
return r.File(f)
}
}

func actions(opts *Options) []name.Ident {
actions := []name.Ident{
name.New("list"),
name.New("show"),
name.New("create"),
name.New("update"),
name.New("destroy"),
}

if opts.App.AsWeb {
actions = append(actions, name.New("new"), name.New("edit"))
}

return actions
}
@@ -53,7 +53,7 @@ func New(opts *Options) (*genny.Generator, error) {

data := map[string]interface{}{
"opts": pres,
"actions": actions,
"actions": actions(opts),
"folder": folder,
}
helpers := template.FuncMap{
@@ -66,7 +66,8 @@ func Test_New(t *testing.T) {
r.Len(res.Files, 9)

nn := name.New(tt.Options.Name).Pluralize().String()
for _, s := range []string{"_form", "edit", "index", "new", "show"} {
actions := []string{"_form", "index", "show", "new", "edit"}
for _, s := range actions {
p := path.Join("templates", nn, s+".html")
_, err = res.Find(p)
r.NoError(err)
@@ -79,6 +79,7 @@ func (v {{.opts.Name.Resource}}Resource) Show(c buffalo.Context) error {
return c.Render(200, r.Auto(c, {{.opts.Model.VarCaseSingle}}))
}

{{ if .opts.App.AsWeb -}}
// New renders the form for creating a new {{.opts.Model.Proper}}.
// This function is mapped to the path GET /{{.opts.Name.URL}}/new
func (v {{.opts.Name.Resource}}Resource) New(c buffalo.Context) error {
@@ -88,6 +89,7 @@ func (v {{.opts.Name.Resource}}Resource) New(c buffalo.Context) error {
{{ end -}}
return c.Render(200, r.Auto(c, &models.{{.opts.Model.Proper}}{}))
}
{{ end -}}

// Create adds a {{.opts.Model.Proper}} to the DB. This function is mapped to the
// path POST /{{.opts.Name.URL}}
@@ -130,6 +132,7 @@ func (v {{.opts.Name.Resource}}Resource) Create(c buffalo.Context) error {
return c.Render(201, r.Auto(c, {{.opts.Model.VarCaseSingle}}))
}

{{ if .opts.App.AsWeb -}}
// Edit renders a edit form for a {{.opts.Model.Proper}}. This function is
// mapped to the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}/edit
func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error {
@@ -152,6 +155,7 @@ func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error {
{{ end -}}
return c.Render(200, r.Auto(c, {{.opts.Model.VarCaseSingle}}))
}
{{ end -}}

// Update changes a {{.opts.Model.Proper}} in the DB. This function is mapped to
// the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -25,9 +25,7 @@ import "errors"
type Resource interface {
List(Context) error
Show(Context) error
New(Context) error
Create(Context) error
Edit(Context) error
Update(Context) error
Destroy(Context) error
}
@@ -59,21 +57,11 @@ func (v BaseResource) Show(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}

// New default implementation. Returns a 404
func (v BaseResource) New(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}

// Create default implementation. Returns a 404
func (v BaseResource) Create(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}

// Edit default implementation. Returns a 404
func (v BaseResource) Edit(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}

// Update default implementation. Returns a 404
func (v BaseResource) Update(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
@@ -116,6 +116,14 @@ func (a *App) fileServer(fs http.FileSystem) http.Handler {
})
}

type newable interface {
New(Context) error
}

type editable interface {
Edit(Context) error
}

// Resource maps an implementation of the Resource interface
// to the appropriate RESTful mappings. Resource returns the *App
// associated with this group of mappings so you can set middleware, etc...
@@ -136,6 +144,7 @@ func (a *App) fileServer(fs http.FileSystem) http.Handler {
g.DELETE("/{user_id}", ur.Destroy) DELETE /users/{user_id} => ur.Destroy
*/
func (a *App) Resource(p string, r Resource) *App {

g := a.Group(p)
p = "/"

@@ -161,12 +170,20 @@ func (a *App) Resource(p string, r Resource) *App {
spath := path.Join(p, "{"+paramName+"}")
setFuncKey(r.List, fmt.Sprintf(rname, "List"))
g.GET(p, r.List)
setFuncKey(r.New, fmt.Sprintf(rname, "New"))
g.GET(path.Join(p, "new"), r.New)

if n, ok := r.(newable); ok {
setFuncKey(n.New, fmt.Sprintf(rname, "New"))
g.GET(path.Join(p, "new"), n.New)
}

setFuncKey(r.Show, fmt.Sprintf(rname, "Show"))
g.GET(path.Join(spath), r.Show)
setFuncKey(r.Edit, fmt.Sprintf(rname, "Edit"))
g.GET(path.Join(spath, "edit"), r.Edit)

if n, ok := r.(editable); ok {
setFuncKey(n.Edit, fmt.Sprintf(rname, "Edit"))
g.GET(path.Join(spath, "edit"), n.Edit)
}

setFuncKey(r.Create, fmt.Sprintf(rname, "Create"))
g.POST(p, r.Create)
setFuncKey(r.Update, fmt.Sprintf(rname, "Update"))
@@ -14,6 +14,7 @@ import (
"github.com/gobuffalo/packd"
"github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

@@ -358,14 +359,28 @@ func Test_Router_ServeFiles(t *testing.T) {
r.Equal(res.Header().Get("Cache-Control"), "max-age=3600")
}

type WebResource struct {
BaseResource
}

// Edit default implementation. Returns a 404
func (v WebResource) Edit(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}

// New default implementation. Returns a 404
func (v WebResource) New(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}

func Test_App_NamedRoutes(t *testing.T) {

type CarsResource struct {
*BaseResource
WebResource
}

type ResourcesResource struct {
*BaseResource
WebResource
}

r := require.New(t)
@@ -520,7 +535,7 @@ func Test_Resource(t *testing.T) {
}

type paramKeyResource struct {
Resource
*userResource
}

func (paramKeyResource) ParamKey() string {
@@ -529,7 +544,7 @@ func (paramKeyResource) ParamKey() string {

func Test_Resource_ParamKey(t *testing.T) {
r := require.New(t)
fr := &paramKeyResource{&BaseResource{}}
fr := &paramKeyResource{&userResource{}}
a := New(Options{})
a.Resource("/foo", fr)
rt := a.Routes()
@@ -639,17 +654,17 @@ func Test_ResourceOnResource(t *testing.T) {
func Test_buildRouteName(t *testing.T) {
r := require.New(t)
cases := map[string]string{
"/": "root",
"/users": "users",
"/users/new": "newUsers",
"/users/{user_id}": "user",
"/users/{user_id}/children": "userChildren",
"/users/{user_id}/children/{child_id}": "userChild",
"/users/{user_id}/children/new": "newUserChildren",
"/": "root",
"/users": "users",
"/users/new": "newUsers",
"/users/{user_id}": "user",
"/users/{user_id}/children": "userChildren",
"/users/{user_id}/children/{child_id}": "userChild",
"/users/{user_id}/children/new": "newUserChildren",
"/users/{user_id}/children/{child_id}/build": "userChildBuild",
"/admin/planes": "adminPlanes",
"/admin/planes/{plane_id}": "adminPlane",
"/admin/planes/{plane_id}/edit": "editAdminPlane",
"/admin/planes": "adminPlanes",
"/admin/planes/{plane_id}": "adminPlane",
"/admin/planes/{plane_id}/edit": "editAdminPlane",
}

a := New(Options{})

0 comments on commit 2e8376a

Please sign in to comment.
You can’t perform that action at this time.