Add unregister_template_handler to prevent leaks.
This commit is contained in:
parent
a5c12cbd3c
commit
a1dbb4e7e0
|
@ -23,10 +23,6 @@ ActiveSupport::Deprecation.debug = true
|
|||
# Disable available locale checks to avoid warnings running the test suite.
|
||||
I18n.enforce_available_locales = false
|
||||
|
||||
# Bogus template processors
|
||||
ActionView::Template.register_template_handler :haml, lambda { |template| "Look its HAML!".inspect }
|
||||
ActionView::Template.register_template_handler :bak, lambda { |template| "Lame backup".inspect }
|
||||
|
||||
FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__))
|
||||
ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
|
||||
|
||||
|
|
|
@ -1,3 +1,9 @@
|
|||
* Add `ActionView::Template::Handler.unregister_template_handler`.
|
||||
|
||||
It performs the opposite of `ActionView::Template::Handler.register_template_handler`.
|
||||
|
||||
*Zuhao Wan*
|
||||
|
||||
* Allow custom `:host` option to be passed to `asset_url` helper that
|
||||
overwrites `config.action_controller.asset_host` for particular asset.
|
||||
|
||||
|
|
|
@ -32,6 +32,15 @@ module ActionView #:nodoc:
|
|||
@@template_extensions = nil
|
||||
end
|
||||
|
||||
# Opposite to register_template_handler.
|
||||
def unregister_template_handler(*extensions)
|
||||
extensions.each do |extension|
|
||||
handler = @@template_handlers.delete extension.to_sym
|
||||
@@default_template_handlers = nil if @@default_template_handlers == handler
|
||||
end
|
||||
@@template_extensions = nil
|
||||
end
|
||||
|
||||
def template_handler_extensions
|
||||
@@template_handlers.keys.map {|key| key.to_s }.sort
|
||||
end
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
require 'abstract_unit'
|
||||
require 'rbconfig'
|
||||
require 'active_support/core_ext/array/extract_options'
|
||||
require 'support/template_handler_helper'
|
||||
|
||||
# The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited
|
||||
# method has access to the view_paths array when looking for a layout to automatically assign.
|
||||
old_load_paths = ActionController::Base.view_paths
|
||||
|
||||
ActionView::Template::register_template_handler :mab,
|
||||
lambda { |template| template.source.inspect }
|
||||
|
||||
ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../../fixtures/actionpack/layout_tests/' ]
|
||||
|
||||
class LayoutTest < ActionController::Base
|
||||
|
@ -39,6 +37,8 @@ class MultipleExtensions < LayoutTest
|
|||
end
|
||||
|
||||
class LayoutAutoDiscoveryTest < ActionController::TestCase
|
||||
include TemplateHandlerHelper
|
||||
|
||||
def setup
|
||||
super
|
||||
@request.host = "www.nextangle.com"
|
||||
|
@ -57,10 +57,12 @@ class LayoutAutoDiscoveryTest < ActionController::TestCase
|
|||
end
|
||||
|
||||
def test_third_party_template_library_auto_discovers_layout
|
||||
@controller = ThirdPartyTemplateLibraryController.new
|
||||
get :hello
|
||||
assert_response :success
|
||||
assert_equal 'layouts/third_party_template_library.mab', @response.body
|
||||
with_template_handler :mab, lambda { |template| template.source.inspect } do
|
||||
@controller = ThirdPartyTemplateLibraryController.new
|
||||
get :hello
|
||||
assert_response :success
|
||||
assert_equal 'layouts/third_party_template_library.mab', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
def test_namespaced_controllers_auto_detect_layouts1
|
||||
|
@ -135,6 +137,7 @@ end
|
|||
|
||||
class LayoutSetInResponseTest < ActionController::TestCase
|
||||
include ActionView::Template::Handlers
|
||||
include TemplateHandlerHelper
|
||||
|
||||
def test_layout_set_when_using_default_layout
|
||||
@controller = DefaultLayoutController.new
|
||||
|
@ -191,9 +194,11 @@ class LayoutSetInResponseTest < ActionController::TestCase
|
|||
end
|
||||
|
||||
def test_layout_set_when_using_render
|
||||
@controller = SetsLayoutInRenderController.new
|
||||
get :hello
|
||||
assert_template :layout => "layouts/third_party_template_library"
|
||||
with_template_handler :mab, lambda { |template| template.source.inspect } do
|
||||
@controller = SetsLayoutInRenderController.new
|
||||
get :hello
|
||||
assert_template :layout => "layouts/third_party_template_library"
|
||||
end
|
||||
end
|
||||
|
||||
def test_layout_is_not_set_when_none_rendered
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
module TemplateHandlerHelper
|
||||
def with_template_handler(*extensions, handler)
|
||||
ActionView::Template.register_template_handler(*extensions, handler)
|
||||
yield
|
||||
ensure
|
||||
ActionView::Template.unregister_template_handler(*extensions)
|
||||
end
|
||||
end
|
|
@ -31,6 +31,7 @@ class DependencyTrackerTest < ActionView::TestCase
|
|||
end
|
||||
|
||||
def teardown
|
||||
ActionView::Template.unregister_template_handler :neckbeard
|
||||
tracker.remove_tracker(:neckbeard)
|
||||
end
|
||||
|
||||
|
|
|
@ -369,23 +369,40 @@ module RenderTestCases
|
|||
|
||||
def test_render_inline_with_render_from_to_proc
|
||||
ActionView::Template.register_template_handler :ruby_handler, :source.to_proc
|
||||
assert_equal '3', @view.render(:inline => "(1 + 2).to_s", :type => :ruby_handler)
|
||||
assert_equal '3', @view.render(inline: "(1 + 2).to_s", type: :ruby_handler)
|
||||
ensure
|
||||
ActionView::Template.unregister_template_handler :ruby_handler
|
||||
end
|
||||
|
||||
def test_render_inline_with_compilable_custom_type
|
||||
ActionView::Template.register_template_handler :foo, CustomHandler
|
||||
assert_equal 'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
|
||||
assert_equal 'source: "Hello, World!"', @view.render(inline: "Hello, World!", type: :foo)
|
||||
ensure
|
||||
ActionView::Template.unregister_template_handler :foo
|
||||
end
|
||||
|
||||
def test_render_inline_with_locals_and_compilable_custom_type
|
||||
ActionView::Template.register_template_handler :foo, CustomHandler
|
||||
assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
|
||||
assert_equal 'source: "Hello, <%= name %>!"', @view.render(inline: "Hello, <%= name %>!", locals: { name: "Josh" }, type: :foo)
|
||||
ensure
|
||||
ActionView::Template.unregister_template_handler :foo
|
||||
end
|
||||
|
||||
def test_render_knows_about_types_registered_when_extensions_are_checked_earlier_in_initialization
|
||||
ActionView::Template::Handlers.extensions
|
||||
ActionView::Template.register_template_handler :foo, CustomHandler
|
||||
assert ActionView::Template::Handlers.extensions.include?(:foo)
|
||||
ensure
|
||||
ActionView::Template.unregister_template_handler :foo
|
||||
end
|
||||
|
||||
def test_render_does_not_use_unregistered_extension_and_template_handler
|
||||
ActionView::Template.register_template_handler :foo, CustomHandler
|
||||
ActionView::Template.unregister_template_handler :foo
|
||||
assert_not ActionView::Template::Handlers.extensions.include?(:foo)
|
||||
assert_equal "Hello, World!", @view.render(inline: "Hello, World!", type: :foo)
|
||||
ensure
|
||||
ActionView::Template::Handlers.class_variable_get(:@@template_handlers).delete(:foo)
|
||||
end
|
||||
|
||||
def test_render_ignores_templates_with_malformed_template_handlers
|
||||
|
@ -474,7 +491,9 @@ module RenderTestCases
|
|||
|
||||
def test_render_with_passing_couple_extensions_to_one_register_template_handler_function_call
|
||||
ActionView::Template.register_template_handler :foo1, :foo2, CustomHandler
|
||||
assert_equal @view.render(:inline => "Hello, World!", :type => :foo1), @view.render(:inline => "Hello, World!", :type => :foo2)
|
||||
assert_equal @view.render(inline: "Hello, World!", type: :foo1), @view.render(inline: "Hello, World!", type: :foo2)
|
||||
ensure
|
||||
ActionView::Template.unregister_template_handler :foo1, :foo2
|
||||
end
|
||||
|
||||
def test_render_throws_exception_when_no_extensions_passed_to_register_template_handler_function_call
|
||||
|
|
Loading…
Reference in New Issue