40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/rails_admin/support/csv_converter.rb', line 40
def to_csv(options)
options ||= {}
return '' if @objects.blank?
@encoding_from = if [nil, '', 'utf8', 'utf-8', 'UTF8', 'UTF-8'].include?(encoding = Rails.configuration.database_configuration[Rails.env]['encoding'])
'UTF-8'
else
encoding
end
unless options[:encoding_to].blank?
@encoding_to = options[:encoding_to]
unless @encoding_to == @encoding_from
require 'iconv'
@iconv = (Iconv.new("#{@encoding_to}//TRANSLIT//IGNORE", @encoding_from) rescue (Rails.logger.error("Iconv cannot convert to #{@encoding_to}: #{$!}\nNo conversion will take place"); nil))
end
else
@encoding_to = @encoding_from
end
csv_string = CSVClass.generate(options[:generator] ? options[:generator].symbolize_keys.delete_if {|key, value| value.blank? } : {}) do |csv|
unless options[:skip_header]
csv << @fields.map do |field|
output(::I18n.t('admin.export.csv.header_for_root_methods', :name => field.label, :model => @abstract_model.pretty_name))
end +
@associations.map do |association_name, option_hash|
option_hash[:fields].map do |field|
output(::I18n.t('admin.export.csv.header_for_association_methods', :name => field.label, :association => option_hash[:association].label))
end
end.flatten
end
@objects.each do |o|
csv << @fields.map do |field|
output(field.with(:object => o).export_value)
end +
@associations.map do |association_name, option_hash|
associated_objects = [o.send(association_name)].flatten.compact
option_hash[:fields].map do |field|
output(associated_objects.map{ |ao| field.with(:object => ao).export_value.presence || @empty }.join(','))
end
end.flatten
end
end
csv_string = "\xEF\xBB\xBF#{csv_string.respond_to?(:force_encoding) ? csv_string.force_encoding('UTF-8') : csv_string}" if options[:encoding_to] == 'UTF-8'
csv_string = ((@iconv ? @iconv.iconv(csv_string) : csv_string) rescue str) if @encoding_to =~ NON_ASCII_ENCODINGS [!options[:skip_header], @encoding_to, csv_string]
end
|