lib/plezi/handlers/controller_magic.rb in plezi-0.12.1 vs lib/plezi/handlers/controller_magic.rb in plezi-0.12.2

- old
+ new

@@ -81,11 +81,11 @@ 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? # redirect response.status = options.delete(:status) || 302 - response['Location'] = url + response['location'] = url response['content-length'] ||= 0 flash.update options true end @@ -117,28 +117,30 @@ # a regular response. # # this is also usful for offering a file name for the browser to "save as". # # it accepts: - # data:: the data to be sent + # 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. # 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? - Plezi.warn 'HTTP response buffer is cleared by `#send_data`' if response.body && response.body.any? && response.body.clear - response << data + if response.body && response.body.any? + Plezi.warn 'existing response body was cleared by `#send_data`!' + response.body.close if response.body.respond_to? :close + end + response = data # set headers content_disposition = options[:inline] ? 'inline' : 'attachment' - content_disposition << "; filename=#{options[:filename]}" if options[:filename] + content_disposition << "; filename=#{::File.basename(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 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).