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

Add basic functions into the default implementation of resource functions #297

Closed
as27 opened this issue Mar 18, 2017 · 6 comments
Closed

Comments

@as27
Copy link
Contributor

as27 commented Mar 18, 2017

The generator for resources should generate some more code into the default implementations. The command buffalo g r users should add a basic functionality for the model user.

The generated code should be similar like in the html-resource example. But I would suggest not to use a middleware inside the generated code, because of readability issues.

@markbates
Copy link
Member

It'll be cleaner if you use the new(ish) ValidateAnd... methods https://godoc.org/github.com/markbates/pop#Connection.ValidateAndCreate

@markbates
Copy link
Member

This probably better code to model after https://github.com/gobuffalo/toodo/blob/master/actions/todos.go

@as27
Copy link
Contributor Author

as27 commented Mar 20, 2017

Inside the documentation it is not defined how to use the resources generator for longer named models.

For one word models that is clear, but whats with longer model names. For example a model ContentSection.

How should the user generate that resource?

buffalo g r content_sections
buffalo g r contentSections
buffalo g r ContentSections

or should the generator support all possibilities?

@markbates
Copy link
Member

I would think it should be able to support any of those possibilities.

@as27
Copy link
Contributor Author

as27 commented Mar 22, 2017

I am moving forward. Now I need feedback, because I am not sure if I am on the right track. The following file is completely generated via buffalo g r pages:
Link to the gist: pages.go

Not all functions are completed so the status is WIP. The code compiles and works, when the template files exist.

I added some comments to make that code easier to edit. Is this too much? or should there be much more comments?

Something different: I don't know how to solve this:
In line 32 the path is in [] brackets:
// Show gets the data for one page. This function is mapped to the path GET /pages/[page_id]

That is because inside the template following does not work

GET /{{under}}/{{{singular}}_id} <-- causes an error
GET /{{under}}/\{{{singular}}_id} <-- renders as GET /pages/{{{singular}}_id}

@markbates
Copy link
Member

Wow! This is looking really good so far! Very impressed!

To answer the last question first, you need to escape the {{ otherwise Go templates tries to execute it. Here's an example of how to escape it:

https://github.com/markbates/pop/blob/master/soda%2Fcmd%2Fgenerate%2Fconfig_templates.go#L12

It's ugly, but that's just how you do it. :(

I'm all for comments in the generated code, the more the merrier!

The only comment I'll make at this stage (knowing that this is WIP), is to use the model name as the variable name.

Right now you have this:

t := &models.Page{}

I would make it:

page := &models.Page{}

In addition to generating the corresponding tests, I would generate, at the very least "stub" HTML pages for each action. If you want to get really crazy you can implement the form pages using https://github.com/gobuffalo/tags, but it would be enough to start with just having simple HTML pages like:

<h1>Pages#Show</h1>

Find me in templates/pages/show.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

No branches or pull requests

2 participants