Skip to content

Commit

Permalink
Add unregister_template_handler to prevent leaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
zuhao committed Jun 12, 2014
1 parent a5c12cb commit a1dbb4e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 18 deletions.
4 changes: 0 additions & 4 deletions actionmailer/test/abstract_unit.rb
Expand Up @@ -23,10 +23,6 @@
# 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

Expand Down
6 changes: 6 additions & 0 deletions actionview/CHANGELOG.md
@@ -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.

Expand Down
9 changes: 9 additions & 0 deletions actionview/lib/action_view/template/handlers.rb
Expand Up @@ -32,6 +32,15 @@ def register_template_handler(*extensions, handler)
@@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
Expand Down
25 changes: 15 additions & 10 deletions actionview/test/actionpack/controller/layout_test.rb
@@ -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
Expand Down Expand Up @@ -39,6 +37,8 @@ class MultipleExtensions < LayoutTest
end

class LayoutAutoDiscoveryTest < ActionController::TestCase
include TemplateHandlerHelper

def setup
super
@request.host = "www.nextangle.com"
Expand All @@ -57,10 +57,12 @@ def test_controller_name_layout_name_match
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
Expand Down Expand Up @@ -135,6 +137,7 @@ def hello

class LayoutSetInResponseTest < ActionController::TestCase
include ActionView::Template::Handlers
include TemplateHandlerHelper

def test_layout_set_when_using_default_layout
@controller = DefaultLayoutController.new
Expand Down Expand Up @@ -191,9 +194,11 @@ def test_layout_except_exception_when_excepted
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
Expand Down
8 changes: 8 additions & 0 deletions actionview/test/support/template_handler_helper.rb
@@ -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
1 change: 1 addition & 0 deletions actionview/test/template/dependency_tracker_test.rb
Expand Up @@ -31,6 +31,7 @@ def setup
end

def teardown
ActionView::Template.unregister_template_handler :neckbeard
tracker.remove_tracker(:neckbeard)
end

Expand Down
27 changes: 23 additions & 4 deletions actionview/test/template/render_test.rb
Expand Up @@ -369,23 +369,40 @@ def test_render_fallbacks_to_erb_for_unknown_types

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
Expand Down Expand Up @@ -474,7 +491,9 @@ def test_render_layout_with_object

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
Expand Down

0 comments on commit a1dbb4e

Please sign in to comment.