diff options
Diffstat (limited to 'actionpack/lib/action_controller/new_base')
6 files changed, 150 insertions, 25 deletions
diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index 4892886341..1adcc9c71f 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -8,6 +8,7 @@ module ActionController include ActionController::HideActions include ActionController::UrlFor + include ActionController::Redirector include ActionController::Renderer include ActionController::Layouts include ActionController::ConditionalGet @@ -34,19 +35,83 @@ module ActionController end end - def render(action = action_name, options = {}) + def render_to_body(action = nil, options = {}) if action.is_a?(Hash) options, action = action, nil - else - options.merge! :action => action + elsif action.is_a?(String) || action.is_a?(Symbol) + key = case action = action.to_s + when %r{^/} then :file + when %r{/} then :template + else :action + end + options.merge! key => action + elsif action + options.merge! :partial => action + end + + if options.key?(:action) && options[:action].to_s.index("/") + options[:template] = options.delete(:action) end - super(options) + # options = {:template => options.to_s} if options.is_a?(String) || options.is_a?(Symbol) + super(options) || " " end - def render_to_body(options = {}) - options = {:template => options} if options.is_a?(String) - super + # Redirects the browser to the target specified in +options+. This parameter can take one of three forms: + # + # * <tt>Hash</tt> - The URL will be generated by calling url_for with the +options+. + # * <tt>Record</tt> - The URL will be generated by calling url_for with the +options+, which will reference a named URL for that record. + # * <tt>String</tt> starting with <tt>protocol://</tt> (like <tt>http://</tt>) - Is passed straight through as the target for redirection. + # * <tt>String</tt> not containing a protocol - The current protocol and host is prepended to the string. + # * <tt>:back</tt> - Back to the page that issued the request. Useful for forms that are triggered from multiple places. + # Short-hand for <tt>redirect_to(request.env["HTTP_REFERER"])</tt> + # + # Examples: + # redirect_to :action => "show", :id => 5 + # redirect_to post + # redirect_to "http://www.rubyonrails.org" + # redirect_to "/images/screenshot.jpg" + # redirect_to articles_url + # redirect_to :back + # + # The redirection happens as a "302 Moved" header unless otherwise specified. + # + # Examples: + # redirect_to post_url(@post), :status=>:found + # redirect_to :action=>'atom', :status=>:moved_permanently + # redirect_to post_url(@post), :status=>301 + # redirect_to :action=>'atom', :status=>302 + # + # When using <tt>redirect_to :back</tt>, if there is no referrer, + # RedirectBackError will be raised. You may specify some fallback + # behavior for this case by rescuing RedirectBackError. + def redirect_to(options = {}, response_status = {}) #:doc: + raise ActionControllerError.new("Cannot redirect to nil!") if options.nil? + + status = if options.is_a?(Hash) && options.key?(:status) + interpret_status(options.delete(:status)) + elsif response_status.key?(:status) + interpret_status(response_status[:status]) + else + 302 + end + + url = case options + # The scheme name consist of a letter followed by any combination of + # letters, digits, and the plus ("+"), period ("."), or hyphen ("-") + # characters; and is terminated by a colon (":"). + when %r{^\w[\w\d+.-]*:.*} + options + when String + request.protocol + request.host_with_port + options + when :back + raise RedirectBackError unless refer = request.headers["Referer"] + refer + else + url_for(options) + end + + super(url, status) end def process_action diff --git a/actionpack/lib/action_controller/new_base/compatibility.rb b/actionpack/lib/action_controller/new_base/compatibility.rb index 4655a94923..d17f498b60 100644 --- a/actionpack/lib/action_controller/new_base/compatibility.rb +++ b/actionpack/lib/action_controller/new_base/compatibility.rb @@ -8,27 +8,40 @@ module ActionController ::ActionController::DoubleRenderError = ::AbstractController::DoubleRenderError cattr_accessor :session_options - self.send(:class_variable_set, "@@session_options", {}) + self.session_options = {} cattr_accessor :allow_concurrency - self.send(:class_variable_set, "@@allow_concurrency", false) + self.allow_concurrency = false cattr_accessor :param_parsers - self.send(:class_variable_set, "@@param_parsers", { Mime::MULTIPART_FORM => :multipart_form, - Mime::URL_ENCODED_FORM => :url_encoded_form, - Mime::XML => :xml_simple, - Mime::JSON => :json }) + self.param_parsers = { Mime::MULTIPART_FORM => :multipart_form, + Mime::URL_ENCODED_FORM => :url_encoded_form, + Mime::XML => :xml_simple, + Mime::JSON => :json } cattr_accessor :relative_url_root - self.send(:class_variable_set, "@@relative_url_root", ENV['RAILS_RELATIVE_URL_ROOT']) + self.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT'] cattr_accessor :default_charset - self.send(:class_variable_set, "@@default_charset", "utf-8") + self.default_charset = "utf-8" - cattr_reader :protected_instance_variables - self.send(:class_variable_set, "@@protected_instance_variables", %w(@assigns @performed_redirect @performed_render @variables_added @request_origin @url @parent_controller + # cattr_reader :protected_instance_variables + cattr_accessor :protected_instance_variables + self.protected_instance_variables = %w(@assigns @performed_redirect @performed_render @variables_added @request_origin @url @parent_controller @action_name @before_filter_chain_aborted @action_cache_path @_headers @_params - @_flash @_response)) + @_flash @_response) + + # Indicates whether or not optimise the generated named + # route helper methods + cattr_accessor :optimise_named_routes + self.optimise_named_routes = true + + cattr_accessor :resources_path_names + self.resources_path_names = { :new => 'new', :edit => 'edit' } + + # Controls the resource action separator + cattr_accessor :resource_action_separator + self.resource_action_separator = "/" end module ClassMethods @@ -38,7 +51,12 @@ module ActionController raise env["action_dispatch.rescue.exception"] end end - + + def initialize(*) + super + @template = _action_view + end + def render_to_body(options) if options.is_a?(Hash) && options.key?(:template) options[:template].sub!(/^\//, '') diff --git a/actionpack/lib/action_controller/new_base/http.rb b/actionpack/lib/action_controller/new_base/http.rb index f269fe70db..fb6041a04e 100644 --- a/actionpack/lib/action_controller/new_base/http.rb +++ b/actionpack/lib/action_controller/new_base/http.rb @@ -38,6 +38,12 @@ module ActionController controller.call(env).to_rack end + delegate :headers, :to => "@_response" + + def params + @_params ||= @_request.parameters + end + # :api: private def call(name, env) @_request = ActionDispatch::Request.new(env) diff --git a/actionpack/lib/action_controller/new_base/layouts.rb b/actionpack/lib/action_controller/new_base/layouts.rb index e851eb5f9a..bf5b14c4e1 100644 --- a/actionpack/lib/action_controller/new_base/layouts.rb +++ b/actionpack/lib/action_controller/new_base/layouts.rb @@ -15,7 +15,7 @@ module ActionController # render :text => ..., :layout => ... # or # render :anything_else - if (!options.key?(:text) && !options.key?(:inline)) || options.key?(:layout) + if (!options.key?(:text) && !options.key?(:inline) && !options.key?(:partial)) || options.key?(:layout) options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _default_layout end diff --git a/actionpack/lib/action_controller/new_base/redirector.rb b/actionpack/lib/action_controller/new_base/redirector.rb new file mode 100644 index 0000000000..ff7b74341c --- /dev/null +++ b/actionpack/lib/action_controller/new_base/redirector.rb @@ -0,0 +1,19 @@ +module ActionController + class RedirectBackError < AbstractController::Error #:nodoc: + DEFAULT_MESSAGE = 'No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].' + + def initialize(message = nil) + super(message || DEFAULT_MESSAGE) + end + end + + module Redirector + def redirect_to(url, status) #:doc: + raise AbstractController::DoubleRenderError if response_body + logger.info("Redirected to #{url}") if logger && logger.info? + response.status = status + response.location = url.gsub(/[\r\n]/, '') + self.response_body = "<html><body>You are being <a href=\"#{CGI.escapeHTML(url)}\">redirected</a>.</body></html>" + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index 41e3dfbe23..8a9f230603 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -9,13 +9,9 @@ module ActionController super end - def render(options = {}) + def render_to_body(options) _process_options(options) - super(options) - end - - def render_to_body(options) if options.key?(:text) options[:_template] = ActionView::TextTemplate.new(_text(options)) template = nil @@ -25,12 +21,18 @@ module ActionController options[:_template] = template elsif options.key?(:template) options[:_template_name] = options[:template] + elsif options.key?(:file) + options[:_template_name] = options[:file] + elsif options.key?(:partial) + _render_partial(options[:partial], options) else options[:_template_name] = (options[:action] || action_name).to_s options[:_prefix] = _prefix end ret = super(options) + + options[:_template] ||= _action_view._partial response.content_type ||= options[:_template].mime_type ret end @@ -49,6 +51,21 @@ module ActionController else text.to_s end end + + def _render_partial(partial, options) + case partial + when true + options[:_prefix] = _prefix + when String + options[:_prefix] = _prefix unless partial.index('/') + options[:_template_name] = partial + else + options[:_partial_object] = true + return + end + + options[:_partial] = options[:object] || true + end def _process_options(options) status, content_type = options.values_at(:status, :content_type) |