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

Resource generator fixes #328 #352

Merged
merged 36 commits into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b50e1f1
resource generator added
as27 Mar 23, 2017
c3f3847
brackets in comments fixed
as27 Mar 23, 2017
16da440
Merge remote-tracking branch 'refs/remotes/gobuffalo/v0.8.0' into v0.8.0
as27 Mar 23, 2017
6a80500
Merge remote-tracking branch 'refs/remotes/gobuffalo/v0.8.0' into v0.8.0
as27 Mar 24, 2017
45cc609
Deactivated failing tests
as27 Mar 24, 2017
0ed75ed
Merge remote-tracking branch 'refs/remotes/gobuffalo/v0.8.0' into v0.8.0
as27 Mar 25, 2017
e66ae0d
Merge remote-tracking branch 'refs/remotes/gobuffalo/v0.8.0' into v0.8.0
as27 Mar 25, 2017
373a6f1
Revert "Deactivated failing tests"
as27 Mar 25, 2017
803e352
Merge remote-tracking branch 'refs/remotes/gobuffalo/v0.8.0' into v0.8.0
as27 Mar 26, 2017
fcbdd36
Tmpl file for skip-model flag
as27 Mar 26, 2017
3df5846
Added model import path
as27 Mar 26, 2017
9fe351b
Merge remote-tracking branch 'refs/remotes/gobuffalo/v0.8.0' into iss…
as27 Mar 26, 2017
bcf1b29
Changed to flag use-model
as27 Mar 27, 2017
de088e0
Set flag to skip the model
as27 Mar 27, 2017
8b2d84b
Merge branch 'master' into issue/297
markbates Mar 27, 2017
2d08d04
html templates
as27 Mar 28, 2017
fd68d4c
Merge remote-tracking branch 'refs/remotes/gobuffalo/development' int…
as27 Mar 28, 2017
ff559a4
Merge remote-tracking branch 'refs/remotes/gobuffalo/development' int…
as27 Mar 28, 2017
9cfa3cc
Plural fixed, basic html template added
as27 Mar 29, 2017
1c24ce7
moved the users test case after the -race test
as27 Mar 29, 2017
225f1bf
Changes from the review
as27 Mar 30, 2017
1cfd7a9
Merge remote-tracking branch 'refs/remotes/gobuffalo/development' int…
as27 Mar 30, 2017
ac921e0
Merge remote-tracking branch 'refs/remotes/origin/issue/297' into htm…
as27 Mar 30, 2017
f4284a5
First forms added
as27 Mar 30, 2017
f8eb121
Templates changed
as27 Mar 30, 2017
4f901d9
Generate index file
as27 Mar 31, 2017
4cc49cd
html template generator
as27 Mar 31, 2017
34160de
set correct name inside edit.html.tmpl
as27 Mar 31, 2017
319d064
removed debugging output
as27 Mar 31, 2017
2b9283b
Merge pull request #1 from as27/htmltemplates
as27 Mar 31, 2017
8e0f01e
Fixed error in edit.tmpl
as27 Mar 31, 2017
6152d1f
Merge remote-tracking branch 'refs/remotes/gobuffalo/development' int…
as27 Mar 31, 2017
3c16231
Set errors correct that templates can show them
as27 Mar 31, 2017
d367f34
Merge branch 'issue/297' of https://github.com/as27/buffalo into as27…
markbates Apr 3, 2017
818ab30
cleaned up some html and fixed a few bugs
markbates Apr 3, 2017
2cfa770
a little more resource clean up around text based attributes
markbates Apr 3, 2017
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
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ 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

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

WORKDIR $GOPATH/src
RUN buffalo new --skip-pop simple_world
WORKDIR ./simple_world
Expand Down
1 change: 1 addition & 0 deletions buffalo/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {

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")
generate.ResourceCmd.Flags().StringVarP(&generate.UseResourceModel, "use-model", "u", "", "generates crud options for a model")

RootCmd.AddCommand(generateCmd)
}
91 changes: 78 additions & 13 deletions buffalo/cmd/generate/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generate

import (
"errors"
"strings"

"github.com/gobuffalo/buffalo/generators/resource"
"github.com/gobuffalo/makr"
Expand Down Expand Up @@ -30,46 +31,110 @@ Generates:
$ buffalo g resource users --skip-model
Generates:

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

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

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

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

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

// UseResourceModel allows to generate a resource with a working model.
var UseResourceModel = ""

// 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 {
var name, modelName string

// Allow overwriting modelName with the --use-model flag
// buffalo generate resource users --use-model people
if UseResourceModel != "" {
modelName = inflect.Pluralize(UseResourceModel)
}

if len(args) == 0 {
return errors.New("you must specify a resource name")
if UseResourceModel == "" {
return errors.New("you must specify a resource name")
}
// When there is no resource name given and --use-model flag is set
name = UseResourceModel
} else {
// When resource name is specified
name = inflect.Pluralize(args[0])
// If there is no --use-model flag set use the resource to create the model
if modelName == "" {
modelName = name
}
}
modelProps := getModelPropertiesFromArgs(args)

name := args[0]
data := makr.Data{
"name": name,
"singular": inflect.Singularize(name),
"plural": inflect.Pluralize(name),
"camel": inflect.Camelize(name),
"under": inflect.Underscore(name),
"downFirstCap": inflect.CamelizeDownFirst(name),
"actions": []string{"List", "Show", "New", "Create", "Edit", "Update", "Destroy"},
"args": args,
"name": name,
"singular": inflect.Singularize(name),
"plural": name,
"camel": inflect.Camelize(name),
"under": inflect.Underscore(name),
"underSingular": inflect.Singularize(inflect.Underscore(name)),
"downFirstCap": inflect.CamelizeDownFirst(name),
"model": inflect.Singularize(inflect.Camelize(modelName)),
"modelPlural": inflect.Camelize(modelName),
"modelUnder": inflect.Singularize(inflect.Underscore(modelName)),
"modelPluralUnder": inflect.Underscore(modelName),
"varPlural": inflect.CamelizeDownFirst(modelName),
"varSingular": inflect.Singularize(inflect.CamelizeDownFirst(modelName)),
"actions": []string{"List", "Show", "New", "Create", "Edit", "Update", "Destroy"},
"args": args,
"modelProps": modelProps,

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

g, err := resource.New(data)
if err != nil {
return err
}
return g.Run(".", data)
},
}

type modelProp struct {
Name string
Type string
}

func (m modelProp) String() string {
return m.Name
}

func getModelPropertiesFromArgs(args []string) []modelProp {
var mProps []modelProp
if len(args) == 0 {
return mProps
}
for _, a := range args[1:] {
ax := strings.Split(a, ":")
p := modelProp{
Name: inflect.Camelize(ax[0]),
Type: "string",
}
if len(ax) > 1 {
p.Type = strings.ToLower(strings.TrimPrefix(ax[1], "nulls."))
}
mProps = append(mProps, p)
}
return mProps
}
22 changes: 21 additions & 1 deletion buffalo/cmd/generate/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func TestGenerateResourceCode(t *testing.T) {
SkipResourceMigration = false
SkipResourceModel = false

e = ResourceCmd.RunE(&cmd, []string{"users"})
// Testing generator with singular definition
e = ResourceCmd.RunE(&cmd, []string{"user"})
r.Nil(e)

fileData, _ := ioutil.ReadFile("actions/app.go")
Expand All @@ -50,4 +51,23 @@ func TestGenerateResourceCode(t *testing.T) {
r.Contains(string(fileData), "func (as *ActionSuite) Test_UsersResource_Show")
r.Contains(string(fileData), "func (as *ActionSuite) Test_UsersResource_Create")

// Testing generator with plural definition
e = ResourceCmd.RunE(&cmd, []string{"comments"})
r.Nil(e)

fileData, _ = ioutil.ReadFile("actions/app.go")
r.Contains(string(fileData), "var commentsResource buffalo.Resource")
r.Contains(string(fileData), "commentsResource = CommentsResource{&buffalo.BaseResource{}}")
r.Contains(string(fileData), "app.Resource(\"/comments\", commentsResource)")

fileData, _ = ioutil.ReadFile("actions/comments.go")
r.Contains(string(fileData), "type CommentsResource struct {")
r.Contains(string(fileData), "func (v CommentsResource) List(c buffalo.Context) error {")
r.Contains(string(fileData), "func (v CommentsResource) Destroy(c buffalo.Context) error {")

fileData, _ = ioutil.ReadFile("actions/comments_test.go")
r.Contains(string(fileData), "func (as *ActionSuite) Test_CommentsResource_List")
r.Contains(string(fileData), "func (as *ActionSuite) Test_CommentsResource_Show")
r.Contains(string(fileData), "func (as *ActionSuite) Test_CommentsResource_Create")

}
27 changes: 24 additions & 3 deletions generators/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resource
import (
"fmt"
"os/exec"
"path/filepath"
"strings"

"github.com/gobuffalo/buffalo/generators"
Expand All @@ -17,8 +18,29 @@ func New(data makr.Data) (*makr.Generator, error) {
if err != nil {
return nil, err
}
// Get the flags
useModel := data["useModel"].(string)
skipModel := data["skipModel"].(bool)

tmplName := "resource-use_model"

if skipModel == true {
tmplName = "resource-name"
}
for _, f := range files {
g.Add(makr.NewFile(strings.Replace(f.WritePath, "resource-name", data["under"].(string), -1), f.Body))
// Adding the resource template to the generator
if strings.Contains(f.WritePath, tmplName) {
g.Add(makr.NewFile(strings.Replace(f.WritePath, tmplName, data["under"].(string), -1), f.Body))
}
// Adding the html templates to the generator
if strings.Contains(f.WritePath, "model-view-") {
targetPath := filepath.Join(
filepath.Dir(f.WritePath),
data["modelPluralUnder"].(string),
strings.Replace(filepath.Base(f.WritePath), "model-view-", "", -1),
)
g.Add(makr.NewFile(targetPath, f.Body))
}
}
g.Add(&makr.Func{
Should: func(data makr.Data) bool { return true },
Expand All @@ -29,8 +51,7 @@ func New(data makr.Data) (*makr.Generator, error) {
)
},
})

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ func (v {{$.camel}}Resource) {{$a}}(c buffalo.Context) error {
return c.Render(200, r.String("{{$.camel}}#{{$a}}"))
}

{{end}}
{{end}}
Loading