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

Resource generator --use-model #297 fixes #328 #333

Merged
merged 33 commits into from
Apr 3, 2017

Conversation

as27
Copy link
Contributor

@as27 as27 commented Mar 27, 2017

Generates a basic implementation for a resource, which supports crud functionality for a existing model.

@markbates markbates changed the base branch from master to development March 27, 2017 18:33
@markbates
Copy link
Member

I ran buffalo g resource user name email bio:text

it's output:

Buffalo version 0.8.0

--> actions/user.go
--> actions/user_test.go
--> buffalo db g model user name email bio:text
v3.13.3

--> models/user.go
--> models/user_test.go
--> goimports -w .
> migrations/20170327183424_create_users.up.fizz
> migrations/20170327183424_create_users.down.fizz
--> goimports -w .

The UserResource, however, didn't have any of the model stuff in it. It should.

package actions

import "github.com/gobuffalo/buffalo"

type UserResource struct {
	buffalo.Resource
}

// List default implementation.
func (v UserResource) List(c buffalo.Context) error {
	return c.Render(200, r.String("User#List"))
}

// Show default implementation.
func (v UserResource) Show(c buffalo.Context) error {
	return c.Render(200, r.String("User#Show"))
}

// New default implementation.
func (v UserResource) New(c buffalo.Context) error {
	return c.Render(200, r.String("User#New"))
}

// Create default implementation.
func (v UserResource) Create(c buffalo.Context) error {
	return c.Render(200, r.String("User#Create"))
}

// Edit default implementation.
func (v UserResource) Edit(c buffalo.Context) error {
	return c.Render(200, r.String("User#Edit"))
}

// Update default implementation.
func (v UserResource) Update(c buffalo.Context) error {
	return c.Render(200, r.String("User#Update"))
}

// Destroy default implementation.
func (v UserResource) Destroy(c buffalo.Context) error {
	return c.Render(200, r.String("User#Destroy"))
}

func (v {{.camel}}Resource) List(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)
{{.downFirstCap}} := &models.{{.camel}}{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be plural.

It's generating user := &models.User{}, but it should be users := &models.Users{}

Copy link
Contributor Author

@as27 as27 Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that resources are always generated with plural args: buffalo g resource users name email bio:text then the logic works. But I am going to change that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model stuff is just implemented with the --use-model flag. Because otherwise the tests will fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the tests need to be fixed. The --use-model flag should be used if you have an existing model, but the default, without any flags, should generate everything.

It's ok in the Dockerfile to delete stuff that was previously generated in the tests, or to create a new app, etc...

@markbates
Copy link
Member

This should generate all of the *.html files as well. Each file can contain something basic like:

<h1>Users#List</h1>
Find me at templates/users/list.html

@as27
Copy link
Contributor Author

as27 commented Mar 29, 2017

  • Logic of the flags changed
  • added a basic structure for html templates. Later just the files can hold more logic
  • changed the generator also to deal with a singular definition.

@markbates
Copy link
Member

OMG! This is sooooooooo awesome!! I'm really excited about this! Just started testing it, but so far so good! I can't wait to get this into v0.8.1.

I have a few notes, but nothing big, as far as I can tell.

// If there are no errors set a success message
c.Flash().Add("success", "{{.model}} was created successfully")
// and redirect to the {{.underPlural}} index page
return c.Redirect(301, "/{{.underPlural}}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have this redirect to the Show action. /users/1234

// If there are no errors set a success message
c.Flash().Add("success", "{{.model}} was edited successfully")
// and redirect to the {{.underPlural}} index page
return c.Redirect(301, "/{{.underPlural}}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have this redirect to the Show action. /users/1234

if err != nil {
return err
}
// Redirect to the {{.underPlural}} index page
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a flash message here to say that it was deleted.

// If there are no errors set a success message
c.Flash().Add("success", "{{.model}} was created successfully")
// and redirect to the {{.underPlural}} index page
return c.Redirect(301, "/{{.underPlural}}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a 302.

// If there are no errors set a success message
c.Flash().Add("success", "{{.model}} was edited successfully")
// and redirect to the {{.underPlural}} index page
return c.Redirect(301, "/{{.underPlural}}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a 302.

return err
}
// Redirect to the {{.underPlural}} index page
return c.Redirect(301, "/{{.underPlural}}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a 302.

var SkipResourceModel = false

// UseResourceModel allows to generate a resource with a working model.
var UseResourceModel = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a string containing the name of the model to use.

buffalo g resource users --use-model=user

@markbates
Copy link
Member

Some issues with pluralization:

buffalo g resource user name email
--> actions/user.go
--> actions/user_test.go
--> templates/users/_form.html
--> templates/users/edit.html
--> templates/users/index.html
--> templates/users/new.html
--> templates/users/show.html
--> buffalo db g model user name email
v3.13.5

--> models/user.go
--> models/user_test.go
--> goimports -w .
> migrations/20170329204449_create_users.up.fizz
> migrations/20170329204449_create_users.down.fizz
--> goimports -w .

In actions/app.go:

var userResource buffalo.Resource
userResource = UserResource{&buffalo.BaseResource{}}
app.Resource("/user", userResource)

But the resource is named UsersResource.

@markbates
Copy link
Member

It looks like the happy path is working great!

$ buffalo g resource users field1 field2 ...
$ buffalo g resource --skip-model users

These still have issues:

$ buffalo g resource user field1 field2 ...
$ buffalo g resource --use-model users

@as27
Copy link
Contributor Author

as27 commented Mar 31, 2017

  • Changed template according to the review
  • Minor changes to support different values for resource and model: buffalo g r users --use-model people
  • Supports longer camel case names for example: UserComments

@as27
Copy link
Contributor Author

as27 commented Mar 31, 2017

Basic set of html templates are now generated.
It supports list, show, edit, new and delete out of the box.

@markbates
Copy link
Member

#352

I fixed a few bugs and cleaned up some of the HTML.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants