Browse files

Corrected default encoding names

  • Loading branch information...
1 parent 78f8785 commit 735086d44a57698afcde1968ad737d8053eb052e @mocoso mocoso committed Feb 12, 2009
Showing with 32 additions and 25 deletions.
  1. +2 −2 README
  2. +30 −23 lib/csv_builder.rb
View
4 README
@@ -36,9 +36,9 @@ e.g.
You can also set the input encoding and output encoding by setting
<tt>@input_encoding</tt> and <tt>@output_encoding</tt> 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
View
53 lib/csv_builder.rb
@@ -21,43 +21,50 @@ module TemplateHandlers
#
# You can also set the input encoding and output encoding by setting
# <tt>@input_encoding</tt> and <tt>@output_encoding</tt> 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

0 comments on commit 735086d

Please sign in to comment.