# encoding: UTF-8 # Register ODS format unless is already set Mime::Type.register("application/vnd.oasis.opendocument.spreadsheet", :ods) unless defined? Mime::ODS module ActiveList class OpenDocumentSpreadsheetExporter < ActiveList::Exporter DATE_ELEMENTS = { "m" => "", "d" => "", "Y" => "" } def file_extension "ods" end def mime_type Mime::ODS end def send_data_code(table) xml_escape = "to_s.gsub('&', '&').gsub('\\'', ''').gsub('<', '<').gsub('>', '>')" xml_escape << ".force_encoding('US-ASCII')" if xml_escape.respond_to?(:force_encoding) record = "r" code = table.select_data_code(:paginate => false) code << "name = #{table.model.name}.model_name.human.gsub(/[^a-z0-9]/i,'_')\n" code << "temporary_dir = ::Rails.root.join('tmp', 'active-list')\n" code << "FileUtils.mkdir_p(temporary_dir)\n" code << "file = temporary_dir.join(name+rand.to_s+'.#{self.file_extension}')\n" code << "Zip::ZipOutputStream.open(file) do |zile|\n" # MimeType in first place code << " zile.put_next_entry('mimetype', nil, nil, Zip::ZipEntry::STORED)\n" code << " zile << '#{self.mime_type}'\n" # Manifest code << " zile.put_next_entry('META-INF/manifest.xml')\n" code << " zile << ('')\n" code << " zile.put_next_entry('content.xml')\n" code << " zile << ('')\n" # Styles code << " zile << ('"+ ""+ ""+ ""+::I18n.translate("date.formats.default").gsub(/\%./){|x| ""+DATE_ELEMENTS[x[1..1]]+""} +""+ "')\n" # Tables code << " zile << ('')\n" code << " zile << ('')\n" code << " zile << ('"+columns_headers(table).collect{|h| "'+(#{h}).#{xml_escape}+'"}.join+"')\n" code << " for #{record} in #{table.records_variable_name}\n" code << " zile << ('"+table.exportable_columns.collect do |column| "'+("+column.exporting_datum_code(record, true)+").#{xml_escape}+'" end.join+"')\n" code << " end\n" code << " zile << ('')\n" code << "end\n" code << "send_file(file, :stream=>false, :type=>#{self.mime_type.to_s.inspect}, :disposition=>'inline', :filename=>name+'.#{self.file_extension}')\n" code << "File.delete(file)\n" # Removes tmp files before they explode the disk # raise code return code end end end ActiveList.register_exporter(:ods, ActiveList::OpenDocumentSpreadsheetExporter)