From 2f7c5f84e4834e49001ed565cfe45f14e120613f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 May 2005 11:07:09 +0000 Subject: Cure some ills discovered with the refactoring git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1351 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/base.rb | 5 ++- .../deprecated_renders_and_redirects.rb | 10 ++++- actionpack/lib/action_controller/layout.rb | 50 +++++++++------------- actionpack/lib/action_controller/response.rb | 2 +- actionpack/lib/action_controller/verification.rb | 2 +- 5 files changed, 35 insertions(+), 34 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index e128b1434e..696bc14292 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -440,6 +440,7 @@ module ActionController #:nodoc: # A unified replacement for the individual renders (work-in-progress). def render(options = {}, deprecated_status = nil) + # puts "Rendering: #{options.inspect}" raise DoubleRenderError, "Can only render or redirect once per action" if performed? # Backwards compatibility @@ -489,7 +490,7 @@ module ActionController #:nodoc: end # Returns the result of the render as a string. - def render_to_string(options) #:doc: + def render_to_string(options = {}) #:doc: result = render(options) erase_render_results return result @@ -538,7 +539,7 @@ module ActionController #:nodoc: case options when %r{^\w+://.*} raise DoubleRenderError, "Can only render or redirect once per action" if performed? - logger.info("Redirected to #{url}") unless logger.nil? + logger.info("Redirected to #{options}") unless logger.nil? @response.redirect(options) @performed_redirect = true diff --git a/actionpack/lib/action_controller/deprecated_renders_and_redirects.rb b/actionpack/lib/action_controller/deprecated_renders_and_redirects.rb index a511656c1e..c38d489a4e 100644 --- a/actionpack/lib/action_controller/deprecated_renders_and_redirects.rb +++ b/actionpack/lib/action_controller/deprecated_renders_and_redirects.rb @@ -26,7 +26,7 @@ module ActionController # considerably faster than rendering through the template engine. # Use block for response body if provided (useful for deferred rendering or streaming output). def render_text(text = nil, status = nil) #:doc: - render(:text => text, :status => status) { yield } + render :text => text, :status => status end # Renders an empty response that can be used when the request is only interested in triggering an effect. Do note that good @@ -51,6 +51,14 @@ module ActionController render :partial => partial_name, :collection => collection, :spacer_template => partial_spacer_template, :locals => local_assigns end + def render_with_layout(template_name = default_template_name, status = nil, layout = nil) #:nodoc: + render :template => template_name, :status => status, :layout => layout + end + + def render_without_layout(template_name = default_template_name, status = nil) #:nodoc: + render :template => template_name, :status => status, :layout => false + end + # Deprecated in favor of calling redirect_to directly with the path. def redirect_to_path(path) #:doc: diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb index d6423fdbff..104079efe1 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/layout.rb @@ -3,8 +3,8 @@ module ActionController #:nodoc: def self.append_features(base) super base.class_eval do - alias_method :render_without_layout, :render - alias_method :render, :render_with_layout + alias_method :render_with_no_layout, :render + alias_method :render, :render_with_a_layout class << self alias_method :inherited_without_layout, :inherited @@ -202,43 +202,35 @@ module ActionController #:nodoc: active_layout.include?("/") ? active_layout : "layouts/#{active_layout}" if active_layout end - def xrender_with_layout(template_name = default_template_name, status = nil, layout = nil) #:nodoc: - if layout ||= active_layout and action_has_layout? - add_variables_to_assigns - logger.info("Rendering #{template_name} within #{layout}") unless logger.nil? - @content_for_layout = @template.render_file(template_name, true) - render_without_layout(layout, status) - else - render_without_layout(template_name, status) - end - end + def render_with_a_layout(options = {}, deprecated_status = nil, deprecated_layout = nil) #:nodoc: + if (layout = pick_layout(options, deprecated_layout)) && options.is_a?(Hash) && options[:text] + logger.info("Rendering #{options[:template]} within #{layout}") unless logger.nil? - def render_with_layout(options = {}, deprecated_status = nil, deprecated_layout = nil) - if (layout = active_layout_for_r(options, deprecated_layout)) && options[:text] - add_variables_to_assigns - logger.info("Rendering #{template_name} within #{layout}") unless logger.nil? + @content_for_layout = render_with_no_layout(options.merge(:layout => false)) + erase_render_results - @content_for_layout = render_without_layout(options) add_variables_to_assigns - - erase_render_results - render_without_layout(options.merge({ :text => @template.render_file(layout, true), :status => options[:status] || deprecated_status })) + render_with_no_layout(options.merge({ :text => @template.render_file(layout, true), :status => options[:status] || deprecated_status })) else - render_without_layout(options, deprecated_status) + render_with_no_layout(options, deprecated_status) end end private - def active_layout_for_r(options = {}, deprecated_layout = nil) + def pick_layout(options = {}, deprecated_layout = nil) return deprecated_layout unless deprecated_layout.nil? - case options[:layout] - when FalseClass - nil - when NilClass - active_layout if action_has_layout? - else - active_layout(options[:layout]) + if options.is_a?(Hash) + case options[:layout] + when FalseClass + nil + when NilClass + active_layout if action_has_layout? + else + active_layout(options[:layout]) + end + else + (deprecated_layout || active_layout) if action_has_layout? end end diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb index 227aa27cc5..746d6097be 100755 --- a/actionpack/lib/action_controller/response.rb +++ b/actionpack/lib/action_controller/response.rb @@ -8,7 +8,7 @@ module ActionController end def redirect(to_url, permanently = false) - @headers["Status"] ||= "302 Found" + @headers["Status"] = "302 Found" unless @headers["Status"] == "301 Moved Permanently" @headers["location"] = to_url @body = "You are being redirected." diff --git a/actionpack/lib/action_controller/verification.rb b/actionpack/lib/action_controller/verification.rb index b0f5236adf..021aa27b6d 100644 --- a/actionpack/lib/action_controller/verification.rb +++ b/actionpack/lib/action_controller/verification.rb @@ -77,7 +77,7 @@ module ActionController #:nodoc: if prereqs_invalid flash.update(options[:add_flash]) if options[:add_flash] - redirect_to(options[:redirect_to]) + redirect_to(options[:redirect_to]) if options[:redirect_to] return false end -- cgit v1.2.3