module Sinatra::ReportHandler
Public Instance Methods
enable_reports()
click to toggle source
# File lib/sinatra/handlers/reports.rb, line 6 def enable_reports p "Enabling reports..." #In order to enable file streaming helpers do def slim(template, options = {}, locals = {}, &block) # Slim calls the option :streaming, but Sinatra has a #stream method # so we also support the :stream option. options[:streaming] ||= options.delete(:stream) # We are not streaming. Call super implementation and # just ensure that the @_out_buf is restored afterwards. unless options[:streaming] old = @_out_buf begin return super ensure @_out_buf = old end end # We use the Temple generator without preamble and postamble # which is suitable for streaming. options[:generator] = Temple::Generator # We are already streaming, continue! return super if @_out_buf.is_a? Sinatra::Helpers::Stream # Create a new stream stream do |out| @_out_buf = out if options[:layout] == false # No layout given, start rendering template super else # Layout given layout = options[:layout] == nil || options[:layout] == true ? :layout : options[:layout] # Invert layout and template rendering order super layout, options.merge(layout: false), locals do super(template, options.merge(layout: false), locals, &block) end end end end end before '/reports/*' do error(403) unless authenticated(User).admin? end get '/reports' do slim :reports end get '/reports/:report_name' do |report_name| date = Date.parse(params[:date]) unless params[:date].nil? date ||= Date.today headers "Content-Disposition" => "attachment;filename=#{report_name}.#{date}.xls", "Content-Type" => "application/octet-stream" slim "reports/#{report_name}".to_sym, locals: { :date => date }, :stream => true, :layout => false end end
slim(template, options = {}, locals = {}, &block)
click to toggle source
Calls superclass method
# File lib/sinatra/handlers/reports.rb, line 11 def slim(template, options = {}, locals = {}, &block) # Slim calls the option :streaming, but Sinatra has a #stream method # so we also support the :stream option. options[:streaming] ||= options.delete(:stream) # We are not streaming. Call super implementation and # just ensure that the @_out_buf is restored afterwards. unless options[:streaming] old = @_out_buf begin return super ensure @_out_buf = old end end # We use the Temple generator without preamble and postamble # which is suitable for streaming. options[:generator] = Temple::Generator # We are already streaming, continue! return super if @_out_buf.is_a? Sinatra::Helpers::Stream # Create a new stream stream do |out| @_out_buf = out if options[:layout] == false # No layout given, start rendering template super else # Layout given layout = options[:layout] == nil || options[:layout] == true ? :layout : options[:layout] # Invert layout and template rendering order super layout, options.merge(layout: false), locals do super(template, options.merge(layout: false), locals, &block) end end end end