thoughtbot / props_template

A very fast json builder for Rails
MIT License
145 stars 4 forks source link

WIP: Utilize props mimetype #14

Open joshleblanc opened 10 months ago

joshleblanc commented 10 months ago

I was playing around with Superglue in a hobby app, which uses the same controller for html views and api calls. So games/show.json would be your json response, and games/show.html would be your html response.

When you install superglue, it assumes that your json response will be the props, so any jbuilder templates will no longer function in favor of the prop templates. This didn't feel right, so I went poking around at adding a new mime type.

This PR allows specifying the format for a request as :props rather than :json. Your file would have to be names [name].props.pbuilder to be picked up properly.

Note that I changed the name of the file to pbuilder instead of props. This was just to get it working so I had something to show. The reason it's changed is because the file format is [name].[format].[handler]. Without changing the name of either the mime type or the handler, the format ends up being [name].props.props, which looks awful.

--

If you want to test this in a superglue app, the following changes need to be made

  1. Rename app/layouts/application.json.props to app/layouts/application.props.pbuilder
  2. Change the first line of the layout to check if format.props? instead of format.json?
  3. Change formats: [:json] to formats: [:props] in your erb views
  4. Rename your .json.props files to .props.pbuilder
jho406 commented 9 months ago

Hi! Thanks for commenting and trying out superglue!

I'm not quite ready for the proposed change.

When you install superglue, it assumes that your json response will be the props, so any jbuilder templates will no longer function in favor of the prop templates. This didn't feel right, so I went poking around at adding a new mime type.

Yup! props_template is a DSL that builds JSON. Its is a friendly alternative to jbuilder that competes for the same json mime type.

so any jbuilder templates will no longer function in favor of the prop templates.

You can still use jbuilder, as long as its on a different route. Or if you really prefer, superglue.js just expects a JSON response so you can create a games/show.json.jbuilder instead, but you won't have support for layouts or digging, and will have to build your response to match the page response

joshleblanc commented 9 months ago

@jho406 Understandable!

I've changed the PR to use Props::Handler.default_format in place of hardcoded :json symbols. This should let users override it if they want, and register their own mimetype

jho406 commented 9 months ago

@joshleblanc Thanks! I'm more open to that change. Would it be possible to add a test case for it?