lib/csv_rails/array.rb in csv_rails-0.5.1 vs lib/csv_rails/array.rb in csv_rails-0.5.2
- old
+ new
@@ -10,20 +10,26 @@
# ==== Options
# * <tt>:fields</tt> - target field names
# * <tt>:header</tt> - header
# * <tt>:without_header</tt> - total_count
# * <tt>:encoding</tt> - encoding
+ # * <tt>:i18n_scope</tt> - i18n scope
def to_csv(opts={})
fields = opts[:fields]
header = if opts[:header]
opts.delete(:header)
- elsif (klass = first.class).respond_to?(:csv_header)
- klass.csv_header(fields)
+ elsif (klass = first.class).respond_to?(:csv_fields)
+ klass.csv_fields
else
- fields
+ scopes = ['csv_rails']
+ scopes << opts[:i18n_scope] if opts[:i18n_scope]
+ fields.map{|f|
+ defaults = scopes.map{|s| "#{s}.#{f}".to_sym }.push(f.to_s)
+ I18n.t(defaults.shift, :default => defaults)
+ } if fields
end
csv = CSV.generate do |_csv|
- _csv << header unless opts[:without_header]
+ _csv << header if header && !opts[:without_header]
each do |element|
_csv << element.to_csv_ary(fields, opts)
end
end
opts[:encoding] ? csv.encode(opts[:encoding]) : csv