diff options
author | lifo <lifo@null.lan> | 2009-04-17 14:06:26 +0100 |
---|---|---|
committer | lifo <lifo@null.lan> | 2009-04-17 14:06:26 +0100 |
commit | 20401783cf26f903d7020cb7136b1e78e60e71ea (patch) | |
tree | 5bb029802ade6dda33e051adf74915dc0a8d1fe5 /actionpack/lib/action_view | |
parent | f99e9f627b6e4ab7fe72bc759426312ec0c7a2cd (diff) | |
parent | abb899c54e8777428b7a607774370ba29a5573bd (diff) | |
download | rails-20401783cf26f903d7020cb7136b1e78e60e71ea.tar.gz rails-20401783cf26f903d7020cb7136b1e78e60e71ea.tar.bz2 rails-20401783cf26f903d7020cb7136b1e78e60e71ea.zip |
Merge commit 'mainstream/master'
Conflicts:
actionpack/lib/action_controller/base.rb
railties/guides/source/caching_with_rails.textile
Diffstat (limited to 'actionpack/lib/action_view')
22 files changed, 633 insertions, 604 deletions
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 9c0134e7f7..efed19a21d 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -3,10 +3,11 @@ module ActionView #:nodoc: end class MissingTemplate < ActionViewError #:nodoc: - attr_reader :path + attr_reader :path, :action_name def initialize(paths, path, template_format = nil) @path = path + @action_name = path.split("/").last.split(".")[0...-1].join(".") full_template_path = path.include?('.') ? path : "#{path}.erb" display_paths = paths.compact.join(":") template_type = (path =~ /layouts/i) ? 'layout' : 'template' @@ -160,14 +161,13 @@ module ActionView #:nodoc: # # See the ActionView::Helpers::PrototypeHelper::GeneratorMethods documentation for more details. class Base - include Helpers, Partials, ::ERB::Util + include Helpers, Rendering, Partials, ::ERB::Util + extend ActiveSupport::Memoizable - attr_accessor :base_path, :assigns, :template_extension + attr_accessor :base_path, :assigns, :template_extension, :formats attr_accessor :controller - attr_writer :template_format - attr_accessor :output_buffer class << self @@ -193,9 +193,13 @@ module ActionView #:nodoc: attr_internal :request + delegate :controller_path, :to => :controller, :allow_nil => true + delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers, :flash, :logger, :action_name, :controller_name, :to => :controller + delegate :find_by_parts, :to => :view_paths + module CompiledTemplates #:nodoc: # holds compiled template code end @@ -218,7 +222,8 @@ module ActionView #:nodoc: end end - def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc: + def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil, formats = nil)#:nodoc: + @formats = formats || [:html] @assigns = assigns_for_first_render @assigns_added = nil @controller = controller @@ -233,56 +238,6 @@ module ActionView #:nodoc: def view_paths=(paths) @view_paths = self.class.process_view_paths(paths) - # we might be using ReloadableTemplates, so we need to let them know this a new request - @view_paths.load! - end - - # Returns the result of a render that's dictated by the options hash. The primary options are: - # - # * <tt>:partial</tt> - See ActionView::Partials. - # * <tt>:update</tt> - Calls update_page with the block given. - # * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those. - # * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller. - # * <tt>:text</tt> - Renders the text passed in out. - # - # If no options hash is passed or :update specified, the default is to render a partial and use the second parameter - # as the locals hash. - def render(options = {}, local_assigns = {}, &block) #:nodoc: - local_assigns ||= {} - - case options - when Hash - options = options.reverse_merge(:locals => {}) - if options[:layout] - _render_with_layout(options, local_assigns, &block) - elsif options[:file] - template = self.view_paths.find_template(options[:file], template_format) - template.render_template(self, options[:locals]) - elsif options[:partial] - render_partial(options) - elsif options[:inline] - InlineTemplate.new(options[:inline], options[:type]).render(self, options[:locals]) - elsif options[:text] - options[:text] - end - when :update - update_page(&block) - else - render_partial(:partial => options, :locals => local_assigns) - end - end - - # The format to be used when choosing between multiple templates with - # the same name but differing formats. See +Request#template_format+ - # for more details. - def template_format - if defined? @template_format - @template_format - elsif controller && controller.respond_to?(:request) - @template_format = controller.request.template_format.to_sym - else - @template_format = :html - end end # Access the current template being rendered. @@ -332,32 +287,5 @@ module ActionView #:nodoc: controller.response.content_type ||= content_type end end - - def _render_with_layout(options, local_assigns, &block) #:nodoc: - partial_layout = options.delete(:layout) - - if block_given? - begin - @_proc_for_layout = block - concat(render(options.merge(:partial => partial_layout))) - ensure - @_proc_for_layout = nil - end - else - begin - original_content_for_layout = @content_for_layout if defined?(@content_for_layout) - @content_for_layout = render(options) - - if (options[:inline] || options[:file] || options[:text]) - @cached_content_for_layout = @content_for_layout - render(:file => partial_layout, :locals => local_assigns) - else - render(options.merge(:partial => partial_layout)) - end - ensure - @content_for_layout = original_content_for_layout - end - end - end end end diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index 541899ea6a..7c0dfdab10 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -194,7 +194,7 @@ module ActionView options[:header_message] else object_name = options[:object_name].to_s.gsub('_', ' ') - object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1) + object_name = I18n.t(options[:object_name].to_s, :default => object_name, :scope => [:activerecord, :models], :count => 1) locale.t :header, :count => count, :model => object_name end message = options.include?(:message) ? options[:message] : locale.t(:body) diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 91ef72e54b..6bad11e354 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -987,13 +987,13 @@ module ActionView end def render(*options_for_render) - old_format = @context && @context.template_format - @context.template_format = :html if @context + old_formats = @context && @context.formats + @context.formats = [:html] if @context Hash === options_for_render.first ? @context.render(*options_for_render) : options_for_render.first.to_s ensure - @context.template_format = old_format if @context + @context.formats = old_formats if @context end def javascript_object_for(object) diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb index a0a2f96886..1d0279889c 100644 --- a/actionpack/lib/action_view/paths.rb +++ b/actionpack/lib/action_view/paths.rb @@ -2,16 +2,13 @@ module ActionView #:nodoc: class PathSet < Array #:nodoc: def self.type_cast(obj) if obj.is_a?(String) - if Base.cache_template_loading? - Template::EagerPath.new(obj.to_s) - else - ReloadableTemplate::ReloadablePath.new(obj.to_s) - end + cache = !Object.const_defined?(:Rails) || Rails.configuration.cache_classes + Template::FileSystemPath.new(obj, :cache => cache) else obj end end - + def initialize(*args) super(*args).map! { |obj| self.class.type_cast(obj) } end @@ -35,9 +32,29 @@ module ActionView #:nodoc: def unshift(*objs) super(*objs.map { |obj| self.class.type_cast(obj) }) end + + def find_by_parts(path, extension = nil, prefix = nil, partial = false) + template_path = path.sub(/^\//, '') + + each do |load_path| + if template = load_path.find_by_parts(template_path, extension, prefix, partial) + return template + end + end + + Template.new(path, self) + rescue ActionView::MissingTemplate => e + extension ||= [] + raise ActionView::MissingTemplate.new(self, "#{prefix}/#{path}.{#{extension.join(",")}}") + end - def load! - each(&:load!) + def find_by_parts?(path, extension = nil, prefix = nil, partial = false) + template_path = path.sub(/^\//, '') + + each do |load_path| + return true if template = load_path.find_by_parts(template_path, extension, prefix, partial) + end + false end def find_template(original_template_path, format = nil, html_fallback = true) @@ -45,13 +62,7 @@ module ActionView #:nodoc: template_path = original_template_path.sub(/^\//, '') each do |load_path| - if format && (template = load_path["#{template_path}.#{I18n.locale}.#{format}"]) - return template - elsif format && (template = load_path["#{template_path}.#{format}"]) - return template - elsif template = load_path["#{template_path}.#{I18n.locale}"] - return template - elsif template = load_path[template_path] + if template = load_path.find_by_parts(template_path, format) return template # Try to find html version if the format is javascript elsif format == :js && html_fallback && template = load_path["#{template_path}.#{I18n.locale}.html"] @@ -61,7 +72,7 @@ module ActionView #:nodoc: end end - return Template.new(original_template_path) if File.file?(original_template_path) + return Template.new(original_template_path, original_template_path.to_s =~ /\A\// ? "" : ".") if File.file?(original_template_path) raise MissingTemplate.new(self, original_template_path, format) end diff --git a/actionpack/lib/action_view/reloadable_template.rb b/actionpack/lib/action_view/reloadable_template.rb deleted file mode 100644 index 5ef833d75c..0000000000 --- a/actionpack/lib/action_view/reloadable_template.rb +++ /dev/null @@ -1,117 +0,0 @@ -module ActionView #:nodoc: - class ReloadableTemplate < Template - - class TemplateDeleted < ActionView::ActionViewError - end - - class ReloadablePath < Template::Path - - def initialize(path) - super - @paths = {} - new_request! - end - - def new_request! - @disk_cache = {} - end - alias_method :load!, :new_request! - - def [](path) - if found_template = @paths[path] - begin - found_template.reset_cache_if_stale! - rescue TemplateDeleted - unregister_template(found_template) - self[path] - end - else - load_all_templates_from_dir(templates_dir_from_path(path)) - # don't ever hand out a template without running a stale check - (new_template = @paths[path]) && new_template.reset_cache_if_stale! - end - end - - private - def register_template_from_file(template_full_file_path) - if !@paths[relative_path = relative_path_for_template_file(template_full_file_path)] && File.file?(template_full_file_path) - register_template(ReloadableTemplate.new(relative_path, self)) - end - end - - def register_template(template) - template.accessible_paths.each do |path| - @paths[path] = template - end - end - - # remove (probably deleted) template from cache - def unregister_template(template) - template.accessible_paths.each do |template_path| - @paths.delete(template_path) if @paths[template_path] == template - end - # fill in any newly created gaps - @paths.values.uniq.each do |template| - template.accessible_paths.each {|path| @paths[path] ||= template} - end - end - - # load all templates from the directory of the requested template - def load_all_templates_from_dir(dir) - # hit disk only once per template-dir/request - @disk_cache[dir] ||= template_files_from_dir(dir).each {|template_file| register_template_from_file(template_file)} - end - - def templates_dir_from_path(path) - dirname = File.dirname(path) - File.join(@path, dirname == '.' ? '' : dirname) - end - - # get all the template filenames from the dir - def template_files_from_dir(dir) - Dir.glob(File.join(dir, '*')) - end - end - - module Unfreezable - def freeze; self; end - end - - def initialize(*args) - super - - # we don't ever want to get frozen - extend Unfreezable - end - - def mtime - File.mtime(filename) - end - - attr_accessor :previously_last_modified - - def stale? - previously_last_modified.nil? || previously_last_modified < mtime - rescue Errno::ENOENT => e - undef_my_compiled_methods! - raise TemplateDeleted - end - - def reset_cache_if_stale! - if stale? - flush_cache 'source', 'compiled_source' - undef_my_compiled_methods! - @previously_last_modified = mtime - end - self - end - - # remove any compiled methods that look like they might belong to me - def undef_my_compiled_methods! - ActionView::Base::CompiledTemplates.public_instance_methods.grep(/#{Regexp.escape(method_name_without_locals)}(?:_locals_)?/).each do |m| - ActionView::Base::CompiledTemplates.send(:remove_method, m) - end - end - - end -end diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/render/partials.rb index 9e5e0f786e..e337dcb63b 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -172,68 +172,156 @@ module ActionView module Partials extend ActiveSupport::Memoizable + def _render_partial(options = {}) #:nodoc: + options[:locals] ||= {} + + case path = partial = options[:partial] + when *_array_like_objects + return _render_partial_collection(partial, options) + else + if partial.is_a?(ActionView::Helpers::FormBuilder) + path = partial.class.to_s.demodulize.underscore.sub(/_builder$/, '') + options[:locals].merge!(path.to_sym => partial) + elsif !partial.is_a?(String) + options[:object] = object = partial + path = ActionController::RecordIdentifier.partial_path(object, controller_path) + end + _, _, prefix, object = parts = partial_parts(path, options) + template = find_by_parts(*parts) + _render_partial_object(template, options, (object unless object == true)) + end + end + private - def render_partial(options = {}) #:nodoc: - local_assigns = options[:locals] || {} + def partial_parts(name, options) + segments = name.split("/") + parts = segments.pop.split(".") - case partial_path = options[:partial] - when String, Symbol, NilClass - if options.has_key?(:collection) - render_partial_collection(options) - else - _pick_partial_template(partial_path).render_partial(self, options[:object], local_assigns) + case parts.size + when 1 + parts + when 2, 3 + extension = parts.delete_at(1).to_sym + if formats.include?(extension) + self.formats.replace [extension] end - when ActionView::Helpers::FormBuilder - builder_partial_path = partial_path.class.to_s.demodulize.underscore.sub(/_builder$/, '') - local_assigns.merge!(builder_partial_path.to_sym => partial_path) - render_partial(:partial => builder_partial_path, :object => options[:object], :locals => local_assigns) + parts.pop if parts.size == 2 + end + + path = parts.join(".") + prefix = segments[0..-1].join("/") + prefix = prefix.blank? ? controller_path : prefix + parts = [path, formats, prefix] + parts.push options[:object] || true + end + + def _render_partial_with_block(layout, block, options) + @_proc_for_layout = block + concat(_render_partial(options.merge(:partial => layout))) + ensure + @_proc_for_layout = nil + end + + def _render_partial_with_layout(layout, options) + if layout + prefix = controller && !layout.include?("/") ? controller.controller_path : nil + layout = find_by_parts(layout, formats, prefix, true) + end + content = _render_partial(options) + return _render_content_with_layout(content, layout, options[:locals]) + end + + def _deprecated_ivar_assign(template) + if respond_to?(:controller) + ivar = :"@#{template.variable_name}" + object = + if controller.instance_variable_defined?(ivar) + ActiveSupport::Deprecation::DeprecatedObjectProxy.new( + controller.instance_variable_get(ivar), + "#{ivar} will no longer be implicitly assigned to #{template.variable_name}") + end + end + end + + def _render_partial_with_block(layout, block, options) + @_proc_for_layout = block + concat(_render_partial(options.merge(:partial => layout))) + ensure + @_proc_for_layout = nil + end + + def _render_partial_with_layout(layout, options) + if layout + prefix = controller && !layout.include?("/") ? controller.controller_path : nil + layout = find_by_parts(layout, formats, prefix, true) + end + content = _render_partial(options) + return _render_content_with_layout(content, layout, options[:locals]) + end + + def _deprecated_ivar_assign(template) + if respond_to?(:controller) + ivar = :"@#{template.variable_name}" + object = + if controller.instance_variable_defined?(ivar) + ActiveSupport::Deprecation::DeprecatedObjectProxy.new( + controller.instance_variable_get(ivar), + "#{ivar} will no longer be implicitly assigned to #{template.variable_name}") + end + end + end + + def _array_like_objects + array_like = [Array] + if defined?(ActiveRecord) + array_like.push(ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope) + end + array_like + end + + def _render_partial_object(template, options, object = nil) + if options.key?(:collection) + _render_partial_collection(options.delete(:collection), options, template) else - if Array === partial_path || - (defined?(ActiveRecord) && - (ActiveRecord::Associations::AssociationCollection === partial_path || - ActiveRecord::NamedScope::Scope === partial_path)) - render_partial_collection(options.except(:partial).merge(:collection => partial_path)) - else - object = partial_path - render_partial( - :partial => ActionController::RecordIdentifier.partial_path(object, controller.class.controller_path), - :object => object, - :locals => local_assigns - ) - end + locals = (options[:locals] ||= {}) + object ||= locals[:object] || locals[template.variable_name] + + _set_locals(object, locals, template, options) + _render_template(template, locals) end end - def render_partial_collection(options = {}) #:nodoc: - return nil if options[:collection].blank? + def _set_locals(object, locals, template, options) + object ||= _deprecated_ivar_assign(template) + locals[:object] = locals[template.variable_name] = object + locals[options[:as]] = object if options[:as] + end + + def _render_partial_collection(collection, options = {}, passed_template = nil) #:nodoc: + return nil if collection.blank? + + spacer = options[:spacer_template] ? _render_partial(:partial => options[:spacer_template]) : '' - partial = options[:partial] - spacer = options[:spacer_template] ? render(:partial => options[:spacer_template]) : '' - local_assigns = options[:locals] ? options[:locals].clone : {} - as = options[:as] + locals = (options[:locals] ||= {}) + index, @_partial_path = 0, nil + collection.map do |object| + template = passed_template || begin + _partial_path = + ActionController::RecordIdentifier.partial_path(object, controller_path) + template = _pick_partial_template(_partial_path) + end - index = 0 - options[:collection].map do |object| - _partial_path ||= partial || - ActionController::RecordIdentifier.partial_path(object, controller.class.controller_path) - template = _pick_partial_template(_partial_path) - local_assigns[template.counter_name] = index - result = template.render_partial(self, object, local_assigns.dup, as) + _set_locals(object, locals, template, options) + locals[template.counter_name] = index + index += 1 - result + _render_template(template, locals) end.join(spacer) end def _pick_partial_template(partial_path) #:nodoc: - if partial_path.include?('/') - path = File.join(File.dirname(partial_path), "_#{File.basename(partial_path)}") - elsif controller - path = "#{controller.class.controller_path}/_#{partial_path}" - else - path = "_#{partial_path}" - end - - self.view_paths.find_template(path, self.template_format) + prefix = controller_path unless partial_path.include?('/') + find_by_parts(partial_path, formats, prefix, true) end memoize :_pick_partial_template end diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb new file mode 100644 index 0000000000..a9b2acecd5 --- /dev/null +++ b/actionpack/lib/action_view/render/rendering.rb @@ -0,0 +1,114 @@ +module ActionView + module Rendering + # Returns the result of a render that's dictated by the options hash. The primary options are: + # + # * <tt>:partial</tt> - See ActionView::Partials. + # * <tt>:update</tt> - Calls update_page with the block given. + # * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those. + # * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller. + # * <tt>:text</tt> - Renders the text passed in out. + # + # If no options hash is passed or :update specified, the default is to render a partial and use the second parameter + # as the locals hash. + def render(options = {}, local_assigns = {}, &block) #:nodoc: + local_assigns ||= {} + + @exempt_from_layout = true + + case options + when Hash + options[:locals] ||= {} + layout = options[:layout] + + return _render_partial_with_layout(layout, options) if options.key?(:partial) + return _render_partial_with_block(layout, block, options) if block_given? + + layout = find_by_parts(layout, formats) if layout + + if file = options[:file] + template = find_by_parts(file, formats) + _render_template_with_layout(template, layout, :locals => options[:locals]) + elsif inline = options[:inline] + _render_inline(inline, layout, options) + elsif text = options[:text] + _render_text(text, layout, options) + end + when :update + update_page(&block) + when String, NilClass + _render_partial(:partial => options, :locals => local_assigns) + end + end + + def _render_content_with_layout(content, layout, locals) + return content unless layout + + locals ||= {} + + if controller && layout + response.layout = layout.path_without_format_and_extension if controller.respond_to?(:response) + logger.info("Rendering template within #{layout.path_without_format_and_extension}") if logger + end + + begin + original_content_for_layout = @content_for_layout if defined?(@content_for_layout) + @content_for_layout = content + + @cached_content_for_layout = @content_for_layout + _render_template(layout, locals) + ensure + @content_for_layout = original_content_for_layout + end + end + + def _render_template(template, local_assigns = {}) + with_template(template) do + _evaluate_assigns_and_ivars + _set_controller_content_type(template.mime_type) if template.respond_to?(:mime_type) + + template.render(self, local_assigns) do |*names| + if !instance_variable_defined?(:"@content_for_#{names.first}") && + instance_variable_defined?(:@_proc_for_layout) && (proc = @_proc_for_layout) + capture(*names, &proc) + elsif instance_variable_defined?(ivar = :"@content_for_#{names.first || :layout}") + instance_variable_get(ivar) + end + end + end + rescue Exception => e + raise e if template.is_a?(InlineTemplate) || !template.filename + if TemplateError === e + e.sub_template_of(template) + raise e + else + raise TemplateError.new(template, assigns, e) + end + end + + def _render_inline(inline, layout, options) + content = _render_template(InlineTemplate.new(options[:inline], options[:type]), options[:locals] || {}) + layout ? _render_content_with_layout(content, layout, options[:locals]) : content + end + + def _render_text(text, layout, options) + layout ? _render_content_with_layout(text, layout, options[:locals]) : text + end + + def _render_template_with_layout(template, layout = nil, options = {}, partial = false) + if controller && logger + logger.info("Rendering #{template.path_without_extension}" + + (options[:status] ? " (#{options[:status]})" : '')) + end + + content = if partial + object = partial unless partial == true + _render_partial_object(template, options, object) + else + _render_template(template, options[:locals] || {}) + end + + return content unless layout && !template.exempt_from_layout? + _render_content_with_layout(content, layout, options[:locals] || {}) + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_view/renderable_partial.rb b/actionpack/lib/action_view/renderable_partial.rb deleted file mode 100644 index 3ea836fa25..0000000000 --- a/actionpack/lib/action_view/renderable_partial.rb +++ /dev/null @@ -1,47 +0,0 @@ -module ActionView - # NOTE: The template that this mixin is being included into is frozen - # so you cannot set or modify any instance variables - module RenderablePartial #:nodoc: - extend ActiveSupport::Memoizable - - def variable_name - name.sub(/\A_/, '').to_sym - end - memoize :variable_name - - def counter_name - "#{variable_name}_counter".to_sym - end - memoize :counter_name - - def render(view, local_assigns = {}) - if defined? ActionController - ActionController::Base.benchmark("Rendered #{path_without_format_and_extension}", Logger::DEBUG, false) do - super - end - else - super - end - end - - def render_partial(view, object = nil, local_assigns = {}, as = nil) - object ||= local_assigns[:object] || local_assigns[variable_name] - - if object.nil? && view.respond_to?(:controller) - ivar = :"@#{variable_name}" - object = - if view.controller.instance_variable_defined?(ivar) - ActiveSupport::Deprecation::DeprecatedObjectProxy.new( - view.controller.instance_variable_get(ivar), - "#{ivar} will no longer be implicitly assigned to #{variable_name}") - end - end - - # Ensure correct object is reassigned to other accessors - local_assigns[:object] = local_assigns[variable_name] = object - local_assigns[as] = object if as - - render_template(view, local_assigns) - end - end -end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb deleted file mode 100644 index a974f2652b..0000000000 --- a/actionpack/lib/action_view/template.rb +++ /dev/null @@ -1,246 +0,0 @@ -module ActionView #:nodoc: - class Template - class Path - attr_reader :path, :paths - delegate :hash, :inspect, :to => :path - - def initialize(path) - raise ArgumentError, "path already is a Path class" if path.is_a?(Path) - @path = (path.ends_with?(File::SEPARATOR) ? path.to(-2) : path).freeze - end - - def to_s - if defined?(RAILS_ROOT) - path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') - else - path.to_s - end - end - - def to_str - path.to_str - end - - def ==(path) - to_str == path.to_str - end - - def eql?(path) - to_str == path.to_str - end - - # Returns a ActionView::Template object for the given path string. The - # input path should be relative to the view path directory, - # +hello/index.html.erb+. This method also has a special exception to - # match partial file names without a handler extension. So - # +hello/index.html+ will match the first template it finds with a - # known template extension, +hello/index.html.erb+. Template extensions - # should not be confused with format extensions +html+, +js+, +xml+, - # etc. A format must be supplied to match a formated file. +hello/index+ - # will never match +hello/index.html.erb+. - def [](path) - end - - def load! - end - - def self.new_and_loaded(path) - returning new(path) do |path| - path.load! - end - end - - private - def relative_path_for_template_file(full_file_path) - full_file_path.split("#{@path}/").last - end - end - - class EagerPath < Path - def load! - return if @loaded - - @paths = {} - templates_in_path do |template| - template.load! - template.accessible_paths.each do |path| - @paths[path] = template - end - end - @paths.freeze - @loaded = true - end - - def [](path) - load! unless @loaded - @paths[path] - end - - private - def templates_in_path - (Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file| - yield create_template(file) unless File.directory?(file) - end - end - - def create_template(file) - Template.new(relative_path_for_template_file(file), self) - end - end - - extend TemplateHandlers - extend ActiveSupport::Memoizable - include Renderable - - # Templates that are exempt from layouts - @@exempt_from_layout = Set.new([/\.rjs$/]) - - # Don't render layouts for templates with the given extensions. - def self.exempt_from_layout(*extensions) - regexps = extensions.collect do |extension| - extension.is_a?(Regexp) ? extension : /\.#{Regexp.escape(extension.to_s)}$/ - end - @@exempt_from_layout.merge(regexps) - end - - attr_accessor :template_path, :filename, :load_path, :base_path - attr_accessor :locale, :name, :format, :extension - delegate :to_s, :to => :path - - def initialize(template_path, load_path = nil) - @template_path, @load_path = template_path.dup, load_path - @base_path, @name, @locale, @format, @extension = split(template_path) - @base_path.to_s.gsub!(/\/$/, '') # Push to split method - - # Extend with partial super powers - extend RenderablePartial if @name =~ /^_/ - end - - def accessible_paths - paths = [] - - if valid_extension?(extension) - paths << path - paths << path_without_extension - if multipart? - formats = format.split(".") - paths << "#{path_without_format_and_extension}.#{formats.first}" - paths << "#{path_without_format_and_extension}.#{formats.second}" - end - else - # template without explicit template handler should only be reachable through its exact path - paths << template_path - end - - paths - end - - def format_and_extension - (extensions = [format, extension].compact.join(".")).blank? ? nil : extensions - end - memoize :format_and_extension - - def multipart? - format && format.include?('.') - end - - def content_type - format.gsub('.', '/') - end - - def mime_type - Mime::Type.lookup_by_extension(format) if format && defined?(::Mime) - end - memoize :mime_type - - def path - [base_path, [name, locale, format, extension].compact.join('.')].compact.join('/') - end - memoize :path - - def path_without_extension - [base_path, [name, locale, format].compact.join('.')].compact.join('/') - end - memoize :path_without_extension - - def path_without_format_and_extension - [base_path, [name, locale].compact.join('.')].compact.join('/') - end - memoize :path_without_format_and_extension - - def relative_path - path = File.expand_path(filename) - path.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') if defined?(RAILS_ROOT) - path - end - memoize :relative_path - - def exempt_from_layout? - @@exempt_from_layout.any? { |exempted| path =~ exempted } - end - - def filename - # no load_path means this is an "absolute pathed" template - load_path ? File.join(load_path, template_path) : template_path - end - memoize :filename - - def source - File.read(filename) - end - memoize :source - - def method_segment - relative_path.to_s.gsub(/([^a-zA-Z0-9_])/) { $1.ord } - end - memoize :method_segment - - def render_template(view, local_assigns = {}) - render(view, local_assigns) - rescue Exception => e - raise e unless filename - if TemplateError === e - e.sub_template_of(self) - raise e - else - raise TemplateError.new(self, view.assigns, e) - end - end - - def load! - freeze - end - - private - def valid_extension?(extension) - !Template.registered_template_handler(extension).nil? - end - - def valid_locale?(locale) - locale && I18n.available_locales.include?(locale.to_sym) - end - - # Returns file split into an array - # [base_path, name, locale, format, extension] - def split(file) - if m = file.to_s.match(/^(.*\/)?([^\.]+)\.(.*)$/) - [m[1], m[2], *parse_extensions(m[3])] - end - end - - # returns parsed extensions as an array - # [locale, format, extension] - def parse_extensions(extensions) - exts = extensions.split(".") - - if extension = valid_extension?(exts.last) && exts.pop || nil - locale = valid_locale?(exts.first) && exts.shift || nil - format = exts.join('.') if exts.any? # join('.') is needed for multipart templates - else # no extension, just format - format = exts.last - end - - [locale, format, extension] - end - end -end diff --git a/actionpack/lib/action_view/template_error.rb b/actionpack/lib/action_view/template/error.rb index 37cb1c7c6c..37cb1c7c6c 100644 --- a/actionpack/lib/action_view/template_error.rb +++ b/actionpack/lib/action_view/template/error.rb diff --git a/actionpack/lib/action_view/template_handler.rb b/actionpack/lib/action_view/template/handler.rb index 672da0ed2b..672da0ed2b 100644 --- a/actionpack/lib/action_view/template_handler.rb +++ b/actionpack/lib/action_view/template/handler.rb diff --git a/actionpack/lib/action_view/template_handlers.rb b/actionpack/lib/action_view/template/handlers.rb index 205f8628f0..fb85f28851 100644 --- a/actionpack/lib/action_view/template_handlers.rb +++ b/actionpack/lib/action_view/template/handlers.rb @@ -1,8 +1,8 @@ module ActionView #:nodoc: module TemplateHandlers #:nodoc: - autoload :ERB, 'action_view/template_handlers/erb' - autoload :RJS, 'action_view/template_handlers/rjs' - autoload :Builder, 'action_view/template_handlers/builder' + autoload :ERB, 'action_view/template/handlers/erb' + autoload :RJS, 'action_view/template/handlers/rjs' + autoload :Builder, 'action_view/template/handlers/builder' def self.extended(base) base.register_default_template_handler :erb, TemplateHandlers::ERB diff --git a/actionpack/lib/action_view/template_handlers/builder.rb b/actionpack/lib/action_view/template/handlers/builder.rb index 788dc93326..788dc93326 100644 --- a/actionpack/lib/action_view/template_handlers/builder.rb +++ b/actionpack/lib/action_view/template/handlers/builder.rb diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb index e3120ba267..a20b1b0cd3 100644 --- a/actionpack/lib/action_view/template_handlers/erb.rb +++ b/actionpack/lib/action_view/template/handlers/erb.rb @@ -1,3 +1,5 @@ +require 'erb' + module ActionView module TemplateHandlers class ERB < TemplateHandler diff --git a/actionpack/lib/action_view/template_handlers/rjs.rb b/actionpack/lib/action_view/template/handlers/rjs.rb index 41a1fddb47..802a79b3fc 100644 --- a/actionpack/lib/action_view/template_handlers/rjs.rb +++ b/actionpack/lib/action_view/template/handlers/rjs.rb @@ -4,7 +4,7 @@ module ActionView include Compilable def compile(template) - "@template_format = :html;" + + "@formats = [:html];" + "controller.response.content_type ||= Mime::JS;" + "update_page do |page|;#{template.source}\nend" end diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/template/inline.rb index 54efa543c8..54efa543c8 100644 --- a/actionpack/lib/action_view/inline_template.rb +++ b/actionpack/lib/action_view/template/inline.rb diff --git a/actionpack/lib/action_view/template/partial.rb b/actionpack/lib/action_view/template/partial.rb new file mode 100644 index 0000000000..30dec1dc5b --- /dev/null +++ b/actionpack/lib/action_view/template/partial.rb @@ -0,0 +1,18 @@ +module ActionView + # NOTE: The template that this mixin is being included into is frozen + # so you cannot set or modify any instance variables + module RenderablePartial #:nodoc: + extend ActiveSupport::Memoizable + + def variable_name + name.sub(/\A_/, '').to_sym + end + memoize :variable_name + + def counter_name + "#{variable_name}_counter".to_sym + end + memoize :counter_name + + end +end diff --git a/actionpack/lib/action_view/template/path.rb b/actionpack/lib/action_view/template/path.rb new file mode 100644 index 0000000000..9709549b70 --- /dev/null +++ b/actionpack/lib/action_view/template/path.rb @@ -0,0 +1,87 @@ +module ActionView + class Template + class Path + attr_reader :path, :paths + delegate :hash, :inspect, :to => :path + + def initialize(options) + @cache = options[:cache] + end + + def to_s + if defined?(RAILS_ROOT) + path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') + else + path.to_s + end + end + + def to_str + path.to_str + end + + def ==(path) + to_str == path.to_str + end + + def eql?(path) + to_str == path.to_str + end + + def find_by_parts(name, extensions = nil, prefix = nil, partial = nil) + path = prefix ? "#{prefix}/" : "" + + name = name.to_s.split("/") + name[-1] = "_#{name[-1]}" if partial + + path << name.join("/") + + template = nil + + Array(extensions).each do |extension| + extensioned_path = extension ? "#{path}.#{extension}" : path + break if (template = find_template(extensioned_path)) + end + template || find_template(path) + end + + private + def create_template(file) + Template.new(file.split("#{self}/").last, self) + end + end + + class FileSystemPath < Path + def initialize(path, options = {}) + raise ArgumentError, "path already is a Path class" if path.is_a?(Path) + + super(options) + @path, @paths = path, {} + + # **/*/** is a hax for symlinked directories + load_templates("#{@path}/{**/*,**}/**") if @cache + end + + private + + def load_template(template) + template.load! + template.accessible_paths.each do |path| + @paths[path] = template + end + end + + def find_template(path) + load_templates("#{@path}/#{path}{,.*}") unless @cache + @paths[path] + end + + def load_templates(glob) + Dir[glob].each do |file| + load_template(create_template(file)) unless File.directory?(file) + end + end + + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_view/renderable.rb b/actionpack/lib/action_view/template/renderable.rb index ff7bc7d9de..54857516ab 100644 --- a/actionpack/lib/action_view/renderable.rb +++ b/actionpack/lib/action_view/template/renderable.rb @@ -6,6 +6,23 @@ module ActionView module Renderable #:nodoc: extend ActiveSupport::Memoizable + def render(view, locals) + compile(locals) + view.send(method_name(locals), locals) {|*args| yield(*args) } + end + + def load! + names = Base::CompiledTemplates.instance_methods.grep(/#{method_name_without_locals}/) + names.each do |name| + Base::CompiledTemplates.class_eval do + remove_method(name) + end + end + super + end + + private + def filename 'compiled-template' end @@ -18,30 +35,13 @@ module ActionView def compiled_source handler.call(self) end + memoize :compiled_source def method_name_without_locals ['_run', extension, method_segment].compact.join('_') end memoize :method_name_without_locals - def render(view, local_assigns = {}) - compile(local_assigns) - - view.with_template self do - view.send(:_evaluate_assigns_and_ivars) - view.send(:_set_controller_content_type, mime_type) if respond_to?(:mime_type) - - view.send(method_name(local_assigns), local_assigns) do |*names| - ivar = :@_proc_for_layout - if !view.instance_variable_defined?(:"@content_for_#{names.first}") && view.instance_variable_defined?(ivar) && (proc = view.instance_variable_get(ivar)) - view.capture(*names, &proc) - elsif view.instance_variable_defined?(ivar = :"@content_for_#{names.first || :layout}") - view.instance_variable_get(ivar) - end - end - end - end - def method_name(local_assigns) if local_assigns && local_assigns.any? method_name = method_name_without_locals.dup @@ -52,16 +52,16 @@ module ActionView method_name.to_sym end - private - # Compile and evaluate the template's code (if necessary) - def compile(local_assigns) - render_symbol = method_name(local_assigns) + # Compile and evaluate the template's code (if necessary) + def compile(local_assigns) + render_symbol = method_name(local_assigns) - if !Base::CompiledTemplates.method_defined?(render_symbol) || recompile? - compile!(render_symbol, local_assigns) - end + if !Base::CompiledTemplates.method_defined?(render_symbol) || recompile? + compile!(render_symbol, local_assigns) end + end + private def compile!(render_symbol, local_assigns) locals_code = local_assigns.keys.map { |key| "#{key} = local_assigns[:#{key}];" }.join @@ -74,9 +74,7 @@ module ActionView end_src begin - ActionView::Base::CompiledTemplates.module_eval(source, filename, 0) - rescue Errno::ENOENT => e - raise e # Missing template file, re-raise for Base to rescue + ActionView::Base::CompiledTemplates.module_eval(source, filename.to_s, 0) rescue Exception => e # errors from template code if logger = defined?(ActionController) && Base.logger logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}" diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb new file mode 100644 index 0000000000..0d2f201458 --- /dev/null +++ b/actionpack/lib/action_view/template/template.rb @@ -0,0 +1,187 @@ +require "action_view/template/path" + +module ActionView #:nodoc: + class Template + extend TemplateHandlers + extend ActiveSupport::Memoizable + + module Loading + def load! + @cached = true + # freeze + end + end + include Loading + + include Renderable + + # Templates that are exempt from layouts + @@exempt_from_layout = Set.new([/\.rjs$/]) + + # Don't render layouts for templates with the given extensions. + def self.exempt_from_layout(*extensions) + regexps = extensions.collect do |extension| + extension.is_a?(Regexp) ? extension : /\.#{Regexp.escape(extension.to_s)}$/ + end + @@exempt_from_layout.merge(regexps) + end + + attr_accessor :template_path, :filename, :load_path, :base_path + attr_accessor :locale, :name, :format, :extension + delegate :to_s, :to => :path + + def initialize(template_path, load_paths = []) + template_path = template_path.dup + @load_path, @filename = find_full_path(template_path, load_paths) + @base_path, @name, @locale, @format, @extension = split(template_path) + @base_path.to_s.gsub!(/\/$/, '') # Push to split method + + # Extend with partial super powers + extend RenderablePartial if @name =~ /^_/ + end + + def accessible_paths + paths = [] + + if valid_extension?(extension) + paths << path + paths << path_without_extension + if multipart? + formats = format.split(".") + paths << "#{path_without_format_and_extension}.#{formats.first}" + paths << "#{path_without_format_and_extension}.#{formats.second}" + end + else + # template without explicit template handler should only be reachable through its exact path + paths << template_path + end + + paths + end + + def relative_path + path = File.expand_path(filename) + path.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') if defined?(RAILS_ROOT) + path + end + memoize :relative_path + + def source + File.read(filename) + end + memoize :source + + def exempt_from_layout? + @@exempt_from_layout.any? { |exempted| path =~ exempted } + end + + def path_without_extension + [base_path, [name, locale, format].compact.join('.')].compact.join('/') + end + memoize :path_without_extension + + def path_without_format_and_extension + [base_path, [name, locale].compact.join('.')].compact.join('/') + end + memoize :path_without_format_and_extension + + def path + [base_path, [name, locale, format, extension].compact.join('.')].compact.join('/') + end + memoize :path + + def mime_type + Mime::Type.lookup_by_extension(format) if format && defined?(::Mime) + end + memoize :mime_type + + def multipart? + format && format.include?('.') + end + + def content_type + format.gsub('.', '/') + end + + private + + def format_and_extension + (extensions = [format, extension].compact.join(".")).blank? ? nil : extensions + end + memoize :format_and_extension + + def mtime + File.mtime(filename) + end + memoize :mtime + + def method_segment + relative_path.to_s.gsub(/([^a-zA-Z0-9_])/) { $1.ord } + end + memoize :method_segment + + def stale? + File.mtime(filename) > mtime + end + + def recompile? + !@cached + end + + def valid_extension?(extension) + !Template.registered_template_handler(extension).nil? + end + + def valid_locale?(locale) + I18n.available_locales.include?(locale.to_sym) + end + + def find_full_path(path, load_paths) + load_paths = Array(load_paths) + [nil] + load_paths.each do |load_path| + file = load_path ? "#{load_path.to_str}/#{path}" : path + return load_path, file if File.file?(file) + end + raise MissingTemplate.new(load_paths, path) + end + + # Returns file split into an array + # [base_path, name, locale, format, extension] + def split(file) + if m = file.to_s.match(/^(.*\/)?([^\.]+)\.(.*)$/) + base_path = m[1] + name = m[2] + extensions = m[3] + else + return + end + + locale = nil + format = nil + extension = nil + + if m = extensions.split(".") + if valid_locale?(m[0]) && m[1] && valid_extension?(m[2]) # All three + locale = m[0] + format = m[1] + extension = m[2] + elsif m[0] && m[1] && valid_extension?(m[2]) # Multipart formats + format = "#{m[0]}.#{m[1]}" + extension = m[2] + elsif valid_locale?(m[0]) && valid_extension?(m[1]) # locale and extension + locale = m[0] + extension = m[1] + elsif valid_extension?(m[1]) # format and extension + format = m[0] + extension = m[1] + elsif valid_extension?(m[0]) # Just extension + extension = m[0] + else # No extension + format = m[0] + end + end + + [base_path, name, locale, format, extension] + end + end +end diff --git a/actionpack/lib/action_view/template/text.rb b/actionpack/lib/action_view/template/text.rb new file mode 100644 index 0000000000..f81174d707 --- /dev/null +++ b/actionpack/lib/action_view/template/text.rb @@ -0,0 +1,9 @@ +module ActionView #:nodoc: + class TextTemplate < String #:nodoc: + + def render(*) self end + + def exempt_from_layout?() false end + + end +end diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index ec337bb05b..c8f204046b 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -7,18 +7,15 @@ module ActionView @_rendered = { :template => nil, :partials => Hash.new(0) } initialize_without_template_tracking(*args) end - end - - module Renderable - alias_method :render_without_template_tracking, :render - def render(view, local_assigns = {}) - if respond_to?(:path) && !is_a?(InlineTemplate) - rendered = view.instance_variable_get(:@_rendered) - rendered[:partials][self] += 1 if is_a?(RenderablePartial) - rendered[:template] ||= self + + alias_method :_render_template_without_template_tracking, :_render_template + def _render_template(template, local_assigns = {}) + if template.respond_to?(:path) && !template.is_a?(InlineTemplate) + @_rendered[:partials][template] += 1 if template.is_a?(RenderablePartial) + @_rendered[:template] ||= template end - render_without_template_tracking(view, local_assigns) - end + _render_template_without_template_tracking(template, local_assigns) + end end class TestCase < ActiveSupport::TestCase |