From 2a12686832fbcf0566454904a5d733998506bf56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 12 Mar 2010 14:25:10 +0100 Subject: Allow anything that responds to render to be given as :template and use find_template instead of find in views. --- actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/lookup_context.rb | 2 ++ actionpack/lib/action_view/render/layouts.rb | 4 ++-- actionpack/lib/action_view/render/partials.rb | 2 +- actionpack/lib/action_view/render/rendering.rb | 9 ++++----- 5 files changed, 10 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index ffe3060404..f1b1c22075 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -194,7 +194,7 @@ module ActionView #:nodoc: attr_accessor :base_path, :assigns, :template_extension, :lookup_context attr_internal :captures, :request, :layout, :controller, :template, :config - delegate :find, :exists?, :formats, :formats=, :locale, :locale=, + delegate :find_template, :template_exists?, :formats, :formats=, :locale, :locale=, :view_paths, :view_paths=, :with_fallbacks, :update_details, :to => :lookup_context delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers, diff --git a/actionpack/lib/action_view/lookup_context.rb b/actionpack/lib/action_view/lookup_context.rb index 27ee8b23c9..22ab076b59 100644 --- a/actionpack/lib/action_view/lookup_context.rb +++ b/actionpack/lib/action_view/lookup_context.rb @@ -70,6 +70,7 @@ module ActionView def find(name, prefix = nil, partial = false) @view_paths.find(name, prefix, partial, details, details_key) end + alias :find_template :find def find_all(name, prefix = nil, partial = false) @view_paths.find_all(name, prefix, partial, details, details_key) @@ -78,6 +79,7 @@ module ActionView def exists?(name, prefix = nil, partial = false) @view_paths.exists?(name, prefix, partial, details, details_key) end + alias :template_exists? :exists? # Add fallbacks to the view paths. Useful in cases you are rendering a :file. def with_fallbacks diff --git a/actionpack/lib/action_view/render/layouts.rb b/actionpack/lib/action_view/render/layouts.rb index 8688de3d18..91a92a833a 100644 --- a/actionpack/lib/action_view/render/layouts.rb +++ b/actionpack/lib/action_view/render/layouts.rb @@ -49,10 +49,10 @@ module ActionView def _find_layout(layout) #:nodoc: begin layout =~ /^\// ? - with_fallbacks { find(layout) } : find(layout) + with_fallbacks { find_template(layout) } : find_template(layout) rescue ActionView::MissingTemplate => e update_details(:formats => nil) do - raise unless exists?(layout) + raise unless template_exists?(layout) end end end diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 950c9d2cd8..0fe2d560f7 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -294,7 +294,7 @@ module ActionView def find_template(path=@path) return path unless path.is_a?(String) prefix = @view.controller_path unless path.include?(?/) - @view.find(path, prefix, true) + @view.find_template(path, prefix, true) end def partial_path(object = @object) diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 47ea70f5ad..d9ac1f6290 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -59,13 +59,12 @@ module ActionView handler = Template.handler_class_for_extension(options[:type] || "erb") Template.new(options[:inline], "inline template", handler, {}) elsif options.key?(:text) - Template::Text.new(options[:text], self.formats.try(:first)) - elsif options.key?(:_template) - options[:_template] + Template::Text.new(options[:text], formats.try(:first)) elsif options.key?(:file) - with_fallbacks { find(options[:file], options[:prefix]) } + with_fallbacks { find_template(options[:file], options[:prefix]) } elsif options.key?(:template) - find(options[:template], options[:prefix]) + options[:template].respond_to?(:render) ? + options[:template] : find_template(options[:template], options[:prefix]) end end -- cgit v1.2.3 From f2c0a353aef41a6df2de8e1fe3eaa3d8bbd8a6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 12 Mar 2010 20:39:53 +0100 Subject: Finish cleaning up rendering stack from views and move assigns evaluation to controller (so plugins and/or controllers can overwrite just one method). --- actionpack/lib/action_view/base.rb | 9 ------ .../lib/action_view/helpers/prototype_helper.rb | 1 - actionpack/lib/action_view/render/layouts.rb | 5 +-- actionpack/lib/action_view/render/rendering.rb | 37 +++++----------------- .../lib/action_view/template/handlers/rjs.rb | 3 +- actionpack/lib/action_view/template/text.rb | 14 ++++---- 6 files changed, 16 insertions(+), 53 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index f1b1c22075..feaf45c333 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -264,14 +264,5 @@ module ActionView #:nodoc: response.body_parts << part nil end - - # Evaluates the local assigns and controller ivars, pushes them to the view. - def _evaluate_assigns_and_ivars #:nodoc: - if controller - variables = controller.instance_variable_names - variables -= controller.protected_instance_variables if controller.respond_to?(:protected_instance_variables) - variables.each { |name| instance_variable_set(name, controller.instance_variable_get(name)) } - end - end end end diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index be49b5cc28..e58fdf81fb 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -180,7 +180,6 @@ module ActionView # #include_helpers_from_context has nothing to overwrite. class JavaScriptGenerator #:nodoc: def initialize(context, &block) #:nodoc: - context._evaluate_assigns_and_ivars @context, @lines = context, [] @context.update_details(:formats => [:js, :html]) do include_helpers_from_context diff --git a/actionpack/lib/action_view/render/layouts.rb b/actionpack/lib/action_view/render/layouts.rb index 91a92a833a..0cb688ca77 100644 --- a/actionpack/lib/action_view/render/layouts.rb +++ b/actionpack/lib/action_view/render/layouts.rb @@ -1,8 +1,5 @@ -require 'active_support/core_ext/object/try' - module ActionView module Layouts - # You can think of a layout as a method that is called with a block. _layout_for # returns the contents that are yielded to the layout. If the user calls yield # :some_name, the block, by default, returns content_for(:some_name). If the user @@ -46,7 +43,7 @@ module ActionView # This is the method which actually finds the layout using details in the lookup # context object. If no layout is found, it checkes if at least a layout with # the given name exists across all details before raising the error. - def _find_layout(layout) #:nodoc: + def find_layout(layout) #:nodoc: begin layout =~ /^\// ? with_fallbacks { find_template(layout) } : find_template(layout) diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index d9ac1f6290..9b5b976727 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -12,14 +12,17 @@ module ActionView # # 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 = {}, locals = {}, &block) #:nodoc: + def render(options = {}, locals = {}, &block) case options when Hash if block_given? content = _render_partial(options.merge(:partial => options[:layout]), &block) safe_concat(content) + elsif options.key?(:partial) + _render_partial(options) else - _render(options) + template = _determine_template(options) + _render_template(template, options[:layout], options) end when :update update_page(&block) @@ -28,31 +31,6 @@ module ActionView end end - # This is the API to render a ViewContext's template from a controller. - def render_template(options, &block) - _evaluate_assigns_and_ivars - - # TODO Layout for partials should be handled here, because inside the - # partial renderer it looks for the layout as a partial. - if options.key?(:partial) && options[:layout] - options[:layout] = _find_layout(options[:layout]) - end - - _render(options, &block) - end - - # This method holds the common render logic for both controllers and - # views rendering stacks. - def _render(options) #:nodoc: - if options.key?(:partial) - _render_partial(options) - else - template = _determine_template(options) - yield template if block_given? - _render_template(template, options[:layout], options) - end - end - # Determine the template to be rendered using the given options. def _determine_template(options) #:nodoc: if options.key?(:inline) @@ -71,8 +49,10 @@ module ActionView # Renders the given template. An string representing the layout can be # supplied as well. def _render_template(template, layout = nil, options = {}) #:nodoc: + self.formats = template.formats + locals = options[:locals] || {} - layout = _find_layout(layout) if layout + layout = find_layout(layout) if layout ActiveSupport::Notifications.instrument("action_view.render_template", :identifier => template.identifier, :layout => layout.try(:identifier)) do @@ -88,6 +68,5 @@ module ActionView content end end - end end diff --git a/actionpack/lib/action_view/template/handlers/rjs.rb b/actionpack/lib/action_view/template/handlers/rjs.rb index 63e7dc0902..128be5077c 100644 --- a/actionpack/lib/action_view/template/handlers/rjs.rb +++ b/actionpack/lib/action_view/template/handlers/rjs.rb @@ -6,8 +6,7 @@ module ActionView self.default_format = Mime::JS def compile(template) - "controller.response.content_type ||= Mime::JS;" + - "update_page do |page|;#{template.source}\nend" + "update_page do |page|;#{template.source}\nend" end def default_format diff --git a/actionpack/lib/action_view/template/text.rb b/actionpack/lib/action_view/template/text.rb index df394b0fb0..269340514c 100644 --- a/actionpack/lib/action_view/template/text.rb +++ b/actionpack/lib/action_view/template/text.rb @@ -1,10 +1,12 @@ module ActionView #:nodoc: class Template class Text < String #:nodoc: - def initialize(string, content_type = nil) + attr_accessor :mime_type + + def initialize(string, mime_type = nil) super(string.to_s) - @content_type = Mime[content_type] || content_type if content_type - @content_type ||= Mime::TEXT + @mime_type = Mime[mime_type] || mime_type if mime_type + @mime_type ||= Mime::TEXT end def identifier @@ -19,12 +21,8 @@ module ActionView #:nodoc: to_s end - def mime_type - @content_type - end - def formats - [@content_type.to_sym] + [@mime_type.to_sym] end def partial? -- cgit v1.2.3 From e484d4ae5684b9ca49b27a844bf48c91c945814e Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Thu, 11 Mar 2010 00:47:10 -0500 Subject: Made asset_tag_helper use config.perform_caching instead of ActionController::Base.perform_caching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- .../lib/action_view/helpers/asset_tag_helper.rb | 28 +++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index 0c488b6793..03ae8ce0d8 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -242,12 +242,12 @@ module ActionView # == Caching multiple javascripts into one # # You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be - # compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching + # compressed by gzip (leading to faster transfers). Caching will only happen if config.perform_caching # is set to true (which is the case by default for the Rails production environment, but not for the development # environment). # # ==== Examples - # javascript_include_tag :all, :cache => true # when ActionController::Base.perform_caching is false => + # javascript_include_tag :all, :cache => true # when config.perform_caching is false => # # # ... @@ -255,15 +255,15 @@ module ActionView # # # - # javascript_include_tag :all, :cache => true # when ActionController::Base.perform_caching is true => + # javascript_include_tag :all, :cache => true # when config.perform_caching is true => # # - # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is false => + # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is false => # # # # - # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is true => + # javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.perform_caching is true => # # # The :recursive option is also available for caching: @@ -275,11 +275,11 @@ module ActionView cache = concat || options.delete("cache") recursive = options.delete("recursive") - if concat || (ActionController::Base.perform_caching && cache) + if concat || (config.perform_caching && cache) joined_javascript_name = (cache == true ? "all" : cache) + ".js" joined_javascript_path = File.join(joined_javascript_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.javascripts_dir, joined_javascript_name) - unless ActionController::Base.perform_caching && File.exists?(joined_javascript_path) + unless config.perform_caching && File.exists?(joined_javascript_path) write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive)) end javascript_src_tag(joined_javascript_name, options) @@ -390,25 +390,25 @@ module ActionView # == Caching multiple stylesheets into one # # You can also cache multiple stylesheets into one file, which requires less HTTP connections and can better be - # compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching + # compressed by gzip (leading to faster transfers). Caching will only happen if config.perform_caching # is set to true (which is the case by default for the Rails production environment, but not for the development # environment). Examples: # # ==== Examples - # stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false => + # stylesheet_link_tag :all, :cache => true # when config.perform_caching is false => # # # # - # stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is true => + # stylesheet_link_tag :all, :cache => true # when config.perform_caching is true => # # - # stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is false => + # stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when config.perform_caching is false => # # # # - # stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is true => + # stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when config.perform_caching is true => # # # The :recursive option is also available for caching: @@ -426,11 +426,11 @@ module ActionView cache = concat || options.delete("cache") recursive = options.delete("recursive") - if concat || (ActionController::Base.perform_caching && cache) + if concat || (config.perform_caching && cache) joined_stylesheet_name = (cache == true ? "all" : cache) + ".css" joined_stylesheet_path = File.join(joined_stylesheet_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.stylesheets_dir, joined_stylesheet_name) - unless ActionController::Base.perform_caching && File.exists?(joined_stylesheet_path) + unless config.perform_caching && File.exists?(joined_stylesheet_path) write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive)) end stylesheet_tag(joined_stylesheet_name, options) -- cgit v1.2.3 From ee4c89627ad5d7b041b88ab4027d3f0d5d582d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 13 Mar 2010 09:14:09 +0100 Subject: Remove formats setters from render template, speeding up partial and collection renderings. --- actionpack/lib/action_view/render/rendering.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 9b5b976727..310efe40e2 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -22,6 +22,7 @@ module ActionView _render_partial(options) else template = _determine_template(options) + self.formats = template.formats _render_template(template, options[:layout], options) end when :update @@ -49,8 +50,6 @@ module ActionView # Renders the given template. An string representing the layout can be # supplied as well. def _render_template(template, layout = nil, options = {}) #:nodoc: - self.formats = template.formats - locals = options[:locals] || {} layout = find_layout(layout) if layout -- cgit v1.2.3 From 3da9a08a7367eb389ddc03159bfbe5e9a8416e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 14 Mar 2010 10:25:29 +0100 Subject: Optimize DetailsKey generation. --- actionpack/lib/action_view/lookup_context.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/lookup_context.rb b/actionpack/lib/action_view/lookup_context.rb index 22ab076b59..8eb17bf8f1 100644 --- a/actionpack/lib/action_view/lookup_context.rb +++ b/actionpack/lib/action_view/lookup_context.rb @@ -38,17 +38,18 @@ module ActionView register_detail(:locale) { [I18n.locale] } class DetailsKey #:nodoc: - attr_reader :details alias :eql? :equal? + alias :object_hash :hash + attr_reader :hash @details_keys = Hash.new def self.get(details) - @details_keys[details] ||= new(details) + @details_keys[details] ||= new end - def initialize(details) - @details, @hash = details, details.hash + def initialize + @hash = object_hash end end -- cgit v1.2.3 From beeb02076a0b7a8bce59555cd486b96c59e231f0 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sun, 14 Mar 2010 13:11:07 -0300 Subject: Making escaped things more readable --- actionpack/lib/action_view/helpers/active_model_helper.rb | 2 +- actionpack/lib/action_view/helpers/tag_helper.rb | 2 +- actionpack/lib/action_view/helpers/url_helper.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/active_model_helper.rb b/actionpack/lib/action_view/helpers/active_model_helper.rb index 4e12cdab54..e3db2923f7 100644 --- a/actionpack/lib/action_view/helpers/active_model_helper.rb +++ b/actionpack/lib/action_view/helpers/active_model_helper.rb @@ -129,7 +129,7 @@ module ActionView if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && (errors = obj.errors[method]) content_tag("div", - (options[:prepend_text].html_safe << errors.first).safe_concat(options[:append_text]), + "#{options[:prepend_text]}#{ERB::Util.h(errors.first)}#{options[:append_text]}".html_safe, :class => options[:css_class] ) else diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index d9d2588a2a..bbbc1f0981 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -109,7 +109,7 @@ module ActionView def content_tag_string(name, content, options, escape = true) tag_options = tag_options(options, escape) if options - ("<#{name}#{tag_options}>".html_safe << content.to_s).safe_concat("") + "<#{name}#{tag_options}>#{ERB::Util.h(content)}".html_safe end def tag_options(options, escape = true) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 148f2868e9..14d59034f1 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -224,7 +224,7 @@ module ActionView end href_attr = "href=\"#{url}\"" unless href - ("".html_safe << (name || url)).safe_concat("") + "#{ERB::Util.h(name || url)}".html_safe end end -- cgit v1.2.3 From a594a22267bfd3346e00923742c4aa7edad0cef7 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 15 Mar 2010 18:29:21 +0100 Subject: with_output_buffer cannot assume there's an output_buffer [#4182 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/lib/action_view/helpers/capture_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index 75fc2fddeb..03c7ba5a87 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -140,7 +140,7 @@ module ActionView def with_output_buffer(buf = nil) #:nodoc: unless buf buf = ActionView::OutputBuffer.new - buf.force_encoding(output_buffer.encoding) if buf.respond_to?(:force_encoding) + buf.force_encoding(output_buffer.encoding) if output_buffer && buf.respond_to?(:force_encoding) end self.output_buffer, old_buffer = buf, output_buffer yield -- cgit v1.2.3