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

Explicit module nesting #2532

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

sdwolfz
Copy link
Contributor

@sdwolfz sdwolfz commented Feb 25, 2024

Initial draft of explicit module nesting.

Please take a look and tell me if you're OK with this approach before I convert the controller template as well.

My initial plan to split the template in 3 an add the indentation programmatically instead of editing every line in the template failed as I could not find a way to do it with Rails' own functionality and manual erb templates I tried were giving me weird errors when passing in binding to the template. So I decide to do it the brute force way.

Dockerfile will be deleted before merge.

Please don't merge this in until #2495 is merged as I have to modify the code to make it compatible.

@sdwolfz sdwolfz changed the title Explicit module nestings Explicit module nesting Feb 25, 2024
Copy link
Member

@nickcharlton nickcharlton left a comment

Choose a reason for hiding this comment

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

The need to add a lot of <%= indent %>, isn't sitting well with me, but I can't think of an alternative. What else did you try?

@sdwolfz
Copy link
Contributor Author

sdwolfz commented Feb 27, 2024

The way I tried:

First I split the template into 3:

  1. Top: containing just the module and class definition
  2. Middle: the content of the class as is right now
  3. End: just properly indented end statements

Then I used ERB, passing in binding as argument so it gets rendered with the current context.

This did not work. The template method does a lot more than just ERB rendering and writing to file, there were many things missing and I would get nil errors.

Unfortunately I did not find another way to generate 3 templates and then stitch them together.

@nickcharlton
Copy link
Member

Interesting, thanks! I'd like to play around with it a bit, I think.

@pablobm, do you have any thoughts on this implementation? I'm really not sure what to suggest here.

@pablobm
Copy link
Collaborator

pablobm commented Mar 1, 2024

Yeah, the internals of Rails templates are not easy to work with... 😓 Something that might work though: how about generating the three files (head, body, foot) and then editing and merging them using normal File functions? My understanding is that the template call puts the file in place, suggesting that we might be able to take it from there.

@sdwolfz
Copy link
Contributor Author

sdwolfz commented Apr 4, 2024

@pablobm how about generating the three files (head, body, foot) this is what's failing. Generating the 3 files using ERB is not the same as doing it with template, even if I pass binding in:

tpl = ERB.new(File.read('whatever.erb'))
result = template.result(binding)

This will fail with errors related to missing methods. Binding does not have the same access that template has to the Generator's context. It does more than just erb rendering.

Having the indents added in every line of the template is the only thing I could do to make it work.

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

Successfully merging this pull request may close these issues.

3 participants