Module: ActiveRecord::Extensions::FindToCSV::ArrayInstanceMethods

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

Overview

:nodoc:

Defined Under Namespace

Classes: NoRecordsError

Instance Method Summary collapse

Instance Method Details

#to_csv(options = {}) ⇒ Object

Returns CSV data with headers for an array of ActiveRecord::Base model objects by iterating over them and calling to_csv with the passed in options.



297
298
299
300
301
302
303
304
305
# File 'lib/ar-extensions/csv.rb', line 297

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

#to_csv_data(options = {}) ⇒ Object

Returns CSV data without headers for an array of ActiveRecord::Base model objects by iterating over them and calling to_csv_data with the passed in options.



288
289
290
291
292
# File 'lib/ar-extensions/csv.rb', line 288

def to_csv_data( options={} )
  inject( [] ) do |arr,model_instance|
    arr.push( *model_instance.to_csv_data( options ) )
  end
end

#to_csv_headers(options = {}) ⇒ Object

Returns CSV headers for an array of ActiveRecord::Base model objects by calling to_csv_headers on the first element.



281
282
283
# File 'lib/ar-extensions/csv.rb', line 281

def to_csv_headers( options={} ) 
  first.class.to_csv_headers( options )
end