Skip to content
This repository
Browse code

Created a Raw handler for templates.

Fixes #2394
  • Loading branch information...
commit 8bea607265a2c9bb9bb2188b0a79089ca373b814 1 parent 1839ecd
Steve Klabnik authored May 13, 2012
2  actionpack/lib/action_view/template/handlers.rb
@@ -4,10 +4,12 @@ class Template
4 4
     module Handlers #:nodoc:
5 5
       autoload :ERB, 'action_view/template/handlers/erb'
6 6
       autoload :Builder, 'action_view/template/handlers/builder'
  7
+      autoload :Raw, 'action_view/template/handlers/raw'
7 8
 
8 9
       def self.extended(base)
9 10
         base.register_default_template_handler :erb, ERB.new
10 11
         base.register_template_handler :builder, Builder.new
  12
+        base.register_template_handler :raw, Raw.new
11 13
       end
12 14
 
13 15
       @@template_handlers = {}
11  actionpack/lib/action_view/template/handlers/raw.rb
... ...
@@ -0,0 +1,11 @@
  1
+module ActionView
  2
+  module Template::Handlers
  3
+    class Raw
  4
+      def call(template)
  5
+        escaped = template.source.gsub(':', '\:')
  6
+
  7
+        '%q:' + escaped + ':;'
  8
+      end
  9
+    end
  10
+  end
  11
+end
4  actionpack/lib/action_view/template/resolver.rb
@@ -171,7 +171,9 @@ def mtime(p)
171 171
     def extract_handler_and_format(path, default_formats)
172 172
       pieces = File.basename(path).split(".")
173 173
       pieces.shift
174  
-      handler = Template.handler_for_extension(pieces.pop)
  174
+      extension = pieces.pop
  175
+      ActiveSupport::Deprecation.warn "The file #{path} did not specify a template handler. The default is currently ERB, but will change to RAW in the future." unless extension
  176
+      handler = Template.handler_for_extension(extension)
175 177
       format  = pieces.last && Mime[pieces.last]
176 178
       [handler, format]
177 179
     end
1  actionpack/test/fixtures/plain_text.raw
... ...
@@ -0,0 +1 @@
  1
+<%= hello_world %>
1  actionpack/test/fixtures/plain_text_with_characters.raw
... ...
@@ -0,0 +1 @@
  1
+Here are some characters: !@#$%^&*()-="'}{`
8  actionpack/test/template/render_test.rb
@@ -79,6 +79,14 @@ def test_render_template_with_handlers
79 79
     assert_equal "<h1>No Comment</h1>\n", @view.render(:template => "comments/empty", :handlers => [:builder])
80 80
   end
81 81
 
  82
+  def test_render_raw_template_with_handlers
  83
+    assert_equal "<%= hello_world %>\n", @view.render(:template => "plain_text")
  84
+  end
  85
+
  86
+  def test_render_raw_template_with_quotes
  87
+    assert_equal %q;Here are some characters: !@#$%^&*()-="'}{`; + "\n", @view.render(:template => "plain_text_with_characters")
  88
+  end
  89
+
82 90
   def test_render_file_with_localization_on_context_level
83 91
     old_locale, @view.locale = @view.locale, :da
84 92
     assert_equal "Hey verden", @view.render(:file => "test/hello_world")
7  actionpack/test/template/template_test.rb
@@ -48,7 +48,7 @@ def my_buffer
48 48
   end
49 49
 
50 50
   def new_template(body = "<%= hello %>", details = {})
51  
-    ActionView::Template.new(body, "hello template", ERBHandler, {:virtual_path => "hello"}.merge!(details))
  51
+    ActionView::Template.new(body, "hello template", details.fetch(:handler) { ERBHandler }, {:virtual_path => "hello"}.merge!(details))
52 52
   end
53 53
 
54 54
   def render(locals = {})
@@ -64,6 +64,11 @@ def test_basic_template
64 64
     assert_equal "Hello", render
65 65
   end
66 66
 
  67
+  def test_raw_template
  68
+    @template = new_template("<%= hello %>", :handler => ActionView::Template::Handlers::Raw.new)
  69
+    assert_equal "<%= hello %>", render
  70
+  end
  71
+
67 72
   def test_template_loses_its_source_after_rendering
68 73
     @template = new_template
69 74
     render
4  actionpack/test/template/testing/fixture_resolver_test.rb
@@ -8,8 +8,8 @@ def test_should_return_empty_list_for_unknown_path
8 8
   end
9 9
 
10 10
   def test_should_return_template_for_declared_path
11  
-    resolver = ActionView::FixtureResolver.new("arbitrary/path" => "this text")
12  
-    templates = resolver.find_all("path", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => []})
  11
+    resolver = ActionView::FixtureResolver.new("arbitrary/path.erb" => "this text")
  12
+    templates = resolver.find_all("path", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => [:erb]})
13 13
     assert_equal 1, templates.size, "expected one template"
14 14
     assert_equal "this text",      templates.first.source
15 15
     assert_equal "arbitrary/path", templates.first.virtual_path
4  actionpack/test/template/testing/null_resolver_test.rb
@@ -3,10 +3,10 @@
3 3
 class NullResolverTest < ActiveSupport::TestCase
4 4
   def test_should_return_template_for_any_path
5 5
     resolver = ActionView::NullResolver.new()
6  
-    templates = resolver.find_all("path", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => []})
  6
+    templates = resolver.find_all("path.erb", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => []})
7 7
     assert_equal 1, templates.size, "expected one template"
8 8
     assert_equal "Template generated by Null Resolver", templates.first.source
9  
-    assert_equal "arbitrary/path", templates.first.virtual_path
  9
+    assert_equal "arbitrary/path.erb", templates.first.virtual_path
10 10
     assert_equal [:html],          templates.first.formats
11 11
   end
12 12
 end

0 notes on commit 8bea607

Please sign in to comment.
Something went wrong with that request. Please try again.