Sha256: fcf68c6e0d4f61fd189b0781f93e26ef1f7fefb711564ffb390b275e0cf8cf41

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

unless defined? Mime::XLS
  Mime::Type.register "application/vnd.ms-excel", :xls
end

# CloudXLSRails::XLSResponder
module CloudXLSRails
  class XLSResponder
    def self.redirect!(controller, scope, options)
      columns  = options.fetch(:columns, nil)
      xdata = options[:data] || {}
      unless (xdata.has_key?(:text) ||
              xdata.has_key?(:url ) ||
              xdata.has_key?(:file)  )

        xdata[:text] = CloudXLS::CSVWriter.text(scope, {:columns => columns})
      end

      xopts = {:data => xdata}
      xopts[:sheet] = options[:sheet] if options[:sheet]
      xopts[:doc]   = options[:doc]   if options[:doc]

      response = CloudXLS.xpipe(xopts)
      controller.redirect_to response.url
    end
  end
end


# For respond_to default
class ActionController::Responder
  def to_xls
    stream = options.delete(:stream) || false
    if stream # either string or boolean
      options[:data] ||= {}
      options[:data][:url] ||= cloudxls_stream_url(stream, 'xls')
    end
    CloudXLSRails::XLSResponder.redirect!(controller, resources.last, options)
  end

protected
  def cloudxls_stream_url(stream, extension = 'xls')
    if stream == true
      controller.request.url.gsub(/#{extension}\Z/, "csv")
    else
      stream.to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudxls-rails-0.4.3 lib/cloudxls-rails/handlers/xls.rb