Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 1eb2009af3
Fetching contributors…

Cannot retrieve contributors at this time

28 lines (21 sloc) 0.655 kB
class Array
def to_csv(options = {})
return '' if self.empty?
klass = self.first.class
attributes = self.first.attributes.keys.sort.map(&:to_sym)
if options[:only]
columns = Array(options[:only]) & attributes
else
columns = attributes - Array(options[:except])
end
columns += Array(options[:methods])
return '' if columns.empty?
output = CSV.generate do |csv|
csv << columns.map { |column| klass.human_attribute_name(column) } unless options[:headers] == false
self.each do |item|
csv << columns.collect { |column| item.send(column) }
end
end
output
end
end
Jump to Line
Something went wrong with that request. Please try again.