Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

fixes DELETE not working properly for nested resources with render.Auto fixes #1299 #1342

Merged
merged 2 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions render/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (ir htmlAutoRenderer) Render(w io.Writer, data Data) error {
}

switch data["method"] {
case "PUT", "POST":
case "PUT", "POST", "DELETE":
if err := ir.redirect(pname, w, data); err != nil {
if er, ok := err.(ErrRedirect); ok && er.Status >= 300 && er.Status < 400 {
return err
Expand All @@ -114,30 +114,31 @@ func (ir htmlAutoRenderer) Render(w io.Writer, data Data) error {
return ir.HTML(fmt.Sprintf("%s/new.html", pname.File())).Render(w, data)
}
return nil
case "DELETE":
return ErrRedirect{
Status: 302,
URL: "/" + pname.URL(),
}
}
if cp, ok := data["current_path"].(string); ok {
if strings.HasSuffix(cp, "/edit/") {
return ir.HTML(fmt.Sprintf("%s/edit.html", pname.File())).Render(w, data)
}
if strings.HasSuffix(cp, "/new/") {
return ir.HTML(fmt.Sprintf("%s/new.html", pname.File())).Render(w, data)
}
cp, ok := data["current_path"].(string)

x, err := regexp.Compile(fmt.Sprintf("%s/.+", pname.URL()))
if err != nil {
return errors.WithStack(err)
}
if x.MatchString(cp) {
return ir.HTML(fmt.Sprintf("%s/show.html", pname.File())).Render(w, data)
}
defCase := func() error {
return ir.HTML(fmt.Sprintf("%s/%s.html", pname.File(), "index")).Render(w, data)
}
if !ok {
return defCase()
}

return ir.HTML(fmt.Sprintf("%s/%s.html", pname.File(), "index")).Render(w, data)
if strings.HasSuffix(cp, "/edit/") {
return ir.HTML(fmt.Sprintf("%s/edit.html", pname.File())).Render(w, data)
}
if strings.HasSuffix(cp, "/new/") {
return ir.HTML(fmt.Sprintf("%s/new.html", pname.File())).Render(w, data)
}

x, err := regexp.Compile(fmt.Sprintf("%s/.+", pname.URL()))
if err != nil {
return errors.WithStack(err)
}
if x.MatchString(cp) {
return ir.HTML(fmt.Sprintf("%s/show.html", pname.File())).Render(w, data)
}
return defCase()
}

func (ir htmlAutoRenderer) redirect(name inflect.Name, w io.Writer, data Data) error {
Expand All @@ -151,11 +152,20 @@ func (ir htmlAutoRenderer) redirect(name inflect.Name, w io.Writer, data Data) e
rt := reflect.TypeOf(fi)
zero := reflect.Zero(rt)
if fi != zero.Interface() {
m, ok := data["method"].(string)
if !ok {
m = "GET"
}
url := fmt.Sprint(data["current_path"])
id := fmt.Sprint(f.Interface())
url = strings.TrimSuffix(url, "/")
if !strings.HasSuffix(url, id) {
url = path.Join(url, id)
switch m {
case "DELETE":
url = strings.TrimSuffix(url, id)
default:
if !strings.HasSuffix(url, id) {
url = path.Join(url, id)
}
}

code := 302
Expand Down
20 changes: 19 additions & 1 deletion render/auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ func Test_Auto_HTML_Create_Nested_Redirect(t *testing.T) {
r.Equal(302, res.Code)
}

func Test_Auto_HTML_Destroy_Nested_Redirect(t *testing.T) {
r := require.New(t)

app := buffalo.New(buffalo.Options{})
admin := app.Group("/admin")
admin.DELETE("/cars", func(c buffalo.Context) error {
return c.Render(200, render.Auto(c, Car{
ID: 1,
Name: "Honda",
}))
})

w := httptest.New(app)
res := w.HTML("/admin/cars").Delete()
r.Equal("/admin/cars", res.Location())
r.Equal(302, res.Code)
}

func Test_Auto_HTML_Edit(t *testing.T) {
r := require.New(t)

Expand Down Expand Up @@ -327,7 +345,7 @@ func Test_Auto_HTML_Destroy_Redirect(t *testing.T) {

w := httptest.New(app)
res := w.HTML("/cars/1").Delete()
r.Equal("/cars", res.Location())
r.Equal("/cars/", res.Location())
r.Equal(302, res.Code)
}

Expand Down