Sha256: 5da1ddf46a3dab369f79443778dcd85e0cec154142365c825f1f3b86933fbc2a
Contents?: true
Size: 1.2 KB
Versions: 4
Compression:
Stored size: 1.2 KB
Contents
require 'csv' module UsefulRenderers module CsvRenderable # Converts an array to CSV formatted string # Options include: # :only => [:col1, :col2] # Specify which columns to include # :except => [:col1, :col2] # Specify which columns to exclude # :add_methods => [:method1, :method2] # Include addtional methods that aren't columns def to_csv(options = {}) klass = first.class return '' if empty? return join(',') unless klass.respond_to? :column_names columns = klass.column_names if options[:only] columns = [] options[:only].each do |method| columns << method.to_s if first.respond_to?(method) end end columns -= options[:except].map(&:to_s) if options[:except] columns += options[:add_methods].map(&:to_s) if options[:add_methods] headers = columns.dup headers.map!{|col| klass.human_attribute_name col } if options[:translate] csv_options = { encoding: 'utf-8', headers: headers, write_headers: true } CSV.generate(csv_options) do |row| self.each do |obj| row << columns.map { |c| obj.send(c) } end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems