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

[feature] implementing model generation for resources #270

Merged
merged 9 commits into from
Mar 9, 2017
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@ RUN go vet -x ./...
RUN buffalo db create -a
RUN buffalo db migrate -e test
RUN buffalo test -race

RUN buffalo g goth facebook twitter linkedin github
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/goth.json

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

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

RUN buffalo g resource admins --skip-model
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/resource_skip_model.json
RUN rm actions/admins_test.go

RUN buffalo test -race
RUN buffalo build

Expand Down
6 changes: 6 additions & 0 deletions buffalo/cmd/filetests/resource_model_migration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[{
"path": "models/user.go",
"contains": [
"type User struct {"
]
}]
13 changes: 13 additions & 0 deletions buffalo/cmd/filetests/resource_skip_model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[{
"path": "models/admin.go",
"absent": true
},{
"path": "models/admin_test.go",
"absent": true
},{
"path": "migrations/*_create_admins.up.fizz",
"absent": true
},{
"path": "migrations/*_create_admins.down.fizz",
"absent": true
}]
4 changes: 4 additions & 0 deletions buffalo/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ func init() {
generateCmd.AddCommand(generate.GothCmd)
generateCmd.AddCommand(generate.WebpackCmd)
generateCmd.AddCommand(generate.ActionCmd)

generate.ResourceCmd.Flags().BoolVarP(&generate.SkipResourceMigration, "skip-migration", "s", false, "sets resource generator not-to add model migration")
generate.ResourceCmd.Flags().BoolVarP(&generate.SkipResourceModel, "skip-model", "", false, "makes resource generator not to generate model nor migrations")

RootCmd.AddCommand(generateCmd)
}
38 changes: 38 additions & 0 deletions buffalo/cmd/generate/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,47 @@ import (
"github.com/spf13/cobra"
)

const resourceExamples = `$ buffalo g resource users
Generates:

- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go
- migrations/2016020216301234_create_users.up.fizz
- migrations/2016020216301234_create_users.down.fizz

$ buffalo g resource users --skip-migration
Generates:

- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go

$ buffalo g resource users --skip-model
Generates:

- actions/users.go
- actions/users_test.go`

//SkipResourceMigration allows to generate a resource without the migration.
var SkipResourceMigration = false

//SkipResourceModel allows to generate a resource without the model and Migration.
var SkipResourceModel = false

// ResourceCmd generates a new actions/resource file and a stub test.
var ResourceCmd = &cobra.Command{
Use: "resource [name]",
Example: resourceExamples,
Aliases: []string{"r"},
Short: "Generates a new actions/resource file",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("you must specify a resource name")
}

name := args[0]
data := makr.Data{
"name": name,
Expand All @@ -27,7 +59,13 @@ var ResourceCmd = &cobra.Command{
"under": inflect.Underscore(name),
"downFirstCap": inflect.CamelizeDownFirst(name),
"actions": []string{"List", "Show", "New", "Create", "Edit", "Update", "Destroy"},
"args": args,

// Flags
"skipMigration": SkipResourceMigration,
"skipModel": SkipResourceModel,
}

g, err := resource.New(data)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions buffalo/cmd/generate/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func TestGenerateResourceCode(t *testing.T) {
os.Mkdir("actions", 0755)
ioutil.WriteFile("actions/app.go", appGo, 0755)

SkipResourceMigration = false
SkipResourceModel = false

e = ResourceCmd.RunE(&cmd, []string{"users"})
r.Nil(e)

Expand Down
2 changes: 1 addition & 1 deletion generators/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"path/filepath"
"strings"

"github.com/bep/inflect"
"github.com/gobuffalo/buffalo/generators"
"github.com/gobuffalo/makr"
"github.com/markbates/inflect"
)

var runningTests bool
Expand Down
22 changes: 22 additions & 0 deletions generators/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package resource

import (
"fmt"
"os/exec"
"strings"

"github.com/gobuffalo/buffalo/generators"
"github.com/gobuffalo/makr"
"github.com/markbates/inflect"
)

// New generates a new actions/resource file and a stub test.
Expand All @@ -27,6 +29,26 @@ func New(data makr.Data) (*makr.Generator, error) {
)
},
})

if skipModel := data["skipModel"].(bool); skipModel == false {
g.Add(modelCommand(data))
}

g.Add(makr.NewCommand(makr.GoFmt()))

return g, nil
}

func modelCommand(data makr.Data) makr.Command {
modelName := inflect.Underscore(data["singular"].(string))

args := data["args"].([]string)
args = append(args[:0], args[0+1:]...)
args = append([]string{"db", "g", "model", modelName}, args...)

if skipMigration := data["skipMigration"].(bool); skipMigration == true {
args = append(args, "--skip-migration")
}

return makr.NewCommand(exec.Command("buffalo", args...))
}