Module: ActiveRecord::Extensions::FindToCSV::InstanceMethods

Defined in:
lib/ar-extensions/csv.rb

Instance Method Summary collapse

Instance Method Details

#to_csv(options = {}) ⇒ Object

Returns CSV data including header rows for the passed in options.



265
266
267
268
269
270
271
# File 'lib/ar-extensions/csv.rb', line 265

def to_csv( options={} )
  FasterCSV.generate do |csv|
    headers = self.class.to_csv_headers( options )
    csv << headers if headers
    to_csv_data( options ).each{ |data| csv << data }
  end
end

#to_csv_data(options = {}) ⇒ Object

Returns CSV data without any header rows for the passed in options.



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ar-extensions/csv.rb', line 251

def to_csv_data( options={} )
  fields = self.class.to_csv_fields( options ).fields
  data, model_data = [], fields.inject( [] ) { |arr,field| arr << attributes[field].to_s }
  if options[:include]
    to_csv_data_for_included_associations( options[:include ] ).map do |assoc_csv_data|
      data << model_data + assoc_csv_data
    end
  else
    data << model_data
  end
  data
end