1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add clear_helpers as a way to clean up all helpers added to this controller, maintaing just the helper with the same name as the controller.

This commit is contained in:
José Valim 2010-08-26 16:03:10 -03:00
parent 84cab320bc
commit 66ef92272c
3 changed files with 31 additions and 2 deletions

View file

@ -95,6 +95,13 @@ module AbstractController
_helpers.module_eval(&block) if block_given? _helpers.module_eval(&block) if block_given?
end end
# Clears up all existing helpers in this class, only keeping the helper
# with the same name as this class.
def clear_helpers
self._helpers = Module.new
default_helper_module! unless anonymous?
end
private private
# Makes all the (instance) methods in the helper module available to templates # Makes all the (instance) methods in the helper module available to templates
# rendered through this controller. # rendered through this controller.

View file

@ -38,6 +38,10 @@ module AbstractController
end end
end end
class ::HelperyTestController < AbstractHelpers
clear_helpers
end
class AbstractHelpersBlock < ControllerWithHelpers class AbstractHelpersBlock < ControllerWithHelpers
helper do helper do
include ::AbstractController::Testing::HelperyTest include ::AbstractController::Testing::HelperyTest
@ -45,7 +49,6 @@ module AbstractController
end end
class TestHelpers < ActiveSupport::TestCase class TestHelpers < ActiveSupport::TestCase
def setup def setup
@controller = AbstractHelpers.new @controller = AbstractHelpers.new
end end
@ -74,8 +77,22 @@ module AbstractController
@controller.process(:with_module) @controller.process(:with_module)
assert_equal "Module Included", @controller.response_body assert_equal "Module Included", @controller.response_body
end end
end end
class ClearHelpersTest < ActiveSupport::TestCase
def setup
@controller = HelperyTestController.new
end
def test_clears_up_previous_helpers
@controller.process(:with_symbol)
assert_equal "I respond to bare_a: false", @controller.response_body
end
def test_includes_controller_default_helper
@controller.process(:with_block)
assert_equal "Hello Default", @controller.response_body
end
end
end end
end end

View file

@ -0,0 +1,5 @@
module HelperyTestHelper
def helpery_test
"Default"
end
end