lib/plezi/handlers/controller_magic.rb in plezi-0.11.2 vs lib/plezi/handlers/controller_magic.rb in plezi-0.12.0

- old
+ new

@@ -40,11 +40,11 @@ # Cookies and some other data must be set BEFORE the response's headers are sent. attr_reader :cookies # 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. + # The first time this method is called, the `n 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 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 @session ||= response.session end @@ -54,11 +54,11 @@ # the :flash is a little bit of a magic hash that sets and reads temporary cookies. # these cookies will live for one successful request to a Controller and will then be removed. attr_reader :flash - # the parameters used to create the host (the parameters passed to the `listen` / `add_service` call). + # the parameters used to create the host (the parameters passed to the `Plezi.host`). attr_reader :host_params # this method does two things. # # 1. sets redirection headers for the response. @@ -77,11 +77,10 @@ # 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 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 @@ -127,12 +126,10 @@ # 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 @@ -178,11 +175,16 @@ (return render(options.delete(:layout), options) { render template, options, &block }) if options[:layout] # set up defaults options[:type] ||= 'html'.freeze options[:locale] ||= params[:locale].to_sym if params[:locale] #update content-type header - response['content-type'] ||= "#{MimeTypeHelper::MIME_DICTIONARY[".#{options[:type]}".freeze]}; charset=utf-8".freeze + case options[:type] + when 'html', 'js', 'txt' + response['content-type'] ||= "#{MimeTypeHelper::MIME_DICTIONARY[".#{options[:type]}".freeze]}; charset=utf-8".freeze + else + response['content-type'] ||= "#{MimeTypeHelper::MIME_DICTIONARY[".#{options[:type]}".freeze]}".freeze + end # Circumvents I18n persistance issues (live updating and thread data storage). 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]}")) ::Plezi::Renderer.render filename, binding, &block @@ -199,22 +201,22 @@ # (which is the last method called before the protocol is switched from HTTP to WebSockets). def requested_method # respond to websocket special case return :pre_connect if request.upgrade? # respond to save 'new' special case - return (self.class.has_method?(:save) ? :save : false) if request.request_method.match(/POST|PUT|PATCH/) && (params[:id].nil? || params[:id] == 'new') + return (self.class.has_method?(:save) ? :save : false) if (request.request_method =~ /POST|PUT|PATCH/i.freeze) && (params[:id].nil? || params[:id] == 'new') # set DELETE method if simulated request.request_method = 'DELETE' if params[:_method].to_s.downcase == 'delete' # respond to special :id routing return params[:id].to_s.to_sym if params[:id] && self.class.has_exposed_method?(params[:id].to_s.to_sym) #review general cases case request.request_method - when 'GET', 'HEAD' + when 'GET'.freeze, 'HEAD'.freeze return (self.class.has_method?(:index) ? :index : false) unless params[:id] return (self.class.has_method?(:show) ? :show : false) - when 'POST', 'PUT', 'PATCH' + when 'POST'.freeze, 'PUT'.freeze, 'PATCH'.freeze return (self.class.has_method?(:update) ? :update : false) - when 'DELETE' + when 'DELETE'.freeze return (self.class.has_method?(:delete) ? :delete : false) end false end end