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

Created a Raw handler for templates. #6309

Merged
merged 1 commit into from May 17, 2012
Merged

Conversation

steveklabnik
Copy link
Member

This addresses the issue brought up in #2394.

I built this based on @josevalim's suggestion, which involves creating a raw template and adding a deprecation message for default template handlers.

One issue with this as it stands is that the tests now throw some warnings; I should probably fix those up, but I wanted to get feedback on my test and this code first. I'm 99% sure that it's all good, but I've never dived into this part of Rails before.

@josevalim
Copy link
Contributor

Thanks @steveklabnik ! This looks great except the template is never being registered. So nobody can render it with a .raw extension. Could you please register it as well and add another test? Thanks!

@steveklabnik
Copy link
Member Author

I will do that later today.

@steveklabnik
Copy link
Member Author

Turns out I have a spare ten minutes here...

I've amended the commit to include the registration, but since there aren't any tests about the registration so far, I'm not sure how to write it. Any hints?

@josevalim
Copy link
Contributor

Well, I would just create a template with raw extension in the fixtures directory and render it by path.

@josevalim
Copy link
Contributor

@steveklabnik
Copy link
Member Author

Awesome, give me a few hours. Thanks for the help.

@steveklabnik
Copy link
Member Author

Okay, so I've added that test. But there are... 505 tests that throw this deprecation notice now. Ouch. I'm guessing it probably shouldn't output that, huh?

@josevalim
Copy link
Contributor

Yes, we should probably fix the majority of these before merging. On the good side, they will probably be fixed by simply renaming the files to have .erb extension.

@steveklabnik
Copy link
Member Author

I made a silly mistake: threw the warning no matter what! That's been fixed, and now we only get one warning, in the NullResolverTest. I'm not 100% sure how to fix it yet... gonna dig in at lunch, I think.

@steveklabnik
Copy link
Member Author

The tests now have zero deprecation warnings. We should be good to go!

module Template::Handlers
class Raw
def call(template)
"'#{template.source}';"
Copy link
Contributor

Choose a reason for hiding this comment

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

One last thing: wouldn't template.source.inspect be a better alternative here since it would automatically escape quotes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Quite possibly. I was copying what was done in the Builder handler. I can absolutely change that if you'd like, I have no idea what's the best.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure using inspect is good:

 1) Failure:
test_render_raw_template_with_handlers(CachedViewRenderTest) [/Users/steve/src/rails/actionpack/test/template/render_test.rb:83]:
--- expected
+++ actual
@@ -1,2 +1,2 @@
-"<%= hello_world %>
-"
+"\"<%= hello_world %>\
+\""


  2) Failure:
test_render_raw_template_with_handlers(LazyViewRenderTest) [/Users/steve/src/rails/actionpack/test/template/render_test.rb:83]:
--- expected
+++ actual
@@ -1,2 +1,2 @@
-"<%= hello_world %>
-"
+"\"<%= hello_world %>\
+\""



  3) Failure:
test_raw_template(TestERBTemplate) [/Users/steve/src/rails/actionpack/test/template/template_test.rb:69]:
Expected: "<%= hello %>"
  Actual: "\"<%= hello %>\""

Or is that what we'd want?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hrm, so let's change the question. Using the current solution (without
inspect), does it work if I have single- or double-quotes in the template?

José Valim
www.plataformatec.com.br
Founder and Lead Developer
*

@steveklabnik
Copy link
Member Author

Updated to explicitly test quoting.

@josevalim
Copy link
Contributor

Jut playing devil's advocate, but now it can fail if it has a } inside the
template or not? :)

José Valim
www.plataformatec.com.br
Founder and Lead Developer
*

@steveklabnik
Copy link
Member Author

Hahahaha. Lemme check that...

@steveklabnik
Copy link
Member Author

Apparently it does work. I updated the test to test a bunch of characters, regardless.

@steveklabnik
Copy link
Member Author

Actually, @evanphx says: It detects matching {}'s yes, but if there just a } it will blow up

@steveklabnik
Copy link
Member Author

So, this commit is now just a WIP, as it fails the test, but @rkh pointed out to me what tilt does: https://github.com/rtomayko/tilt/blob/master/lib/tilt/string.rb

Trying to port that here. Tests are failing, but I have a few hours where I can't work on this, so I figured i'd push my wip up

@steveklabnik
Copy link
Member Author

Okay! This now works. Some discussion here: https://gist.github.com/2720171

@josevalim
Copy link
Contributor

Merged, thanks a lot!

josevalim added a commit that referenced this pull request May 17, 2012
Created a Raw handler for templates.
@josevalim josevalim merged commit f5ebc9a into rails:master May 17, 2012
@steveklabnik
Copy link
Member Author

Yay! :D

@josevalim
Copy link
Contributor

Added a changelog entry here:

https://github.com/rails/rails/blob/master/actionpack/CHANGELOG.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants