Skip to content

Commit

Permalink
Corrected default encoding names
Browse files Browse the repository at this point in the history
  • Loading branch information
mocoso committed Feb 12, 2009
1 parent 78f8785 commit 735086d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README
Expand Up @@ -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
Expand Down
53 changes: 30 additions & 23 deletions lib/csv_builder.rb
Expand Up @@ -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

Expand Down

0 comments on commit 735086d

Please sign in to comment.