From 735086d44a57698afcde1968ad737d8053eb052e Mon Sep 17 00:00:00 2001 From: Joel Chippindale Date: Thu, 12 Feb 2009 15:51:02 +0000 Subject: [PATCH] Corrected default encoding names --- README | 4 ++-- lib/csv_builder.rb | 53 ++++++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/README b/README index d508ff1..d0d1aa5 100644 --- a/README +++ b/README @@ -36,9 +36,9 @@ e.g. You can also set the input encoding and output encoding by setting @input_encoding and @output_encoding instance variables. -These default to 'utf-8' and 'latin1' respectively. e.g. +These default to 'UTF8' and 'LATIN1' respectively. e.g. - @output_encoding = 'utf-8' + @output_encoding = 'UTF8' You can also attach a csv file to mail sent out by your application by including a snippet like the following in your mailer method diff --git a/lib/csv_builder.rb b/lib/csv_builder.rb index 14566b2..5ee35e0 100644 --- a/lib/csv_builder.rb +++ b/lib/csv_builder.rb @@ -21,43 +21,50 @@ module TemplateHandlers # # You can also set the input encoding and output encoding by setting # @input_encoding and @output_encoding instance variables. - # These default to 'utf-8' and 'latin1' respectively. e.g. + # These default to 'UTF8' and 'LATIN1' respectively. e.g. # - # @output_encoding = 'utf-8' - + # @output_encoding = 'UTF8' + class CsvBuilder < TemplateHandler include Compilable def self.line_offset - 7 + 9 end def compile(template) <<-EOV - unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) - @filename ||= "\#{controller.action_name}.csv" - controller.response.headers["Content-Type"] ||= 'text/csv' - controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}" - end + begin - result = FasterCSV.generate do |csv| - #{template.source} - end + unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) + @filename ||= "\#{controller.action_name}.csv" + controller.response.headers["Content-Type"] ||= 'text/csv' + controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}" + end - # Transliterate into the required encoding if necessary - # TODO: make defaults configurable - @input_encoding ||= 'utf-8' - @output_encoding ||= 'latin1' + result = FasterCSV.generate do |csv| + #{template.source} + end - if @input_encoding == @output_encoding - result - else - # TODO: do some checking to make sure iconv works correctly in current environment - # See ActiveSupport::Inflector#transliterate definition for details - Iconv.iconv("\#{@output_encoding}//ignore//translit", @input_encoding, result).to_s - end + # Transliterate into the required encoding if necessary + # TODO: make defaults configurable + @input_encoding ||= 'UTF8' + @output_encoding ||= 'LATIN1' + + if @input_encoding == @output_encoding + result + else + # TODO: do some checking to make sure iconv works correctly in current environment + # See ActiveSupport::Inflector#transliterate definition for details + c = Iconv.new("\#{@output_encoding}//TRANSLIT//IGNORE", @input_encoding) + c.iconv(result) + end + rescue Exception => e + RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV") + raise e + end EOV end