GitXplorerGitXplorer
o

mongoid_to_csv

public
4 stars
8 forks
0 issues

Commits

List of commits on branch master.
Unverified
bf5001f30a4b2ecc843f635c4fe1072fd72d68ac

Change to Array#mongoid_to-csv. Works for empty array too.

oordinaryzelig committed 13 years ago
Unverified
90b50df68fba1fc2fe62f793a2f4ed77baf68e4f

Go back to Array#to_mongoid_csv.

oordinaryzelig committed 13 years ago
Unverified
995730e190374bbe5e33fd0b9587b2ca8c61dddc

Remove fastercsv as gem dependency.

oordinaryzelig committed 13 years ago
Unverified
d405f3e0ae6f7e609ed316a5955ff795d34ba751

Fix gemspec.

oordinaryzelig committed 13 years ago
Unverified
371aaa3870c8f252ca69f415fb8ee4b131bbdce2

Implement Array#to_csv for mongoid documents.

oordinaryzelig committed 13 years ago
Unverified
b159bb6f50b3ff37440fee0be82dee4d9da859c7

Bump version

oordinaryzelig committed 13 years ago

README

The README file for this repository.

= Mongoid.to_csv

== Description

A simple Mongoid::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

For normal arrays, to_mongoid_csv works the same way.

Movie.all.mongoid_to_csv

Same as

Movie.to_csv

=== Why not Array.to_csv?

I tried.

require 'csv' module ArrayToCSV def to_csv if first.is_a?(Mongoid::Document) MongoidToCSV.documents_to_csv(self) else super end end end Array.send :include, ArrayToCSV

Something is giving ruby's internal Array#to_csv precedence and ignoring mine. Got tired of messing around with it.

=== 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).
  • Combine with active_record_to_csv somehow since they are essentially doing the same thing.

== Compatibility

Tested with Ruby 1.9.2-p318 and Mongoid v2.0.2

If you are using a lower version of Ruby 1.9.2-p318 (the p318 is important), you need to install and require the faster_csv gem. This was tested with faster_csv v1.5.4.

== Related gems