Skip to content

Commit

Permalink
Add #prepend_view_path and #append_view_path instance methods on Acti…
Browse files Browse the repository at this point in the history
…onController::Base for consistency with the class methods. [rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8214 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Nov 26, 2007
1 parent 1af084e commit 87e506d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Add #prepend_view_path and #append_view_path instance methods on ActionController::Base for consistency with the class methods. [rick]

* Refactor sanitizer helpers into HTML classes and make it easy to swap them out with custom implementations. Closes #10129. [rick]

* Add deprecation for old subtemplate syntax for ActionMailer templates, use render :partial [rick]
Expand Down
25 changes: 22 additions & 3 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -430,7 +430,7 @@ def view_paths=(value)

# 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
# the superclass. This change will be visible for all future requests.
#
# ArticleController.prepend_view_path("views/default")
# ArticleController.prepend_view_path(["views/default", "views/custom"])
Expand All @@ -442,7 +442,7 @@ def prepend_view_path(path)

# 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
# the superclass. This change will be visible for all future requests.
#
# ArticleController.append_view_path("views/default")
# ArticleController.append_view_path(["views/default", "views/custom"])
Expand Down Expand Up @@ -636,7 +636,6 @@ def session_enabled?
request.session_options && request.session_options[:disabled] != false
end


self.view_paths = []

# View load paths for controller.
Expand All @@ -647,7 +646,27 @@ def view_paths
def view_paths=(value)
(@template || self.class).view_paths = value
end

# Adds a view_path to the front of the view_paths array.
# This change affects the current request only.
#
# self.prepend_view_path("views/default")
# self.prepend_view_path(["views/default", "views/custom"])
#
def prepend_view_path(path)
(@template || self.class).prepend_view_path(path)
end

# Adds a view_path to the end of the view_paths array.
# This change affects the current request only.
#
# self.append_view_path("views/default")
# self.append_view_path(["views/default", "views/custom"])
#
def append_view_path(path)
(@template || self.class).append_view_path(path)
end

protected
# Renders the content that will be returned to the browser as the response body.
#
Expand Down
22 changes: 21 additions & 1 deletion actionpack/lib/action_view/base.rb
Expand Up @@ -265,7 +265,7 @@ def self.register_template_handler(extension, klass)
end

def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
@view_paths = view_paths.respond_to?(:find) ? view_paths : [*view_paths].compact
@view_paths = view_paths.respond_to?(:find) ? view_paths.dup : [*view_paths].compact
@assigns = assigns_for_first_render
@assigns_added = nil
@controller = controller
Expand Down Expand Up @@ -472,6 +472,26 @@ def template_handler_preferences
TEMPLATE_HANDLER_PREFERENCES[template_format] || DEFAULT_TEMPLATE_HANDLER_PREFERENCE
end

# Adds a view_path to the front of the view_paths array.
# This change affects the current request only.
#
# @template.prepend_view_path("views/default")
# @template.prepend_view_path(["views/default", "views/custom"])
#
def prepend_view_path(path)
@view_paths.unshift(*path)
end

# Adds a view_path to the end of the view_paths array.
# This change affects the current request only.
#
# @template.append_view_path("views/default")
# @template.append_view_path(["views/default", "views/custom"])
#
def append_view_path(path)
@view_paths.push(*path)
end

private
def find_full_template_path(template_path, extension)
file_name = "#{template_path}.#{extension}"
Expand Down
28 changes: 26 additions & 2 deletions actionpack/test/controller/view_paths_test.rb
Expand Up @@ -47,22 +47,46 @@ def test_template_load_path_was_set_correctly
assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths
end

def test_template_appends_path_correctly
def test_controller_appends_view_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
def test_controller_prepends_view_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_template_appends_view_path_correctly
@controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller)
class_view_paths = TestController.view_paths

@controller.append_view_path 'foo'
assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths

@controller.append_view_path(%w(bar baz))
assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
assert_equal class_view_paths, TestController.view_paths
end

def test_template_prepends_view_path_correctly
@controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller)
class_view_paths = TestController.view_paths

@controller.prepend_view_path 'baz'
assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths

@controller.prepend_view_path(%w(foo bar))
assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
assert_equal class_view_paths, TestController.view_paths
end

def test_view_paths
get :hello_world
assert_response :success
Expand Down

0 comments on commit 87e506d

Please sign in to comment.