Changeset 1425
- Timestamp:
- 06/15/05 17:17:58 (3 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/helpers.rb (modified) (6 diffs)
- trunk/actionpack/lib/action_view/base.rb (modified) (1 diff)
- trunk/actionpack/test/controller/helper_test.rb (modified) (9 diffs)
- trunk/actionpack/test/controller/layout_test.rb (deleted)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r1424 r1425 1 1 *SVN* 2 3 * Ensure that helpers are only available to the controllers where they are defined and their subclasses. #1394 [kdole@tamu.edu] 2 4 3 5 * render("foo/bar") works with a layout again trunk/actionpack/lib/action_controller/helpers.rb
r990 r1425 3 3 def self.append_features(base) 4 4 super 5 base.class_eval { class << self; alias_method :inherited_without_helper, :inherited; end } 5 6 # Initialize the base module to aggregate its helpers. 7 base.class_inheritable_accessor :master_helper_module 8 base.master_helper_module = Module.new 9 10 # Extend base with class methods to declare helpers. 6 11 base.extend(ClassMethods) 12 13 base.class_eval do 14 # Wrap inherited to create a new master helper module for subclasses. 15 class << self 16 alias_method :inherited_without_helper, :inherited 17 alias_method :inherited, :inherited_with_helper 18 end 19 20 # Wrap initialize_template_class to extend new template class 21 # instances with the master helper module. 22 alias_method :initialize_template_class_without_helper, :initialize_template_class 23 alias_method :initialize_template_class, :initialize_template_class_with_helper 24 end 7 25 end 8 26 … … 33 51 # available to the templates. 34 52 def add_template_helper(helper_module) #:nodoc: 35 template_class.class_eval "include #{helper_module}"53 master_helper_module.module_eval "include #{helper_module}" 36 54 end 37 55 … … 69 87 70 88 # Evaluate block in template class if given. 71 template_class.module_eval(&block) if block_given?89 master_helper_module.module_eval(&block) if block_given? 72 90 end 73 91 … … 77 95 # makes the link_to controller method available in the view. 78 96 def helper_method(*methods) 79 template_class.controller_delegate(*methods) 97 methods.flatten.each do |method| 98 master_helper_module.module_eval <<-end_eval 99 def #{method}(*args, &block) 100 controller.send(%(#{method}), *args, &block) 101 end 102 end_eval 103 end 80 104 end 81 105 … … 90 114 91 115 private 92 def inherited (child)116 def inherited_with_helper(child) 93 117 inherited_without_helper(child) 94 begin child.helper(child.controller_path) 118 begin 119 child.master_helper_module = Module.new 120 child.master_helper_module.send :include, master_helper_module 121 child.helper child.controller_path 95 122 rescue MissingSourceFile => e 96 123 raise unless e.is_missing?("helpers/#{child.controller_path}_helper") … … 98 125 end 99 126 end 127 128 private 129 # Extend the template class instance with our controller's helper module. 130 def initialize_template_class_with_helper(response) 131 returning(initialize_template_class_without_helper(response)) do 132 response.template.extend self.class.master_helper_module 133 end 134 end 100 135 end 101 136 end trunk/actionpack/lib/action_view/base.rb
r1379 r1425 142 142 143 143 class_eval("include ActionView::Helpers::#{helper_module_name}") if Helpers.const_defined?(helper_module_name) 144 end145 end146 147 def self.controller_delegate(*methods)#:nodoc:148 methods.flatten.each do |method|149 class_eval <<-end_eval150 def #{method}(*args, &block)151 controller.send(%(#{method}), *args, &block)152 end153 end_eval154 144 end 155 145 end trunk/actionpack/test/controller/helper_test.rb
r1174 r1425 10 10 class GamesController < ActionController::Base 11 11 def render_hello_world 12 render _template"hello: <%= stratego %>"12 render :inline => "hello: <%= stratego %>" 13 13 end 14 14 … … 24 24 25 25 class HelperTest < Test::Unit::TestCase 26 27 26 def setup 28 27 # Increment symbol counter. … … 51 50 52 51 def test_deprecated_helper 53 assert_equal helper_methods, missing_methods52 assert_equal expected_helper_methods, missing_methods 54 53 assert_nothing_raised { @controller_class.helper TestHelper } 55 54 assert_equal [], missing_methods … … 59 58 require 'abc_helper' 60 59 self.test_helper = AbcHelper 61 assert_equal helper_methods, missing_methods60 assert_equal expected_helper_methods, missing_methods 62 61 assert_nothing_raised { @controller_class.helper :abc } 63 62 assert_equal [], missing_methods … … 65 64 66 65 def test_declare_missing_helper 67 assert_equal helper_methods, missing_methods66 assert_equal expected_helper_methods, missing_methods 68 67 assert_raise(MissingSourceFile) { @controller_class.helper :missing } 69 68 end … … 79 78 @controller_class.helper { def block_helper_method; end } 80 79 } 81 assert template_methods.include?('block_helper_method')80 assert master_helper_methods.include?('block_helper_method') 82 81 end 83 82 84 83 def test_helper_block_include 85 assert_equal helper_methods, missing_methods84 assert_equal expected_helper_methods, missing_methods 86 85 assert_nothing_raised { 87 86 @controller_class.helper { include TestHelper } … … 92 91 def test_helper_method 93 92 assert_nothing_raised { @controller_class.helper_method :delegate_method } 94 assert template_methods.include?('delegate_method')93 assert master_helper_methods.include?('delegate_method') 95 94 end 96 95 97 96 def test_helper_attr 98 97 assert_nothing_raised { @controller_class.helper_attr :delegate_attr } 99 assert template_methods.include?('delegate_attr')100 assert template_methods.include?('delegate_attr=')98 assert master_helper_methods.include?('delegate_attr') 99 assert master_helper_methods.include?('delegate_attr=') 101 100 end 102 101 … … 110 109 111 110 private 112 def helper_methods; TestHelper.instance_methods end 113 def template_methods; @template_class.instance_methods end 114 def missing_methods; helper_methods - template_methods end 111 def expected_helper_methods 112 TestHelper.instance_methods 113 end 114 115 def master_helper_methods 116 @controller_class.master_helper_module.instance_methods 117 end 118 119 def missing_methods 120 expected_helper_methods - master_helper_methods 121 end 115 122 116 123 def test_helper=(helper_module) … … 120 127 end 121 128 end 129 130 131 class IsolatedHelpersTest < Test::Unit::TestCase 132 class A < ActionController::Base 133 def index 134 render :inline => '<%= shout %>' 135 end 136 137 def rescue_action(e) raise end 138 end 139 140 class B < A 141 helper { def shout; 'B' end } 142 143 def index 144 render :inline => '<%= shout %>' 145 end 146 end 147 148 class C < A 149 helper { def shout; 'C' end } 150 151 def index 152 render :inline => '<%= shout %>' 153 end 154 end 155 156 def setup 157 @request = ActionController::TestRequest.new 158 @response = ActionController::TestResponse.new 159 @request.action = 'index' 160 end 161 162 def test_helper_in_a 163 assert_raise(NameError) { A.process(@request, @response) } 164 end 165 166 def test_helper_in_b 167 assert_equal 'B', B.process(@request, @response).body 168 end 169 170 def test_helper_in_c 171 assert_equal 'C', C.process(@request, @response).body 172 end 173 end