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

Commit

Permalink
Fix Issue #751 - unknown flag: --use-model
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brunskill committed Nov 29, 2017
1 parent 6730fcc commit 91b2f96
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ RUN buffalo build -static
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 buffalo g resource admins --use-model users
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/resource_use_model.json

RUN rm actions/admins_test.go
RUN rm models/user_test.go
RUN rm models/user.go
RUN rm actions/users_test.go
Expand Down
19 changes: 19 additions & 0 deletions buffalo/cmd/filetests/resource_use_model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[{
"path": "actions/admins.go",
"contains": [
"users := &models.Users{}"
]
},
{
"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
}]
11 changes: 9 additions & 2 deletions buffalo/cmd/generate/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/pkg/errors"

"github.com/gobuffalo/buffalo/generators/resource"
"github.com/gobuffalo/buffalo/meta"
"github.com/gobuffalo/makr"
"github.com/spf13/cobra"
)

var resourceOptions = struct {
SkipMigration bool
SkipModel bool
ModelName string
Name string
MimeType string
}{}
Expand All @@ -31,6 +33,10 @@ var ResourceCmd = &cobra.Command{
o.MimeType = strings.ToUpper(resourceOptions.MimeType)
o.SkipModel = resourceOptions.SkipModel
o.SkipMigration = resourceOptions.SkipMigration
if resourceOptions.ModelName != "" {
o.UseModel = true
o.Model = meta.Name(resourceOptions.ModelName)
}

if err := o.Validate(); err != nil {
return err
Expand All @@ -43,8 +49,9 @@ var ResourceCmd = &cobra.Command{
var resourceMN string

func init() {
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipMigration, "skip-migration", "s", false, "sets resource generator not-to add model migration")
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipModel, "skip-model", "", false, "makes resource generator not to generate model nor migrations")
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().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)")
}
Expand Down
1 change: 1 addition & 0 deletions generators/resource/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Generator struct {
Model meta.Name `json:"model"`
SkipMigration bool `json:"skip_migration"`
SkipModel bool `json:"skip_model"`
UseModel bool `json:"use_model"`
MimeType string `json:"mime_type"`
FilesPath string `json:"files_path"`
ActionsPath string `json:"actions_path"`
Expand Down
2 changes: 1 addition & 1 deletion generators/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (res Generator) Run(root string, data makr.Data) error {
},
})

if !res.SkipModel {
if !res.SkipModel && !res.UseModel {
g.Add(res.modelCommand())
}

Expand Down

0 comments on commit 91b2f96

Please sign in to comment.