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

fix resource name handling bug #1019

Merged
merged 1 commit into from
Apr 10, 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
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (a *App) Resource(p string, r Resource) *App {
rt := rv.Type()
rname := fmt.Sprintf("%s.%s", rt.PkgPath(), rt.Name()) + ".%s"

name := strings.Replace(rt.Name(), "Resource", "", 1)
name := strings.TrimSuffix(rt.Name(), "Resource")
paramName := inflect.Name(name).ParamID()

type paramKeyable interface {
Expand Down
12 changes: 12 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,19 @@ func Test_App_NamedRoutes(t *testing.T) {
*BaseResource
}

type ResourcesResource struct {
*BaseResource
}

r := require.New(t)
a := New(Options{})

var carsResource Resource
carsResource = CarsResource{&BaseResource{}}

var resourcesResource Resource
resourcesResource = ResourcesResource{&BaseResource{}}

rr := render.New(render.Options{
HTMLLayout: "application.html",
TemplatesBox: packr.NewBox("../templates"),
Expand All @@ -367,6 +374,8 @@ func Test_App_NamedRoutes(t *testing.T) {
9. <%= rootPath({"some":"variable","other": 12}) %>
10. <%= rootPath() %>
11. <%= rootPath({"special/":"12=ss"}) %>
12. <%= resourcePath({resource_id: 1}) %>
13. <%= editResourcePath({resource_id: 1}) %>
`))
}

Expand All @@ -375,6 +384,7 @@ func Test_App_NamedRoutes(t *testing.T) {
a.GET("/users/{user_id}", sampleHandler)
a.GET("/peeps", sampleHandler).Name("myPeeps")
a.Resource("/car", carsResource)
a.Resource("/resources", resourcesResource)

w := willie.New(a)
res := w.Request("/").Get()
Expand All @@ -390,6 +400,8 @@ func Test_App_NamedRoutes(t *testing.T) {
r.Contains(res.Body.String(), "9. /?other=12&some=variable")
r.Contains(res.Body.String(), "10. /")
r.Contains(res.Body.String(), "11. /?special%2F=12%3Dss")
r.Contains(res.Body.String(), "12. /resources/1")
r.Contains(res.Body.String(), "13. /resources/1/edit")
}

func Test_App_NamedRoutes_MissingParameter(t *testing.T) {
Expand Down