github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/buffalo/cmd/generate/resource.go (about)

     1  package generate
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/markbates/gentronics"
     9  	"github.com/markbates/inflect"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // ResourceCmd generates a new actions/resource file and a stub test.
    14  var ResourceCmd = &cobra.Command{
    15  	Use:     "resource [name]",
    16  	Aliases: []string{"r"},
    17  	Short:   "Generates a new actions/resource file",
    18  	RunE: func(cmd *cobra.Command, args []string) error {
    19  		if len(args) == 0 {
    20  			return errors.New("you must specify a resource name")
    21  		}
    22  		name := args[0]
    23  		data := gentronics.Data{
    24  			"name":         name,
    25  			"singular":     inflect.Singularize(name),
    26  			"plural":       inflect.Pluralize(name),
    27  			"camel":        inflect.Camelize(name),
    28  			"under":        inflect.Underscore(name),
    29  			"downFirstCap": inflect.CamelizeDownFirst(name),
    30  			"actions":      []string{"List", "Show", "New", "Create", "Edit", "Update", "Destroy"},
    31  		}
    32  		return NewResourceGenerator(data).Run(".", data)
    33  	},
    34  }
    35  
    36  // NewResourceGenerator generates a new actions/resource file and a stub test.
    37  func NewResourceGenerator(data gentronics.Data) *gentronics.Generator {
    38  	g := gentronics.New()
    39  	g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s.go", data["downFirstCap"])), rAction))
    40  	g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s_test.go", data["under"])), rResourceTest))
    41  	g.Add(&gentronics.Func{
    42  		Should: func(data gentronics.Data) bool { return true },
    43  		Runner: func(root string, data gentronics.Data) error {
    44  			return addInsideAppBlock(fmt.Sprintf("var %sResource buffalo.Resource", data["downFirstCap"]),
    45  				fmt.Sprintf("%sResource = %sResource{&buffalo.BaseResource{}}", data["downFirstCap"], data["camel"]),
    46  				fmt.Sprintf("app.Resource(\"/%s\", %sResource)", data["under"], data["downFirstCap"]),
    47  			)
    48  		},
    49  	})
    50  	g.Add(Fmt)
    51  	return g
    52  }
    53  
    54  var rAction = `package actions
    55  
    56  import "github.com/gobuffalo/buffalo"
    57  
    58  type {{camel}}Resource struct{
    59  	buffalo.Resource
    60  }
    61  
    62  {{#each actions}}
    63  // {{.}} default implementation.
    64  func (v {{camel}}Resource) {{.}}(c buffalo.Context) error {
    65  	return c.Render(200, r.String("{{camel}}#{{.}}"))
    66  }
    67  
    68  {{/each}}
    69  `
    70  
    71  var rResourceTest = `package actions_test
    72  
    73  import (
    74  	"testing"
    75  
    76  	"github.com/stretchr/testify/require"
    77  )
    78  {{#each actions}}
    79  func Test_{{camel}}Resource_{{camelize .}}(t *testing.T) {
    80  	r := require.New(t)
    81  	r.Fail("Not Implemented!")
    82  }
    83  {{/each}}
    84  `