aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rwxr-xr-xactionpack/lib/action_controller/base.rb58
-rw-r--r--actionpack/lib/action_controller/layout.rb19
-rw-r--r--actionpack/lib/action_view/base.rb32
3 files changed, 75 insertions, 34 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 8a3cb3eec3..62dba3bc43 100755
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -277,11 +277,7 @@ module ActionController #:nodoc:
# Controls the default charset for all renders.
@@default_charset = "utf-8"
cattr_accessor :default_charset
-
- # Template root determines the base from which template references will be made. So a call to render("test/template")
- # will be converted to "#{template_root}/test/template.rhtml".
- class_inheritable_accessor :template_root
-
+
# The logger is used for generating information on the action run-time (including benchmarking) if available.
# Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
cattr_accessor :logger
@@ -357,7 +353,41 @@ module ActionController #:nodoc:
def hide_action(*names)
write_inheritable_attribute(:hidden_actions, hidden_actions | names.collect { |n| n.to_s })
end
-
+
+ # Deprecated. Use view_paths instead.
+ def template_root=(path)
+ view_paths.unshift(path)
+ end
+ deprecate :template_root= => :view_paths
+
+ # Deprecated. Use view_paths instead.
+ def template_root
+ view_paths.first
+ end
+ deprecate :template_root => :view_paths
+
+ @@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.
+ 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
+ end
+
# Replace sensitive paramater data from the request log.
# Filters paramaters that have any of the arguments as a substring.
# Looks in all subhashes of the param hash for keys to filter.
@@ -534,10 +564,15 @@ module ActionController #:nodoc:
def controller_path
self.class.controller_path
end
-
+
def session_enabled?
request.session_options[:disabled] != false
end
+
+ # View load paths for controller.
+ def view_paths
+ self.class.view_paths
+ end
protected
# Renders the content that will be returned to the browser as the response body.
@@ -1030,14 +1065,10 @@ module ActionController #:nodoc:
end
end
- def self.view_root
- @view_root ||= template_root
- end
-
def initialize_template_class(response)
raise "You must assign a template class through ActionController.template_class= before processing a request" unless @@template_class
- response.template = self.class.view_class.new(self.class.view_root, {}, self)
+ response.template = self.class.view_class.new(view_paths, {}, self)
response.redirected_to = nil
@performed_render = @performed_redirect = false
end
@@ -1057,7 +1088,6 @@ module ActionController #:nodoc:
assign_deprecated_shortcuts(request, response)
end
-
# TODO: assigns cookies headers params request response template
DEPRECATED_INSTANCE_VARIABLES = %w(cookies flash headers params request response session)
@@ -1151,7 +1181,7 @@ module ActionController #:nodoc:
end
def add_class_variables_to_assigns
- %w(template_root logger template_class ignore_missing_templates).each do |cvar|
+ %w(view_paths logger template_class ignore_missing_templates).each do |cvar|
@assigns[cvar] = self.send(cvar)
end
end
diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb
index 82c7180a52..6eaf8bb31f 100644
--- a/actionpack/lib/action_controller/layout.rb
+++ b/actionpack/lib/action_controller/layout.rb
@@ -179,16 +179,18 @@ module ActionController #:nodoc:
def default_layout #:nodoc:
@default_layout ||= read_inheritable_attribute("layout")
end
-
+
+ def layout_list #:nodoc:
+ view_paths.collect do |path|
+ Dir["#{path}/layouts/**/*"]
+ end.flatten
+ end
+
private
def inherited_with_layout(child)
inherited_without_layout(child)
layout_match = child.name.underscore.sub(/_controller$/, '').sub(/^controllers\//, '')
- child.layout(layout_match) unless layout_list.grep(%r{layouts/#{layout_match}\.[a-z][0-9a-z]*$}).empty?
- end
-
- def layout_list
- Dir.glob("#{template_root}/layouts/**/*")
+ child.layout(layout_match) unless child.layout_list.grep(%r{layouts/#{layout_match}\.[a-z][0-9a-z]*$}).empty?
end
def add_layout_conditions(conditions)
@@ -305,7 +307,10 @@ module ActionController #:nodoc:
# Does a layout directory for this class exist?
# we cache this info in a class level hash
def layout_directory?(layout_name)
- template_path = File.join(self.class.view_root, 'layouts', layout_name)
+ view_paths.find do |path|
+ File.file?(File.join(path, 'layouts', layout_name))
+ end
+ template_path ||= File.join(view_paths.first, 'layouts', layout_name)
dirname = File.dirname(template_path)
self.class.send(:layout_directory_exists_cache)[dirname]
end
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index 303a946186..d8bb6add34 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -232,15 +232,16 @@ module ActionView #:nodoc:
@@template_handlers[extension] = klass
end
- def initialize(base_path = nil, assigns_for_first_render = {}, controller = nil)#:nodoc:
- @base_path, @assigns = base_path, assigns_for_first_render
+ def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
+ @view_paths = [*view_paths].compact
+ @assigns = assigns_for_first_render
@assigns_added = nil
@controller = controller
@logger = controller && controller.logger
end
# Renders the template present at <tt>template_path</tt>. If <tt>use_full_path</tt> is set to true,
- # it's relative to the template_root, otherwise it's absolute. The hash in <tt>local_assigns</tt>
+ # it's relative to the view_paths array, otherwise it's absolute. The hash in <tt>local_assigns</tt>
# is made available as local variables.
def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc:
@first_render ||= template_path
@@ -268,12 +269,12 @@ module ActionView #:nodoc:
e.sub_template_of(template_file_name)
raise e
else
- raise TemplateError.new(@base_path, template_file_name, @assigns, template_source, e)
+ raise TemplateError.new(find_base_path_for(template_file_name), template_file_name, @assigns, template_source, e)
end
end
end
-
- # Renders the template present at <tt>template_path</tt> (relative to the template_root).
+
+ # Renders the template present at <tt>template_path</tt> (relative to the view_paths array).
# The hash in <tt>local_assigns</tt> is made available as local variables.
def render(options = {}, old_local_assigns = {}, &block) #:nodoc:
if options.is_a?(String)
@@ -358,12 +359,11 @@ module ActionView #:nodoc:
def file_exists?(template_path)#:nodoc:
template_file_name, template_file_extension = path_and_extension(template_path)
-
if template_file_extension
template_exists?(template_file_name, template_file_extension)
else
cached_template_extension(template_path) ||
- %w(erb builder javascript delegate).any? do |template_type|
+ %w(erb builder javascript delegate).any? do |template_type|
send("#{template_type}_template_exists?", template_path)
end
end
@@ -376,7 +376,9 @@ module ActionView #:nodoc:
private
def full_template_path(template_path, extension)
- "#{@base_path}/#{template_path}.#{extension}"
+ file_name = "#{template_path}.#{extension}"
+ base_path = find_base_path_for(file_name)
+ "#{base_path}/#{file_name}"
end
def template_exists?(template_path, extension)
@@ -392,7 +394,11 @@ module ActionView #:nodoc:
def cached_template_extension(template_path)
@@cache_template_extensions && @@cached_template_extension[template_path]
end
-
+
+ def find_base_path_for(template_file_name)
+ @view_paths.find { |p| File.file?(File.join(p, template_file_name)) }
+ end
+
def find_template_extension_for(template_path)
if match = delegate_template_exists?(template_path)
match.first.to_sym
@@ -400,7 +406,7 @@ module ActionView #:nodoc:
elsif builder_template_exists?(template_path): :rxml
elsif javascript_template_exists?(template_path): :rjs
else
- raise ActionViewError, "No rhtml, rxml, rjs or delegate template found for #{template_path} in #{@base_path}"
+ raise ActionViewError, "No rhtml, rxml, rjs or delegate template found for #{template_path} in #{@view_paths.inspect}"
end
end
@@ -536,7 +542,7 @@ module ActionView #:nodoc:
logger.debug "Backtrace: #{e.backtrace.join("\n")}"
end
- raise TemplateError.new(@base_path, file_name || template, @assigns, template, e)
+ raise TemplateError.new(lookup_template_base_path_for(file_name || template), file_name || template, @assigns, template, e)
end
@@compile_time[render_symbol] = Time.now
@@ -545,4 +551,4 @@ module ActionView #:nodoc:
end
end
-require 'action_view/template_error'
+require 'action_view/template_error' \ No newline at end of file