GitXplorerGitXplorer
o

active_record_to_csv

public
3 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
86ae691f300012e876524c02773f3527cf2ba220

Use object.send instead of object[].

oordinaryzelig committed 14 years ago
Unverified
7de1b3dad06e06b1669ac0b2da7ec5f5b4d08825

Call to_csv on attribute if possible.

oordinaryzelig committed 14 years ago
Unverified
4f7dc93b612ec574af01631be82aaae6a6b376e2

Moved require active_record to better place.

oordinaryzelig committed 14 years ago
Unverified
9e987b23cbaf3fc657981ff9e753626940a392d8

tested with gem-testers.org.

oordinaryzelig committed 14 years ago
Unverified
a87e64785d36e73f3afb7efa10baa970e672c4fe

Added README.rdoc.

oordinaryzelig committed 14 years ago
Unverified
8591e61682aea2d974626d64b6b0d314cadeba52

First commit

oordinaryzelig committed 14 years ago

README

The README file for this repository.

= ActiveRecord.to_csv

== Description

A simple ActiveRecord::Base to_csv() class method that preserves scopes. to_csv() returns the entire contents including the header ready to be written to file.

== Usage

Assuming a Movie model with title and director_id columns.

Movie.to_csv

would return:

title,director_id title,director_id Black Swan,0 Inception,1 The Fighter,2 The King's Speech,3 The Kids Are All Right,4

Movie.bad.to_csv

would return:

title,director_id The Kids Are All Right,4

Note that #to_csv is called like a scope or query. The following will NOT give you the same results:

Movie.all.to_csv

This will use Ruby's Array#to_csv method.

=== Attribute#to_csv

After a model object's attributes are collected, to_csv is called on the resulting array. However, this poses a problem because it will blindly convert the attributes to a string -- i.e. call to_s on them. If one of your attributes is a Date, then calling to_s may produce unwanted output. For example, if you have Date::DATE_FORMATS[:default] = '%d %B, %Y' your dates will have the month written out like 'January', 'February', etc. To counter this, this gem will make an attempt to call to_csv() on each attribute. To get YYYY-MM-DD output, you could do something like:

class Date def to_csv strftime('%Y-%m-%d') end end

Note that object.send(attribute_name) is used, so datetime fields will be returned as ActiveSupport::TimeWithZone objects.

== TODO

Options to specify columns to be included (currently, id and timestamp columns are excluded).

== Compatibility

Tested with ActiveRecord v3.0.5

http://gem-testers.org/gems/active_record_to_csv

== Related gems