Skip to content

Instantly share code, notes, and snippets.

@chad
Forked from jcasimir/exporter.rb
Created February 26, 2012 18:57
Show Gist options
  • Save chad/1918269 to your computer and use it in GitHub Desktop.
Save chad/1918269 to your computer and use it in GitHub Desktop.
Export ActiveRecord Tables to CSV
require 'csv'
module Exporter
DEFAULT_EXPORT_TABLES = [ Invoice, InvoiceItem, Item, Merchant, Transaction, User ]
DESTINATION_FOLDER = "tmp/"
def self.included(klass)
klass.extend ClassLevelMethods
end
def self.export_tables_to_csv(tables = DEFAULT_EXPORT_TABLES)
tables.each do |klass|
klass.send(:include, self)
klass.export_table_to_csv
end
end
def data
self.class.column_names.map { |column| send(column) }
end
module ClassLevelMethods
def export_table_to_csv
CSV.open(filename_for_class, "w") do |output_file|
output_file << column_names
data.each{ |row| output_file << row }
end
end
def filename_for_class
[DESTINATION_FOLDER, to_s.pluralize.underscore, '.csv'].join
end
def data
all.map(&:data)
end
end
end
@mattetti
Copy link

I very much like the module approach, I would however suggest to not include it in ActiveRecord::Base class but in a AR::Base subclass that needs the features. This way you keep your features only available where you need them on only there.

@ngauthier
Copy link

Or, take the DCI approach and mix it in only when you need it. That way it's not on the object unless you're exporting.

Not exactly sure if this works since it's a class, but the idea is:

task :export_people do
  person_klass = Person.include(Exportable)
  person_klass.export
end

That way the primary Person class does not include Exportable unless the context is appropriate.

@saturnflyer could probably elaborate :-)

@chad
Copy link
Author

chad commented Feb 27, 2012

@mattetti and @ngauthier how about this?

@mattetti
Copy link

it definitely addresses my small complaint, one more pedantic comment would be the naming of def self.export_tables_to_csv(tables = DEFAULT_EXPORT_TABLES). Reading this method, I would expect that the input param to be an array of DB tables. But looking at the constant definition and at the code, the expected param is actually an array of models.
And since I'm at it, I would require activesupport at the top of the file since pluralize and underscore rely on it, I wouldn't call find(:all) but use a batch approach to avoid issues with big DB.

All small details tho.

@chad
Copy link
Author

chad commented Feb 28, 2012

I chose not to change Jeff's original name on the "tables" variable. Probably better named "models".

activesupport would be required implicitly since we're assuming active record models here. We could also do find_in_batches. My changes were almost entirely stylistic. Ultimately the best approach is to mysql the database's dump utility for any of this :)

@mattetti
Copy link

(^_-) very true. This code could also work with other ORMs tho but granted using an ORM for that task seems totally overkill.

@ngauthier
Copy link

Yup, that's why there are task-oriented gems like Taps around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment