lib/plezi/handlers/controller_magic.rb in plezi-0.12.13 vs lib/plezi/handlers/controller_magic.rb in plezi-0.12.14
- old
+ new
@@ -60,35 +60,30 @@
attr_reader :host_params
# this method does two things.
#
# 1. sets redirection headers for the response.
- # 2. sets the `flash` object (short-time cookies) with all the values passed except the :status value.
+ # 2. sets the `flash` object (short-time cookies) with all the values passed except the :permanent value.
#
# use:
- # redirect_to 'http://google.com', notice: "foo", status: 302
- # # => redirects to 'http://google.com' with status 302 and adds notice: "foo" to the flash
- # or simply:
+ # redirect_to 'http://google.com', notice: "foo", permanent: true
+ # # => redirects to 'http://google.com' with status 301 (permanent redirection) and adds notice: "foo" to the flash
+ # or, a simple temporary redirect:
# redirect_to 'http://google.com'
- # # => redirects to 'http://google.com' with status 302 (default status)
+ # # => redirects to 'http://google.com' with status 302 (default temporary redirection)
#
- # if the url is a symbol, the method will try to format it into a correct url, replacing any
- # underscores ('_') with a backslash ('/').
+ # if the url is a symbol or a hash, the method will try to format it into a url Srting, using the `url_for` method.
#
- # if the url is an empty string, the method will try to format it into a correct url
- # representing the index of the application (http://server/)
+ # if the url is a String, it will be passed along as is.
#
+ # An empty String or `nil` will be replaced with the root path for the request's specific host (i.e. `http://localhost:3000/`).
+ #
def redirect_to url, options = {}
return super() if defined? super
- raise 'Cannot redirect after headers were sent.' if response.headers_sent?
- url = "#{request.base_url}/#{url.to_s.gsub('_', '/')}" if url.is_a?(Symbol) || ( url.is_a?(String) && url.empty? ) || url.nil?
+ url = full_url_for(url) unless url.is_a?(String) || url.nil?
# redirect
- response.status = options.delete(:status) || 302
- response['location'] = url
- response['content-length'] ||= 0
- flash.update options
- true
+ response.redirect_to url, options
end
# Returns the RELATIVE url for methods in THIS controller (i.e.: "/path_to_controller/restful/params?non=restful¶ms=foo")
#
# accepts one parameter:
@@ -106,26 +101,26 @@
def url_for dest = nil
self.class.url_for dest
end
# same as #url_for, but returns the full URL (protocol:port:://host/path?params=foo)
def full_url_for dest
- request.base_url + url_for(dest)
+ "#{request.base_url}#{self.class.url_for(dest)}"
end
# Send raw data to be saved as a file or viewed as an attachment. Browser should believe it had recieved a file.
#
- # this is usful for sending 'attachments' (data to be downloaded) rather then
+ # this is useful for sending 'attachments' (data to be downloaded) rather then
# a regular response.
#
- # this is also usful for offering a file name for the browser to "save as".
+ # this is also useful for offering a file name for the browser to "save as".
#
# it accepts:
# data:: the data to be sent - this could be a String or an open File handle.
# options:: a hash of any of the options listed furtheron.
#
# the :symbol=>value options are:
- # type:: the type of the data to be sent. defaults to empty. if :filename is supplied, an attempt to guess will be made.
+ # type:: the mime-type of the data to be sent. defaults to empty. if :filename is supplied, an attempt to guess will be made.
# inline:: sets the data to be sent an an inline object (to be viewed rather then downloaded). defaults to false.
# filename:: sets a filename for the browser to "save as". defaults to empty.
#
def send_data data, options = {}
raise 'Cannot use "send_data" after headers were sent' if response.headers_sent?
@@ -142,19 +137,19 @@
response['content-type'] = (options[:type] ||= options[:filename] && MimeTypeHelper::MIME_DICTIONARY[::File.extname(options[:filename])])
response['content-disposition'] = content_disposition
true
end
- # Renders a template file (.slim/.erb/.haml) or an html file (.html) to text and attempts to set the response's 'content-type' header (if it's still empty).
+ # Renders a template file (.slim/.erb/.haml) to a String and attempts to set the response's 'content-type' header (if it's still empty).
#
# For example, to render the file `body.html.slim` with the layout `main_layout.html.haml`:
# render :body, layout: :main_layout
#
# or, for example, to render the file `json.js.slim`
- # render :json, type: 'js'
+ # render :json, format: 'js'
#
# or, for example, to render the file `template.haml`
- # render :template, type: ''
+ # render :template, format: ''
#
# template:: a Symbol for the template to be used.
# options:: a Hash for any options such as `:layout` or `locale`.
# block:: an optional block, in case the template has `yield`, the block will be passed on to the template and it's value will be used inplace of the yield statement.
#