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

Resource generator should be able to generate JSON #365

Closed
markbates opened this issue Apr 6, 2017 · 3 comments
Closed

Resource generator should be able to generate JSON #365

markbates opened this issue Apr 6, 2017 · 3 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@markbates
Copy link
Member

Currently the resource generator only generates HTML resources. It should be able to generate JSON resources as well.

A flag will be needed to do this:

$ buffalo g resource --type=json users name email

When generating a JSON resource, no template files should be generated, and instead of r.HTML("template.html") being used in actions it should be r.JSON(model).

@markbates markbates modified the milestone: 0.8.2 Apr 6, 2017
@stanislas-m
Copy link
Member

@markbates I can take this one if you want.

@stanislas-m stanislas-m added the enhancement New feature or request label Apr 7, 2017
@markbates
Copy link
Member Author

that would be awesome!

@stanislas-m stanislas-m self-assigned this Apr 7, 2017
@u007
Copy link
Member

u007 commented Apr 14, 2017

i have modified model_templates with this code:

func (a *{{.model_name}}) ToJSON() ([]byte, error) {
	res, err := json.Marshal(a.ToHash())
	return res, err
}

func (a *{{.model_name}}) ToHash() map[string]interface{} {
	res := map[string]interface{}{
		{{range $a := .model.Attributes -}}"{{$a.Names.Original}}":         {{if eq $a.Names.Original "created_at"}}ISOTimeFormat(a.{{$a.Names.Original}}){{else if eq $a.Names.Original "updated_at"}}ISOTimeFormat(a.{{$a.Names.Original}}){{else}}a.{{$a.Names.Original}}{{end}},
		{{end -}}
	}
	return res
}

and this declared somewhere in models directory

func ISOTimeFormat(pTime time.Time) string {
	if pTime.Year() <= 1 {
		return ""
	}
	return pTime.UTC().Format("2006-01-02T15:04:05")+"Z"//for utc
}

and also, ive replaced with my own command with copy of original code to generate reactjs+redux templates :)

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

No branches or pull requests

3 participants