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

Commit

Permalink
Handle multiple types for resources fixes #389 (#919)
Browse files Browse the repository at this point in the history
* wip r.Interface

* renamed render.Interface to render.Auto

* cleaned up tests for auto

* Handle multiple types for resources fixes #389

* removed a few unneeded lines in the generated resource code

* fixed broken test
  • Loading branch information
markbates authored Feb 14, 2018
1 parent 0f77060 commit 8ea9a3c
Show file tree
Hide file tree
Showing 13 changed files with 504 additions and 280 deletions.
15 changes: 1 addition & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN go get -v -u github.com/gobuffalo/makr
RUN go get -v -u github.com/markbates/grift
RUN go get -v -u github.com/markbates/inflect
RUN go get -v -u github.com/markbates/refresh
RUN go get -v -u github.com/markbates/willie
RUN go get -v -u github.com/gobuffalo/tags

ENV BP=$GOPATH/src/github.com/gobuffalo/buffalo
Expand Down Expand Up @@ -61,20 +62,6 @@ RUN rm models/user.go
RUN rm actions/users_test.go
RUN rm -rv templates/users

RUN buffalo g resource --type=json users name:text email:text
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/resource_json-xml.json

RUN rm models/user_test.go
RUN rm models/user.go
RUN rm actions/users_test.go

RUN buffalo g resource --type=xml users name:text email:text
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/resource_json-xml.json

RUN rm models/user_test.go
RUN rm models/user.go
RUN rm actions/users_test.go

RUN buffalo g resource ouch
RUN buffalo d resource -y ouch
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/destroy_resource_all.json
Expand Down
13 changes: 6 additions & 7 deletions buffalo/cmd/filetests/generate_underscore.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[{
"path": "actions/person_events.go",
"contains": [
"c.Set(\"personEvents\", personEvents)",
"c.Set(\"personEvent\", personEvent)"
]
}
]
"path": "actions/person_events.go",
"contains": [
"c.Render(200, r.Auto(c, personEvents))",
"c.Render(200, r.Auto(c, personEvent))"
]
}]
20 changes: 0 additions & 20 deletions buffalo/cmd/filetests/resource_json-xml.json

This file was deleted.

8 changes: 3 additions & 5 deletions buffalo/cmd/generate/resource.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package generate

import (
"strings"

"github.com/markbates/inflect"
"github.com/pkg/errors"

Expand All @@ -14,9 +12,9 @@ import (
var resourceOptions = struct {
SkipMigration bool
SkipModel bool
SkipTemplates bool
ModelName string
Name string
MimeType string
}{}

// ResourceCmd generates a new actions/resource file and a stub test.
Expand All @@ -30,9 +28,9 @@ var ResourceCmd = &cobra.Command{
if err != nil {
return errors.WithStack(err)
}
o.MimeType = strings.ToUpper(resourceOptions.MimeType)
o.SkipModel = resourceOptions.SkipModel
o.SkipMigration = resourceOptions.SkipMigration
o.SkipTemplates = resourceOptions.SkipTemplates
if resourceOptions.ModelName != "" {
o.UseModel = true
o.Model = inflect.Name(resourceOptions.ModelName)
Expand All @@ -51,9 +49,9 @@ var resourceMN string
func init() {
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipMigration, "skip-migration", "s", false, "tells resource generator not-to add model migration")
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipModel, "skip-model", "", false, "tells resource generator not to generate model nor migrations")
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipTemplates, "skip-templates", "", false, "tells resource generator not to generate templates for the resource")
ResourceCmd.Flags().StringVarP(&resourceOptions.ModelName, "use-model", "", "", "tells resource generator to reference an existing model in generated code")
ResourceCmd.Flags().StringVarP(&resourceOptions.Name, "name", "n", "", "allows to define a different model name for the resource being generated.")
ResourceCmd.Flags().StringVarP(&resourceOptions.MimeType, "type", "", "html", "sets the resource type (html, json, xml)")
}

const resourceExamples = `$ buffalo g resource users
Expand Down
8 changes: 8 additions & 0 deletions default_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/gobuffalo/buffalo/binding"
"github.com/gobuffalo/buffalo/render"
"github.com/gorilla/websocket"
"github.com/markbates/pop"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -114,10 +115,14 @@ func (d *DefaultContext) Render(status int, rr render.Renderer) error {
data["flash"] = d.Flash().data
data["session"] = d.Session()
data["request"] = d.Request()
data["status"] = status
bb := &bytes.Buffer{}

err := rr.Render(bb, data)
if err != nil {
if er, ok := errors.Cause(err).(render.ErrRedirect); ok {
return d.Redirect(er.Status, er.URL)
}
return HTTPError{Status: 500, Cause: errors.WithStack(err)}
}

Expand All @@ -127,6 +132,9 @@ func (d *DefaultContext) Render(status int, rr render.Renderer) error {
}

d.Response().Header().Set("Content-Type", rr.ContentType())
if p, ok := data["pagination"].(*pop.Paginator); ok {
d.Response().Header().Set("X-Pagination", p.String())
}
d.Response().WriteHeader(status)
_, err = io.Copy(d.Response(), bb)
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions generators/resource/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Generator struct {
Model inflect.Name `json:"model"`
SkipMigration bool `json:"skip_migration"`
SkipModel bool `json:"skip_model"`
SkipTemplates bool `json:"skip_templates"`
UseModel bool `json:"use_model"`
MimeType string `json:"mime_type"`
FilesPath string `json:"files_path"`
ActionsPath string `json:"actions_path"`
Props []Prop `json:"props"`
Expand All @@ -27,8 +27,7 @@ type Generator struct {
// New constructs new options for generating a resource
func New(modelName string, args ...string) (Generator, error) {
o := Generator{
MimeType: "HTML",
Args: args,
Args: args,
}
pwd, _ := os.Getwd()
o.App = meta.New(pwd)
Expand All @@ -54,11 +53,6 @@ func New(modelName string, args ...string) (Generator, error) {

// Validate that the options have what you need to build a new resource
func (o Generator) Validate() error {
mt := o.MimeType
if mt != "HTML" && mt != "JSON" && mt != "XML" {
return errors.New("invalid resource type, you need to choose between \"html\", \"xml\" and \"json\"")
}

if len(o.Args) == 0 && o.Model == "" {
return errors.New("you must specify a resource name")
}
Expand Down
11 changes: 2 additions & 9 deletions generators/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ func (res Generator) Run(root string, data makr.Data) error {

tmplName := "resource-use_model"

mimeType := res.MimeType
if mimeType == "JSON" || mimeType == "XML" {
tmplName = "resource-json-xml"
} else if res.SkipModel {
if res.SkipModel {
tmplName = "resource-name"
}

Expand All @@ -45,7 +42,7 @@ func (res Generator) Run(root string, data makr.Data) error {
p := strings.Replace(f.WritePath, tmplName, folder, -1)
g.Add(makr.NewFile(p, f.Body))
}
if mimeType == "HTML" {
if !res.SkipTemplates {
// Adding the html templates to the generator
if strings.Contains(f.WritePath, "model-view-") {
targetPath := filepath.Join(
Expand Down Expand Up @@ -80,9 +77,5 @@ func (res Generator) modelCommand() makr.Command {
args = append(args, "--skip-migration")
}

if res.MimeType == "JSON" || res.MimeType == "XML" {
args = append(args, "--struct-tag", strings.ToLower(res.MimeType))
}

return makr.NewCommand(exec.Command("buffalo", args...))
}
171 changes: 0 additions & 171 deletions generators/resource/templates/actions/resource-json-xml.go.tmpl

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8ea9a3c

Please sign in to comment.