mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #6309 from steveklabnik/fix-2394
Created a Raw handler for templates.
This commit is contained in:
commit
f5ebc9a232
9 changed files with 36 additions and 6 deletions
|
@ -4,10 +4,12 @@ module ActionView #:nodoc:
|
||||||
module Handlers #:nodoc:
|
module Handlers #:nodoc:
|
||||||
autoload :ERB, 'action_view/template/handlers/erb'
|
autoload :ERB, 'action_view/template/handlers/erb'
|
||||||
autoload :Builder, 'action_view/template/handlers/builder'
|
autoload :Builder, 'action_view/template/handlers/builder'
|
||||||
|
autoload :Raw, 'action_view/template/handlers/raw'
|
||||||
|
|
||||||
def self.extended(base)
|
def self.extended(base)
|
||||||
base.register_default_template_handler :erb, ERB.new
|
base.register_default_template_handler :erb, ERB.new
|
||||||
base.register_template_handler :builder, Builder.new
|
base.register_template_handler :builder, Builder.new
|
||||||
|
base.register_template_handler :raw, Raw.new
|
||||||
end
|
end
|
||||||
|
|
||||||
@@template_handlers = {}
|
@@template_handlers = {}
|
||||||
|
|
11
actionpack/lib/action_view/template/handlers/raw.rb
Normal file
11
actionpack/lib/action_view/template/handlers/raw.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
module ActionView
|
||||||
|
module Template::Handlers
|
||||||
|
class Raw
|
||||||
|
def call(template)
|
||||||
|
escaped = template.source.gsub(':', '\:')
|
||||||
|
|
||||||
|
'%q:' + escaped + ':;'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -171,7 +171,9 @@ module ActionView
|
||||||
def extract_handler_and_format(path, default_formats)
|
def extract_handler_and_format(path, default_formats)
|
||||||
pieces = File.basename(path).split(".")
|
pieces = File.basename(path).split(".")
|
||||||
pieces.shift
|
pieces.shift
|
||||||
handler = Template.handler_for_extension(pieces.pop)
|
extension = pieces.pop
|
||||||
|
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
|
||||||
|
handler = Template.handler_for_extension(extension)
|
||||||
format = pieces.last && Mime[pieces.last]
|
format = pieces.last && Mime[pieces.last]
|
||||||
[handler, format]
|
[handler, format]
|
||||||
end
|
end
|
||||||
|
|
1
actionpack/test/fixtures/plain_text.raw
vendored
Normal file
1
actionpack/test/fixtures/plain_text.raw
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<%= hello_world %>
|
1
actionpack/test/fixtures/plain_text_with_characters.raw
vendored
Normal file
1
actionpack/test/fixtures/plain_text_with_characters.raw
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Here are some characters: !@#$%^&*()-="'}{`
|
|
@ -79,6 +79,14 @@ module RenderTestCases
|
||||||
assert_equal "<h1>No Comment</h1>\n", @view.render(:template => "comments/empty", :handlers => [:builder])
|
assert_equal "<h1>No Comment</h1>\n", @view.render(:template => "comments/empty", :handlers => [:builder])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_raw_template_with_handlers
|
||||||
|
assert_equal "<%= hello_world %>\n", @view.render(:template => "plain_text")
|
||||||
|
end
|
||||||
|
|
||||||
|
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_file_with_localization_on_context_level
|
def test_render_file_with_localization_on_context_level
|
||||||
old_locale, @view.locale = @view.locale, :da
|
old_locale, @view.locale = @view.locale, :da
|
||||||
assert_equal "Hey verden", @view.render(:file => "test/hello_world")
|
assert_equal "Hey verden", @view.render(:file => "test/hello_world")
|
||||||
|
|
|
@ -48,7 +48,7 @@ class TestERBTemplate < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_template(body = "<%= hello %>", details = {})
|
def new_template(body = "<%= hello %>", details = {})
|
||||||
ActionView::Template.new(body, "hello template", ERBHandler, {:virtual_path => "hello"}.merge!(details))
|
ActionView::Template.new(body, "hello template", details.fetch(:handler) { ERBHandler }, {:virtual_path => "hello"}.merge!(details))
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(locals = {})
|
def render(locals = {})
|
||||||
|
@ -64,6 +64,11 @@ class TestERBTemplate < ActiveSupport::TestCase
|
||||||
assert_equal "Hello", render
|
assert_equal "Hello", render
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_raw_template
|
||||||
|
@template = new_template("<%= hello %>", :handler => ActionView::Template::Handlers::Raw.new)
|
||||||
|
assert_equal "<%= hello %>", render
|
||||||
|
end
|
||||||
|
|
||||||
def test_template_loses_its_source_after_rendering
|
def test_template_loses_its_source_after_rendering
|
||||||
@template = new_template
|
@template = new_template
|
||||||
render
|
render
|
||||||
|
|
|
@ -8,8 +8,8 @@ class FixtureResolverTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_return_template_for_declared_path
|
def test_should_return_template_for_declared_path
|
||||||
resolver = ActionView::FixtureResolver.new("arbitrary/path" => "this text")
|
resolver = ActionView::FixtureResolver.new("arbitrary/path.erb" => "this text")
|
||||||
templates = resolver.find_all("path", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => []})
|
templates = resolver.find_all("path", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => [:erb]})
|
||||||
assert_equal 1, templates.size, "expected one template"
|
assert_equal 1, templates.size, "expected one template"
|
||||||
assert_equal "this text", templates.first.source
|
assert_equal "this text", templates.first.source
|
||||||
assert_equal "arbitrary/path", templates.first.virtual_path
|
assert_equal "arbitrary/path", templates.first.virtual_path
|
||||||
|
|
|
@ -3,10 +3,10 @@ require 'abstract_unit'
|
||||||
class NullResolverTest < ActiveSupport::TestCase
|
class NullResolverTest < ActiveSupport::TestCase
|
||||||
def test_should_return_template_for_any_path
|
def test_should_return_template_for_any_path
|
||||||
resolver = ActionView::NullResolver.new()
|
resolver = ActionView::NullResolver.new()
|
||||||
templates = resolver.find_all("path", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => []})
|
templates = resolver.find_all("path.erb", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => []})
|
||||||
assert_equal 1, templates.size, "expected one template"
|
assert_equal 1, templates.size, "expected one template"
|
||||||
assert_equal "Template generated by Null Resolver", templates.first.source
|
assert_equal "Template generated by Null Resolver", templates.first.source
|
||||||
assert_equal "arbitrary/path", templates.first.virtual_path
|
assert_equal "arbitrary/path.erb", templates.first.virtual_path
|
||||||
assert_equal [:html], templates.first.formats
|
assert_equal [:html], templates.first.formats
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue