lib/plezi/handlers/controller_magic.rb in plezi-0.10.17 vs lib/plezi/handlers/controller_magic.rb in plezi-0.11.0

- old
+ new

@@ -11,10 +11,16 @@ # cookies:: a cookie-jar to get and set cookies (set: `cookie\[:name] = data` or get: `cookie\[:name]`). Cookies and some other data must be set BEFORE the response's headers are sent. # flash:: a temporary cookie-jar, good for one request. this is a short-cut for the `response.flash` which handles this magical cookie style. # response:: the HTTPResponse **OR** the WSResponse object that formats the response and sends it. use `response << data`. This object can be used to send partial data (such as headers, or partial html content) in blocking mode as well as sending data in the default non-blocking mode. # host_params:: a copy of the parameters used to create the host and service which accepted the request and created this instance of the controller class. # + # For Controller Class menthods, please read the documentation about {Plezi::ControllerMagic::ClassMethods}. + # + # For Controller Instance methods, please read the documentation about {Plezi::ControllerMagic::InstanceMethods}. + # + # {include: Plezi::ControllerMagic::InstanceMethods} + # module ControllerMagic def self.included base base.send :include, InstanceMethods base.extend ClassMethods end @@ -36,11 +42,11 @@ # Session data can be stored here (session data will be stored on the Redis server, if Redis is available). # # The first time this method is called, the session object will be created. The session object must be created BEFORE the headers are set , if it is to be used. # - # Sessions are not automatically created, because they are memory hogs. The one exception is the Websocket connection that will force a session object into existence. + # Sessions are not automatically created, because they require more resources. The one exception is the Websocket connection that will force a session object into existence, as it's very common to use session data in Websocket connections and the extra connection time is less relevant for a long term connection. def session response.session end # the HTTPResponse **OR** the WSResponse object that formats the response and sends it. use `response << data`. This object can be used to send partial data (such as headers, or partial html content) in blocking mode as well as sending data in the default non-blocking mode. @@ -71,18 +77,18 @@ # 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/) # def redirect_to url, options = {} return super *[] if defined? super - raise 'Cannot redirect after headers were sent' if response.headers_sent? + raise 'Cannot redirect once a Websocket connection was established.' if response.is_a?(::GRHttp::WSEvent) + 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? # redirect response.status = options.delete(:status) || 302 response['Location'] = url response['content-length'] ||= 0 flash.update options - response.finish true end # Returns the RELATIVE url for methods in THIS controller (i.e.: "/path_to_controller/restful/params?non=restful&params=foo") # @@ -121,27 +127,29 @@ # type:: the 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" once a Websocket connection was established.' if response.is_a?(::GRHttp::WSEvent) + # return response.write(data) if response.is_a?(::GRHttp::WSEvent) raise 'Cannot use "send_data" after headers were sent' if response.headers_sent? Plezi.warn 'HTTP response buffer is cleared by `#send_data`' if response.body && response.body.any? && response.body.clear response << data # set headers content_disposition = options[:inline] ? 'inline' : 'attachment' content_disposition << "; filename=#{options[:filename]}" if options[:filename] - response['content-type'] = options[:type] ||= MimeTypeHelper::MIME_DICTIONARY[::File.extname(options[:filename])] if options[:filename] + response['content-type'] = (options[:type] ||= MimeTypeHelper::MIME_DICTIONARY[::File.extname(options[:filename])]) response['content-length'] = data.bytesize rescue true response['content-disposition'] = content_disposition - response.finish true end - # renders a template file (.slim/.erb/.haml) or an html file (.html) to text - # for example, to render the file `body.html.slim` with the layout `main_layout.html.haml`: + # 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). + # + # 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' # @@ -169,17 +177,19 @@ # render layout by recursion, if exists (return render(options.delete(:layout), options) { render template, options, &block }) if options[:layout] # set up defaults options[:type] ||= 'html' options[:locale] ||= params[:locale].to_sym if params[:locale] + #update content-type header + response['content-type'] ||= (MimeTypeHelper::MIME_DICTIONARY[options[:type]] + "; charset=utf-8".freeze) # options[:locals] ||= {} I18n.locale = options[:locale] || I18n.default_locale if defined?(I18n) # sets the locale to nil for default behavior even if the locale was set by a previous action - removed: # && options[:locale] # find template and create template object filename = template.is_a?(String) ? File.join( host_params[:templates].to_s, template) : (File.join( host_params[:templates].to_s, *template.to_s.split('_')) + (options[:type].empty? ? '': ".#{options[:type]}") + '.slim') return ( Plezi.cache_needs_update?(filename) ? Plezi.cache_data( filename, ( Slim::Template.new() { IO.binread filename } ) ) : (Plezi.get_cached filename) ).render(self, &block) if defined?(::Slim) && Plezi.file_exists?(filename) - filename.gsub! /\.slim$/, '.haml' + filename.sub! /\.slim$/, '.haml' return ( Plezi.cache_needs_update?(filename) ? Plezi.cache_data( filename, ( Haml::Engine.new( IO.binread(filename) ) ) ) : (Plezi.get_cached filename) ).render(self, &block) if defined?(::Haml) && Plezi.file_exists?(filename) - filename.gsub! /\.haml$/, '.erb' + filename.sub! /\.haml$/, '.erb' return ( Plezi.cache_needs_update?(filename) ? Plezi.cache_data( filename, ( ERB.new( IO.binread(filename) ) ) ) : (Plezi.get_cached filename) ).result(binding, &block) if defined?(::ERB) && Plezi.file_exists?(filename) return false end # returns the initial method called (or about to be called) by the router for the HTTP request.