diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-06-15 17:17:58 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-06-15 17:17:58 +0000 |
commit | 7bb486055e62b33547101a10d2505564ff1d004f (patch) | |
tree | 0667f7731beafef5899cd63edca248f0c27046a7 /actionpack/lib/action_controller/helpers.rb | |
parent | 14c378cc7fd364ea5d96e13746345a29341cd845 (diff) | |
download | rails-7bb486055e62b33547101a10d2505564ff1d004f.tar.gz rails-7bb486055e62b33547101a10d2505564ff1d004f.tar.bz2 rails-7bb486055e62b33547101a10d2505564ff1d004f.zip |
r1318@iwill: jeremy | 2005-06-15 01:08:22 -0700
Ticket 1394 - Helper isolation
r1319@iwill: jeremy | 2005-06-15 01:10:00 -0700
Formulate a test case for helper isolation.
r1331@iwill: jeremy | 2005-06-15 15:21:07 -0700
Update changelog
r1332@iwill: jeremy | 2005-06-15 15:21:30 -0700
Remove superfluous, broken layout_test
r1333@iwill: jeremy | 2005-06-15 15:24:10 -0700
Use an anonymous Module to store helpers per-class instead of tossing them all in template_class. Create a new helper module for subclasses which includes its superclass' helper module. Remove unnecessary ActionView::Base.controller_delegate. Update helper tests.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1425 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/helpers.rb')
-rw-r--r-- | actionpack/lib/action_controller/helpers.rb | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/actionpack/lib/action_controller/helpers.rb b/actionpack/lib/action_controller/helpers.rb index ef58d38021..86653e4f06 100644 --- a/actionpack/lib/action_controller/helpers.rb +++ b/actionpack/lib/action_controller/helpers.rb @@ -2,8 +2,26 @@ module ActionController #:nodoc: module Helpers #:nodoc: def self.append_features(base) super - base.class_eval { class << self; alias_method :inherited_without_helper, :inherited; end } + + # Initialize the base module to aggregate its helpers. + base.class_inheritable_accessor :master_helper_module + base.master_helper_module = Module.new + + # Extend base with class methods to declare helpers. base.extend(ClassMethods) + + base.class_eval do + # Wrap inherited to create a new master helper module for subclasses. + class << self + alias_method :inherited_without_helper, :inherited + alias_method :inherited, :inherited_with_helper + end + + # Wrap initialize_template_class to extend new template class + # instances with the master helper module. + alias_method :initialize_template_class_without_helper, :initialize_template_class + alias_method :initialize_template_class, :initialize_template_class_with_helper + end end # The template helpers serves to relieve the templates from including the same inline code again and again. It's a @@ -32,7 +50,7 @@ module ActionController #:nodoc: # See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules # available to the templates. def add_template_helper(helper_module) #:nodoc: - template_class.class_eval "include #{helper_module}" + master_helper_module.module_eval "include #{helper_module}" end # Declare a helper: @@ -68,7 +86,7 @@ module ActionController #:nodoc: end # Evaluate block in template class if given. - template_class.module_eval(&block) if block_given? + master_helper_module.module_eval(&block) if block_given? end # Declare a controller method as a helper. For example, @@ -76,7 +94,13 @@ module ActionController #:nodoc: # def link_to(name, options) ... end # makes the link_to controller method available in the view. def helper_method(*methods) - template_class.controller_delegate(*methods) + methods.flatten.each do |method| + master_helper_module.module_eval <<-end_eval + def #{method}(*args, &block) + controller.send(%(#{method}), *args, &block) + end + end_eval + end end # Declare a controller attribute as a helper. For example, @@ -89,13 +113,24 @@ module ActionController #:nodoc: end private - def inherited(child) + def inherited_with_helper(child) inherited_without_helper(child) - begin child.helper(child.controller_path) + begin + child.master_helper_module = Module.new + child.master_helper_module.send :include, master_helper_module + child.helper child.controller_path rescue MissingSourceFile => e raise unless e.is_missing?("helpers/#{child.controller_path}_helper") end end end + + private + # Extend the template class instance with our controller's helper module. + def initialize_template_class_with_helper(response) + returning(initialize_template_class_without_helper(response)) do + response.template.extend self.class.master_helper_module + end + end end end |