aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
authorZuhao Wan <wanzuhao@gmail.com>2014-06-09 22:45:46 +0800
committerZuhao Wan <wanzuhao@gmail.com>2014-06-12 18:49:40 +0800
commita1dbb4e7e0a580e013423c7adf8ba3127c4c59e0 (patch)
tree1a6065836115a0592c89fd09dd6363a49466aa91 /actionview
parenta5c12cbd3c0e9b392edb9e4eb13dcdd256327acd (diff)
downloadrails-a1dbb4e7e0a580e013423c7adf8ba3127c4c59e0.tar.gz
rails-a1dbb4e7e0a580e013423c7adf8ba3127c4c59e0.tar.bz2
rails-a1dbb4e7e0a580e013423c7adf8ba3127c4c59e0.zip
Add unregister_template_handler to prevent leaks.
Diffstat (limited to 'actionview')
-rw-r--r--actionview/CHANGELOG.md6
-rw-r--r--actionview/lib/action_view/template/handlers.rb9
-rw-r--r--actionview/test/actionpack/controller/layout_test.rb25
-rw-r--r--actionview/test/support/template_handler_helper.rb8
-rw-r--r--actionview/test/template/dependency_tracker_test.rb1
-rw-r--r--actionview/test/template/render_test.rb27
6 files changed, 62 insertions, 14 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md
index 147e5b47db..105ebb0984 100644
--- a/actionview/CHANGELOG.md
+++ b/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.
diff --git a/actionview/lib/action_view/template/handlers.rb b/actionview/lib/action_view/template/handlers.rb
index d9cddc0040..33bfcb458c 100644
--- a/actionview/lib/action_view/template/handlers.rb
+++ b/actionview/lib/action_view/template/handlers.rb
@@ -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
diff --git a/actionview/test/actionpack/controller/layout_test.rb b/actionview/test/actionpack/controller/layout_test.rb
index b44f57a23e..70d81c0cad 100644
--- a/actionview/test/actionpack/controller/layout_test.rb
+++ b/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
@@ -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
diff --git a/actionview/test/support/template_handler_helper.rb b/actionview/test/support/template_handler_helper.rb
new file mode 100644
index 0000000000..78d9c7cf20
--- /dev/null
+++ b/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
diff --git a/actionview/test/template/dependency_tracker_test.rb b/actionview/test/template/dependency_tracker_test.rb
index df3a0602d1..6c780f2297 100644
--- a/actionview/test/template/dependency_tracker_test.rb
+++ b/actionview/test/template/dependency_tracker_test.rb
@@ -31,6 +31,7 @@ class DependencyTrackerTest < ActionView::TestCase
end
def teardown
+ ActionView::Template.unregister_template_handler :neckbeard
tracker.remove_tracker(:neckbeard)
end
diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb
index ca508abfb8..240a8e3c32 100644
--- a/actionview/test/template/render_test.rb
+++ b/actionview/test/template/render_test.rb
@@ -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