aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view.rb6
-rw-r--r--actionpack/lib/action_view/base.rb10
-rw-r--r--actionpack/lib/action_view/test_case.rb64
3 files changed, 74 insertions, 6 deletions
diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb
index f9de5c1307..609334d52d 100644
--- a/actionpack/lib/action_view.rb
+++ b/actionpack/lib/action_view.rb
@@ -38,6 +38,8 @@ require 'action_view/template_error'
ActionView::Base.class_eval do
include ActionView::Partials
-end
-ActionView::Base.load_helpers
+ ActionView::Base.helper_modules.each do |helper_module|
+ include helper_module
+ end
+end
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index e57c447682..12dd7d2bc9 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -205,15 +205,17 @@ module ActionView #:nodoc:
class ObjectWrapper < Struct.new(:value) #:nodoc:
end
- def self.load_helpers #:nodoc:
- Dir.entries("#{File.dirname(__FILE__)}/helpers").sort.each do |file|
+ def self.helper_modules #:nodoc:
+ helpers = []
+ Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file|
next unless file =~ /^([a-z][a-z_]*_helper).rb$/
require "action_view/helpers/#{$1}"
helper_module_name = $1.camelize
if Helpers.const_defined?(helper_module_name)
- include Helpers.const_get(helper_module_name)
+ helpers << Helpers.const_get(helper_module_name)
end
end
+ return helpers
end
def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
@@ -323,7 +325,7 @@ If you are rendering a subtemplate, you must now use controller-like partial syn
end
end
- private
+ private
def wrap_content_for_layout(content)
original_content_for_layout = @content_for_layout
@content_for_layout = content
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
new file mode 100644
index 0000000000..b2e6589d81
--- /dev/null
+++ b/actionpack/lib/action_view/test_case.rb
@@ -0,0 +1,64 @@
+require 'active_support/test_case'
+
+module ActionView
+ class NonInferrableHelperError < ActionViewError
+ def initialize(name)
+ super "Unable to determine the helper to test from #{name}. " +
+ "You'll need to specify it using tests YourHelper in your " +
+ "test case definition"
+ end
+ end
+
+ class TestCase < ActiveSupport::TestCase
+ class_inheritable_accessor :helper_class
+ @@helper_class = nil
+
+ class << self
+ def tests(helper_class)
+ self.helper_class = helper_class
+ end
+
+ def helper_class
+ if current_helper_class = read_inheritable_attribute(:helper_class)
+ current_helper_class
+ else
+ self.helper_class = determine_default_helper_class(name)
+ end
+ end
+
+ def determine_default_helper_class(name)
+ name.sub(/Test$/, '').constantize
+ rescue NameError
+ raise NonInferrableHelperError.new(name)
+ end
+ end
+
+ ActionView::Base.helper_modules.each do |helper_module|
+ include helper_module
+ end
+ include ActionController::PolymorphicRoutes
+ include ActionController::RecordIdentifier
+
+ setup :setup_with_helper_class
+
+ def setup_with_helper_class
+ self.class.send(:include, helper_class)
+ end
+
+ class TestController < ActionController::Base
+ attr_accessor :request, :response
+
+ def initialize
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+ end
+
+ private
+ def method_missing(selector, *args)
+ controller = TestController.new
+ return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
+ super
+ end
+ end
+end