Permalink
Browse files

Add .rb template handler

This handler simply allows arbitrary Ruby code as a template
  • Loading branch information...
guilleiguaran committed Sep 11, 2012
1 parent 5d264f2 commit ab7ae689e9727781ef39b01a836a746ac75352fc
View
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Add .rb template handler, this handler simply allows arbitrary Ruby code as a template. *Guillermo Iguaran*
+
* Add `separator` option for `ActionView::Helpers::TextHelper#excerpt`:
excerpt('This is a very beautiful morning', 'very', :separator => ' ', :radius => 1)
@@ -10,6 +10,7 @@ def self.extended(base)
base.register_default_template_handler :erb, ERB.new
base.register_template_handler :builder, Builder.new
base.register_template_handler :raw, Raw.new
+ base.register_template_handler :rb, :source.to_proc

This comment has been minimized.

Show comment
Hide comment
@fxn

fxn Sep 24, 2012

Member

:source.to_proc 🤘

@fxn

fxn Sep 24, 2012

Member

:source.to_proc 🤘

end
@@template_handlers = {}
@@ -0,0 +1,3 @@
+body = ""
+body << ["Hello", "from", "Ruby", "code"].join(" ")
+body
@@ -97,6 +97,14 @@ def test_render_raw_template_with_quotes
assert_equal %q;Here are some characters: !@#$%^&*()-="'}{`; + "\n", @view.render(:template => "plain_text_with_characters")
end
+ def test_render_rb_template_with_handlers
+ assert_equal "Hello from Ruby code", @view.render(:template => "ruby_template")
+ end
+
+ def test_render_rb_template_inline
+ assert_equal '4', @view.render(:inline => "(2**2).to_s", :type => :rb)
+ end
+
def test_render_file_with_localization_on_context_level
old_locale, @view.locale = @view.locale, :da
assert_equal "Hey verden", @view.render(:file => "test/hello_world")

5 comments on commit ab7ae68

@josh

This comment has been minimized.

Show comment
Hide comment
@josh

josh Sep 14, 2012

Member

This is pretty annoying change.

If you have any .rb files under a view path, AP is going to try to precompile them.

Just put a module under app/views echo "module Foo; end" > app/views/foo.rb and your app will just crash will an error message saying "module definition in method body".

My personal usage is mustache view classes.

Member

josh replied Sep 14, 2012

This is pretty annoying change.

If you have any .rb files under a view path, AP is going to try to precompile them.

Just put a module under app/views echo "module Foo; end" > app/views/foo.rb and your app will just crash will an error message saying "module definition in method body".

My personal usage is mustache view classes.

@guilleiguaran

This comment has been minimized.

Show comment
Hide comment
@guilleiguaran

guilleiguaran Sep 14, 2012

Member

@josh I added this since @dhh asked for it after of see it in Railscasts, I will talk with him on CF to check if we can found any solution or if we should revert this

Member

guilleiguaran replied Sep 14, 2012

@josh I added this since @dhh asked for it after of see it in Railscasts, I will talk with him on CF to check if we can found any solution or if we should revert this

@josh

This comment has been minimized.

Show comment
Hide comment
@josh

josh Sep 14, 2012

Member

I like that jbuilder (and similar extensions) use its own extension, not .rb.

Member

josh replied Sep 14, 2012

I like that jbuilder (and similar extensions) use its own extension, not .rb.

@dhh

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Sep 17, 2012

Member

We'll just go with .ruby for now, then.

Member

dhh replied Sep 17, 2012

We'll just go with .ruby for now, then.

@guilleiguaran

This comment has been minimized.

Show comment
Hide comment
Member

guilleiguaran replied Sep 17, 2012

Done in de1060f

Please sign in to comment.