mirror of
https://github.com/github/rails.git
synced 2026-04-04 03:00:58 -04:00
Simplfy #view_paths implementation. ActionView templates get the exact object, not a dup. [Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8035 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Simplfy #view_paths implementation. ActionView templates get the exact object, not a dup. [Rick]
|
||||
|
||||
* Update tests for ActiveSupport's JSON escaping change. [rick]
|
||||
|
||||
* FormHelper's auto_index should use #to_param instead of #id_before_type_cast. Closes #9994 [mattly]
|
||||
|
||||
@@ -408,43 +408,39 @@ module ActionController #:nodoc:
|
||||
write_inheritable_attribute(:hidden_actions, hidden_actions | names.map(&:to_s))
|
||||
end
|
||||
|
||||
|
||||
@@view_paths = {}
|
||||
|
||||
# View load paths determine the bases from which template references can be made. So a call to
|
||||
# render("test/template") will be looked up in the view load paths array and the closest match will be
|
||||
# returned.
|
||||
def view_paths=(value)
|
||||
@@view_paths[name] = value
|
||||
end
|
||||
|
||||
# View load paths for controller.
|
||||
## View load paths determine the bases from which template references can be made. So a call to
|
||||
## render("test/template") will be looked up in the view load paths array and the closest match will be
|
||||
## returned.
|
||||
def view_paths
|
||||
if paths = @@view_paths[name]
|
||||
paths
|
||||
else
|
||||
if superclass.respond_to?(:view_paths)
|
||||
superclass.view_paths.dup.freeze
|
||||
else
|
||||
@@view_paths[name] = []
|
||||
end
|
||||
end
|
||||
@view_paths || superclass.view_paths
|
||||
end
|
||||
|
||||
|
||||
def view_paths=(value)
|
||||
@view_paths = value
|
||||
end
|
||||
|
||||
# Adds a view_path to the front of the view_paths array.
|
||||
# If the current class has no view paths, copy them from
|
||||
# the superclass
|
||||
#
|
||||
# ArticleController.prepend_view_path("views/default")
|
||||
# ArticleController.prepend_view_path(["views/default", "views/custom"])
|
||||
#
|
||||
def prepend_view_path(path)
|
||||
self.view_paths = view_paths.dup if view_paths.frozen?
|
||||
view_paths.unshift(path)
|
||||
@view_paths = superclass.view_paths.dup if @view_paths.nil?
|
||||
view_paths.unshift(*path)
|
||||
end
|
||||
|
||||
# Adds a view_path to the end of the view_paths array.
|
||||
# If the current class has no view paths, copy them from
|
||||
# the superclass
|
||||
#
|
||||
# ArticleController.append_view_path("views/default")
|
||||
# ArticleController.append_view_path(["views/default", "views/custom"])
|
||||
#
|
||||
def append_view_path(path)
|
||||
self.view_paths = view_paths.dup if view_paths.frozen?
|
||||
view_paths << path
|
||||
@view_paths = superclass.view_paths.dup if @view_paths.nil?
|
||||
view_paths.push(*path)
|
||||
end
|
||||
|
||||
# Replace sensitive paramater data from the request log.
|
||||
@@ -631,9 +627,16 @@ module ActionController #:nodoc:
|
||||
request.session_options && request.session_options[:disabled] != false
|
||||
end
|
||||
|
||||
|
||||
self.view_paths = []
|
||||
|
||||
# View load paths for controller.
|
||||
def view_paths
|
||||
self.class.view_paths
|
||||
(@template || self.class).view_paths
|
||||
end
|
||||
|
||||
def view_paths=(value)
|
||||
(@template || self.class).view_paths = value
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
@@ -4,6 +4,8 @@ class ViewLoadPathsTest < Test::Unit::TestCase
|
||||
|
||||
LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
||||
|
||||
ActionController::Base.view_paths = [ LOAD_PATH_ROOT ]
|
||||
|
||||
class TestController < ActionController::Base
|
||||
def self.controller_path() "test" end
|
||||
def rescue_action(e) raise end
|
||||
@@ -24,7 +26,7 @@ class ViewLoadPathsTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def setup
|
||||
TestController.view_paths = [ LOAD_PATH_ROOT ]
|
||||
TestController.view_paths = nil
|
||||
@controller = TestController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@@ -43,6 +45,22 @@ class ViewLoadPathsTest < Test::Unit::TestCase
|
||||
assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths
|
||||
end
|
||||
|
||||
def test_template_appends_path_correctly
|
||||
TestController.append_view_path 'foo'
|
||||
assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths
|
||||
|
||||
TestController.append_view_path(%w(bar baz))
|
||||
assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
|
||||
end
|
||||
|
||||
def test_template_prepends_path_correctly
|
||||
TestController.prepend_view_path 'baz'
|
||||
assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths
|
||||
|
||||
TestController.prepend_view_path(%w(foo bar))
|
||||
assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
|
||||
end
|
||||
|
||||
def test_view_paths
|
||||
get :hello_world
|
||||
assert_response :success
|
||||
@@ -85,11 +103,9 @@ class ViewLoadPathsTest < Test::Unit::TestCase
|
||||
assert_equal A.view_paths, B.view_paths
|
||||
assert_equal original_load_paths, C.view_paths
|
||||
|
||||
e = assert_raises(TypeError) { C.view_paths << 'c/path' }
|
||||
assert_equal "can't modify frozen array", e.message
|
||||
|
||||
C.view_paths = []
|
||||
assert_nothing_raised { C.view_paths << 'c/path' }
|
||||
assert_equal ['c/path'], C.view_paths
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user