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

Commit

Permalink
Use model#isPlural to pick which template to auto-render (#1728)
Browse files Browse the repository at this point in the history
* Use plural vs. singular to pick which template to auto render,

This allows using dashes instead of underscores in resource urls.

* add test cases
  • Loading branch information
bytheway authored and markbates committed Jul 10, 2019
1 parent e3c45b6 commit b1f586d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
10 changes: 3 additions & 7 deletions render/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"path"
"reflect"
"regexp"
"strings"

"github.com/gobuffalo/flect/name"
Expand Down Expand Up @@ -93,8 +92,9 @@ func (htmlAutoRenderer) ContentType() string {
func (ir htmlAutoRenderer) Render(w io.Writer, data Data) error {
n := name.New(ir.typeName())
pname := name.New(n.Pluralize().String())
isPlural := ir.isPlural()

if ir.isPlural() {
if isPlural {
data[pname.VarCasePlural().String()] = ir.model
} else {
data[n.VarCaseSingle().String()] = ir.model
Expand Down Expand Up @@ -134,11 +134,7 @@ func (ir htmlAutoRenderer) Render(w io.Writer, data Data) error {
return ir.HTML(fmt.Sprintf("%s/new.html", templatePrefix)).Render(w, data)
}

x, err := regexp.Compile(fmt.Sprintf("%s/.+", pname.URL()))
if err != nil {
return err
}
if x.MatchString(cp) {
if !isPlural {
return ir.HTML(fmt.Sprintf("%s/show.html", templatePrefix)).Render(w, data)
}
return defCase()
Expand Down
48 changes: 48 additions & 0 deletions render/auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,32 @@ func Test_Auto_HTML_List_Plural_MultiWord(t *testing.T) {
r.NoError(err)
}

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

type RoomProvider struct {
Name string
}

type RoomProviders []RoomProvider

err := withHTMLFile("room_providers/index.html", "INDEX: <%= len(roomProviders) %>", func(e *render.Engine) {
app := buffalo.New(buffalo.Options{})
app.GET("/room-providers", func(c buffalo.Context) error {
return c.Render(200, e.Auto(c, RoomProviders{
RoomProvider{Name: "Ford"},
RoomProvider{Name: "Chevy"},
}))
})

w := httptest.New(app)
res := w.HTML("/room-providers").Get()

r.Contains(res.Body.String(), "INDEX: 2")
})
r.NoError(err)
}

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

Expand All @@ -154,6 +180,28 @@ func Test_Auto_HTML_Show(t *testing.T) {
r.NoError(err)
}

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

type RoomProvider struct {
ID int
Name string
}

err := withHTMLFile("room_providers/show.html", "SHOW: <%= roomProvider.Name %>", func(e *render.Engine) {
app := buffalo.New(buffalo.Options{})
app.GET("/room-providers/{id}", func(c buffalo.Context) error {
return c.Render(200, e.Auto(c, RoomProvider{ID: 1, Name: "Ford"}))
})

w := httptest.New(app)
res := w.HTML("/room-providers/1").Get()

r.Contains(res.Body.String(), "SHOW: Ford")
})
r.NoError(err)
}

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

Expand Down

0 comments on commit b1f586d

Please sign in to comment.