Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a use_superglue_defaults action. #1

Open
jho406 opened this issue Jan 23, 2025 · 0 comments
Open

Add a use_superglue_defaults action. #1

jho406 opened this issue Jan 23, 2025 · 0 comments
Assignees

Comments

@jho406
Copy link
Contributor

jho406 commented Jan 23, 2025

context

This issue is related to thoughtbot/superglue#67 and thoughtbot/superglue#47.

Since superglue_rails was extracted from the original superglue monorepo, i'll do a quick recap:

app/
|-- controllers/
|-- views/
|   |-- dashboard/
|   |   |-- index.jsx
|   |   |-- index.json.props
|   |   |-- index.html.erb
|   |-- posts/
|   |   |-- index.jsx
|   |   |-- index.json.props
|   |   |-- index.html.erb
|   |-- users/
|   |   |-- index.jsx
|   |   |-- index.json.props
|   |   |-- index.html.erb

In a typical superglue app, the html.erb files for every action are virtually the same, and looks like this:

<% initial_state = controller.render_to_string(active_template_virtual_path, formats: [:json], locals: local_assigns, layout: true) %>
<script type="text/javascript">
  window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;
</script>
<div id="app"></div>

Goal

I would like for superglue_rails to add a way to modify the rendering (implicit and explicit render) behavior so that we can omit the html.erb files:

app/
|-- controllers/
|-- views/
|   |-- dashboard/
|   |   |-- index.jsx
|   |   |-- index.json.props
|   |-- posts/
|   |   |-- index.jsx
|   |   |-- index.json.props
|   |-- users/
|   |   |-- index.jsx
|   |   |-- index.json.props

I'm wondering if a before_callback is the best path forward. For example:

before_action :use_superglue_defaults

We would need to override the default_render and render.

default_render

checks first if the template exists (implicitly using the request.format, if it does, then call render). If the request.format.html? is true, then this breaks, we need to override so that NOT having a html.erb file is acceptable. The override behavior probably should be: if the request format is html AND the template file for the json version exist && @_use_superglue_defaults is true, then call render, else super.

render

needs to be changed so that its rendering a template explicitly. Something like application/superglue.html.erb with the same content as the erb snipped above.. The override behavior should

  • if the request format is html AND @_user_superglue_defaults is true, then render_to_string the json version of the template, that that content and inject it to a @superglue_initial_page_state and set that as the initial_page_state in the application/superglue.html.erb.
  • Somehow make the template configurable.

I also want to make sure that the below is possible, a locally defined index.html.erb should override a superglue.html.erb:

app/
|-- controllers/
|-- views/
|   |-- application/
|   |   |-- superglue.html.erb
|   |-- dashboard/
|   |   |-- index.html.erb
|   |   |-- index.jsx
|   |   |-- index.json.props
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant