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

Commit

Permalink
Merge pull request #757 from gobuffalo/regressed-nested-resources
Browse files Browse the repository at this point in the history
Regession in Resource generator for nested resources fixes #754
  • Loading branch information
markbates authored Nov 14, 2017
2 parents 6daf776 + 9ccda72 commit a4cf642
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 26 deletions.
9 changes: 8 additions & 1 deletion buffalo/cmd/filetests/generate_resource_nested.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"path": "actions/admin_planes.go",
"contains": [
"type AdminPlanesResource struct",
"func (v AdminPlanesResource)"
"func (v AdminPlanesResource)",
"tx.Find(plane, c.Param(\"admin_plane_id\"))"
]
},
{
Expand All @@ -29,5 +30,11 @@
"contains": [
"newAdminPlanesPath()"
]
},
{
"path": "templates/admin/planes/show.html",
"contains": [
"editAdminPlanePath({ admin_plane_id: plane.ID })"
]
}
]
16 changes: 16 additions & 0 deletions generators/resource/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package resource

import (
"testing"

"github.com/stretchr/testify/require"
)

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

g, err := New("", "admin/user")
r.NoError(err)
name := g.Name
r.Equal("admin_user_id", name.ParamID())
}
16 changes: 8 additions & 8 deletions generators/resource/templates/actions/resource-json-xml.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ func (v {{.opts.Name.Resource}}Resource) List(c buffalo.Context) error {
}

// Show gets the data for one {{.opts.Model.Model}}. This function is mapped to
// the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Model.UnderSingular}}_id}
// the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
func (v {{.opts.Name.Resource}}Resource) Show(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)

// Allocate an empty {{.opts.Model.Model}}
{{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{}

// To find the {{.opts.Model.Model}} the parameter {{.opts.Model.UnderSingular}}_id is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Model.UnderSingular}}_id")); err != nil {
// To find the {{.opts.Model.Model}} the parameter {{.opts.Name.ParamID}} is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
return c.Error(404, err)
}

Expand Down Expand Up @@ -104,15 +104,15 @@ func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error {
}

// Update changes a {{.opts.Model.Model}} in the DB. This function is mapped to
// the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Model.UnderSingular}}_id}
// the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
func (v {{.opts.Name.Resource}}Resource) Update(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)

// Allocate an empty {{.opts.Model.Model}}
{{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{}

if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Model.UnderSingular}}_id")); err != nil {
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
return c.Error(404, err)
}

Expand All @@ -135,16 +135,16 @@ func (v {{.opts.Name.Resource}}Resource) Update(c buffalo.Context) error {
}

// Destroy deletes a {{.opts.Model.Model}} from the DB. This function is mapped
// to the path DELETE /{{.opts.Name.URL}}/{{"{"}}{{.opts.Model.UnderSingular}}_id}
// to the path DELETE /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
func (v {{.opts.Name.Resource}}Resource) Destroy(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)

// Allocate an empty {{.opts.Model.Model}}
{{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{}

// To find the {{.opts.Model.Model}} the parameter {{.opts.Model.UnderSingular}}_id is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Model.UnderSingular}}_id")); err != nil {
// To find the {{.opts.Model.Model}} the parameter {{.opts.Name.ParamID}} is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
return c.Error(404, err)
}

Expand Down
20 changes: 10 additions & 10 deletions generators/resource/templates/actions/resource-use_model.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ func (v {{.opts.Name.Resource}}Resource) List(c buffalo.Context) error {
}

// Show gets the data for one {{.opts.Model.Model}}. This function is mapped to
// the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Model.UnderSingular}}_id}
// the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
func (v {{.opts.Name.Resource}}Resource) Show(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)

// Allocate an empty {{.opts.Model.Model}}
{{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{}

// To find the {{.opts.Model.Model}} the parameter {{.opts.Model.UnderSingular}}_id is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Model.UnderSingular}}_id")); err != nil {
// To find the {{.opts.Model.Model}} the parameter {{.opts.Name.ParamID}} is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
return c.Error(404, err)
}

Expand Down Expand Up @@ -120,15 +120,15 @@ func (v {{.opts.Name.Resource}}Resource) Create(c buffalo.Context) error {
}

// Edit renders a edit form for a {{.opts.Model.Model}}. This function is
// mapped to the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Model.UnderSingular}}_id}/edit
// mapped to the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}/edit
func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)

// Allocate an empty {{.opts.Model.Model}}
{{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{}

if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Model.UnderSingular}}_id")); err != nil {
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
return c.Error(404, err)
}

Expand All @@ -138,15 +138,15 @@ func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error {
}

// Update changes a {{.opts.Model.Model}} in the DB. This function is mapped to
// the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Model.UnderSingular}}_id}
// the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
func (v {{.opts.Name.Resource}}Resource) Update(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)

// Allocate an empty {{.opts.Model.Model}}
{{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{}

if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Model.UnderSingular}}_id")); err != nil {
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
return c.Error(404, err)
}

Expand Down Expand Up @@ -180,16 +180,16 @@ func (v {{.opts.Name.Resource}}Resource) Update(c buffalo.Context) error {
}

// Destroy deletes a {{.opts.Model.Model}} from the DB. This function is mapped
// to the path DELETE /{{.opts.Name.URL}}/{{"{"}}{{.opts.Model.UnderSingular}}_id}
// to the path DELETE /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
func (v {{.opts.Name.Resource}}Resource) Destroy(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)

// Allocate an empty {{.opts.Model.Model}}
{{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{}

// To find the {{.opts.Model.Model}} the parameter {{.opts.Model.UnderSingular}}_id is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Model.UnderSingular}}_id")); err != nil {
// To find the {{.opts.Model.Model}} the parameter {{.opts.Name.ParamID}} is used.
if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
return c.Error(404, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>Edit {{.opts.Model.Model}}</h1>
</div>

<%= form_for({{.opts.Model.VarCaseSingular}}, {action: {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Model.UnderSingular}}_id: {{.opts.Model.VarCaseSingular}}.ID }), method: "PUT"}) { %>
<%= form_for({{.opts.Model.VarCaseSingular}}, {action: {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Name.ParamID}}: {{.opts.Model.VarCaseSingular}}.ID }), method: "PUT"}) { %>
<%= partial("{{.opts.FilesPath}}/form.html") %>
<a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Model.UnderSingular}}_id: {{.opts.Model.VarCaseSingular}}.ID }) %>" class="btn btn-warning" data-confirm="Are you sure?">Cancel</a>
<a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Name.ParamID}}: {{.opts.Model.VarCaseSingular}}.ID }) %>" class="btn btn-warning" data-confirm="Are you sure?">Cancel</a>
<% } %>
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
{{ end -}}
<td>
<div class="pull-right">
<a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Model.UnderSingular}}_id: {{.opts.Model.VarCaseSingular}}.ID }) %>" class="btn btn-info">View</a>
<a href="<%= edit{{.opts.Name.Model}}Path({ {{.opts.Model.UnderSingular}}_id: {{.opts.Model.VarCaseSingular}}.ID }) %>" class="btn btn-warning">Edit</a>
<a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Model.UnderSingular}}_id: {{.opts.Model.VarCaseSingular}}.ID }) %>" data-method="DELETE" data-confirm="Are you sure?" class="btn btn-danger">Destroy</a>
<a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Name.ParamID}}: {{.opts.Model.VarCaseSingular}}.ID }) %>" class="btn btn-info">View</a>
<a href="<%= edit{{.opts.Name.Model}}Path({ {{.opts.Name.ParamID}}: {{.opts.Model.VarCaseSingular}}.ID }) %>" class="btn btn-warning">Edit</a>
<a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Name.ParamID}}: {{.opts.Model.VarCaseSingular}}.ID }) %>" data-method="DELETE" data-confirm="Are you sure?" class="btn btn-danger">Destroy</a>
</div>
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

<ul class="list-unstyled list-inline">
<li><a href="<%= {{.opts.Name.VarCasePlural}}Path() %>" class="btn btn-info">Back to all {{.opts.Model.ModelPlural}}</a></li>
<li><a href="<%= edit{{.opts.Name.Model}}Path({ {{.opts.Model.UnderSingular}}_id: {{.opts.Model.VarCaseSingular}}.ID })%>" class="btn btn-warning">Edit</a></li>
<li><a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Model.UnderSingular}}_id: {{.opts.Model.VarCaseSingular}}.ID })%>" data-method="DELETE" data-confirm="Are you sure?" class="btn btn-danger">Destroy</a>
<li><a href="<%= edit{{.opts.Name.Model}}Path({ {{.opts.Name.ParamID}}: {{.opts.Model.VarCaseSingular}}.ID })%>" class="btn btn-warning">Edit</a></li>
<li><a href="<%= {{.opts.Name.VarCaseSingular}}Path({ {{.opts.Name.ParamID}}: {{.opts.Model.VarCaseSingular}}.ID })%>" data-method="DELETE" data-confirm="Are you sure?" class="btn btn-danger">Destroy</a>
</ul>

{{ range $p := .opts.Props -}}
Expand Down
6 changes: 6 additions & 0 deletions meta/name.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meta

import (
"fmt"
"strings"

"github.com/markbates/inflect"
Expand Down Expand Up @@ -122,3 +123,8 @@ func (n Name) VarCasePlural() string {
func (n Name) Lower() string {
return strings.ToLower(string(n))
}

// ParamID returns foo_bar_id
func (n Name) ParamID() string {
return fmt.Sprintf("%s_id", strings.Replace(n.UnderSingular(), "/", "_", -1))
}
16 changes: 16 additions & 0 deletions meta/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Name_ParamID(t *testing.T) {
r := require.New(t)
table := []struct {
V string
E string
}{
{V: "foo_bar", E: "foo_bar_id"},
{V: "admin/widget", E: "admin_widget_id"},
{V: "widget", E: "widget_id"},
{V: "User", E: "user_id"},
}
for _, tt := range table {
r.Equal(tt.E, Name(tt.V).ParamID())
}
}

func Test_Name_Title(t *testing.T) {
r := require.New(t)
table := []struct {
Expand Down

0 comments on commit a4cf642

Please sign in to comment.