lib/action_view/base.rb in actionpack-1.13.3 vs lib/action_view/base.rb in actionpack-1.13.4
- old
+ new
@@ -146,11 +146,11 @@
# page.remove "person-#{@person.id}"
# page.visual_effect :highlight, 'user-list'
#
# This refreshes the sidebar, removes a person element and highlights the user list.
#
- # See the ActionView::Helpers::PrototypeHelper::JavaScriptGenerator documentation for more details.
+ # See the ActionView::Helpers::PrototypeHelper::GeneratorMethods documentation for more details.
class Base
include ERB::Util
attr_reader :first_render
attr_accessor :base_path, :assigns, :template_extension
@@ -158,11 +158,11 @@
attr_reader :logger, :response, :headers
attr_internal *ActionController::Base::DEPRECATED_INSTANCE_VARIABLES
# Specify trim mode for the ERB compiler. Defaults to '-'.
- # See ERB documentation for suitable values.
+ # See ERb documentation for suitable values.
@@erb_trim_mode = '-'
cattr_accessor :erb_trim_mode
# Specify whether file modification times should be checked to see if a template needs recompilation
@@cache_template_loading = false
@@ -189,21 +189,21 @@
module CompiledTemplates #:nodoc:
# holds compiled template code
end
include CompiledTemplates
- # maps inline templates to their method names
+ # Maps inline templates to their method names
@@method_names = {}
- # map method names to their compile time
+ # Map method names to their compile time
@@compile_time = {}
- # map method names to the names passed in local assigns so far
+ # Map method names to the names passed in local assigns so far
@@template_args = {}
- # count the number of inline templates
+ # Count the number of inline templates
@@inline_template_count = 0
- # maps template paths without extension to their file extension returned by pick_template_extension.
- # if for a given path, path.ext1 and path.ext2 exist on the file system, the order of extensions
- # used by pick_template_extension determines whether ext1 or ext2 will be stored
+ # Maps template paths without extension to their file extension returned by pick_template_extension.
+ # If for a given path, path.ext1 and path.ext2 exist on the file system, the order of extensions
+ # used by pick_template_extension determines whether ext1 or ext2 will be stored.
@@cached_template_extension = {}
class ObjectWrapper < Struct.new(:value) #:nodoc:
end
@@ -303,11 +303,10 @@
end
# Render the provided template with the given local assigns. If the template has not been rendered with the provided
# local assigns yet, or if the template has been updated on disk, then the template will be compiled to a method.
#
-
# Either, but not both, of template and file_path may be nil. If file_path is given, the template
# will only be read if it has to be compiled.
#
def compile_and_render_template(extension, template = nil, file_path = nil, local_assigns = {}) #:nodoc:
# convert string keys to symbols if requested
@@ -369,14 +368,16 @@
def file_public?(template_path)#:nodoc:
template_path.split('/').last[0,1] != '_'
end
private
+ # Builds a string holding the full path of the template including extension
def full_template_path(template_path, extension)
"#{@base_path}/#{template_path}.#{extension}"
end
+ # Asserts the existence of a template.
def template_exists?(template_path, extension)
file_path = full_template_path(template_path, extension)
@@method_names.has_key?(file_path) || FileTest.exists?(file_path)
end
@@ -387,10 +388,11 @@
def cached_template_extension(template_path)
@@cache_template_extensions && @@cached_template_extension[template_path]
end
+ # Determines the template's file extension, such as rhtml, rxml, or rjs.
def find_template_extension_for(template_path)
if match = delegate_template_exists?(template_path)
match.first.to_sym
elsif erb_template_exists?(template_path): :rhtml
elsif builder_template_exists?(template_path): :rxml
@@ -403,10 +405,11 @@
# This method reads a template file.
def read_template_file(template_path, extension)
File.read(template_path)
end
+ # Evaluate the local assigns and pushes them to the view.
def evaluate_assigns
unless @assigns_added
assign_variables_from_controller
@assigns_added = true
end
@@ -414,10 +417,11 @@
def delegate_render(handler, template, local_assigns)
handler.new(self).render(template, local_assigns)
end
+ # Assigns instance variables from the controller to the view.
def assign_variables_from_controller
@assigns.each { |key, value| instance_variable_set("@#{key}", value) }
end
@@ -425,14 +429,14 @@
def supports_local_assigns?(render_symbol, local_assigns)
local_assigns.empty? ||
((args = @@template_args[render_symbol]) && local_assigns.all? { |k,_| args.has_key?(k) })
end
- # Check whether compilation is necessary.
- # Compile if the inline template or file has not been compiled yet.
- # Or if local_assigns has a new key, which isn't supported by the compiled code yet.
- # Or if the file has changed on disk and checking file mods hasn't been disabled.
+ # Method to check whether template compilation is necessary.
+ # The template will be compiled if the inline template or file has not been compiled yet,
+ # if local_assigns has a new key, which isn't supported by the compiled code yet,
+ # or if the file has changed on disk and checking file mods hasn't been disabled.
def compile_template?(template, file_name, local_assigns)
method_key = file_name || template
render_symbol = @@method_names[method_key]
if @@compile_time[render_symbol] && supports_local_assigns?(render_symbol, local_assigns)
@@ -443,18 +447,19 @@
else
true
end
end
- # Create source code for given template
+ # Method to create the source code for a given template.
def create_template_source(extension, template, render_symbol, locals)
if template_requires_setup?(extension)
body = case extension.to_sym
when :rxml
"controller.response.content_type ||= 'application/xml'\n" +
- "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
- template
+ "xml ||= Builder::XmlMarkup.new(:indent => 2)\n" +
+ template +
+ "\nxml.target!\n"
when :rjs
"controller.response.content_type ||= 'text/javascript'\n" +
"update_page do |page|\n#{template}\nend"
end
else
@@ -471,15 +476,15 @@
end
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
end
- def template_requires_setup?(extension)
+ def template_requires_setup?(extension) #:nodoc:
templates_requiring_setup.include? extension.to_s
end
- def templates_requiring_setup
+ def templates_requiring_setup #:nodoc:
%w(rxml rjs)
end
def assign_method_name(extension, template, file_name)
method_key = file_name || template
@@ -499,9 +504,10 @@
else
(@@inline_template_count += 1).to_s
end
end
+ # Compile and evaluate the template's code
def compile_template(extension, template, file_name, local_assigns)
render_symbol = assign_method_name(extension, template, file_name)
render_source = create_template_source(extension, template, render_symbol, local_assigns.keys)
line_offset = @@template_args[render_symbol].size