Skip to content

Commit

Permalink
don't rely on the fastercsv gem when ruby >= 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
arydjmal committed Aug 19, 2011
1 parent 07c2238 commit 9dc83a3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion MIT-LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2008-2010 Ary Djmal
Copyright (c) 2008-2011 Ary Djmal

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
17 changes: 6 additions & 11 deletions README.rdoc
Expand Up @@ -12,18 +12,18 @@ This simple plugin gives you the ability to call to_csv to a collection of activ
#

@users.to_csv
@users.to_csv(:only => [:last_name, :role])
@users.to_csv(:only => [:last_name, :role]) # This will also set the order
@users.to_csv(:headers => false)
@users.to_csv(:except => [:last_name, :role])
@users.to_csv(:methods => :admin?)
@users.to_csv({:methods => :admin?}, {:col_sep => '\t'}) # Send csv generation options


== Real life example

In the controller where you want to export to csv, add the format.csv line (as of rails 2.1 it is not necessary to register the csv myme_type)

class UserController < ApplicationController

def index
@users = User.all

Expand All @@ -33,19 +33,14 @@ In the controller where you want to export to csv, add the format.csv line (as o
format.csv { send_data @users.to_csv }
end
end

def show...
def new...
def edit...
def create...
def update...
def destroy...

end


== Dependencies

None if using ruby >= 1.9

Else, install fastercsv:
sudo gem install fastercsv


Expand All @@ -64,4 +59,4 @@ I got ideas and influence from Mike Clarks recipe #35 in Rails Recipes book, som
Does not work on a single activerecord, ie, User.first.to_csv.


Copyright (c) 2008-2010 Ary Djmal, released under the MIT license
Copyright (c) 2008-2011 Ary Djmal, released under the MIT license
3 changes: 0 additions & 3 deletions Rakefile
@@ -1,14 +1,11 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the to_csv plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
7 changes: 6 additions & 1 deletion init.rb
@@ -1,2 +1,7 @@
require 'fastercsv'
if RUBY_VERSION >= "1.9.0"
require 'csv'
else
require 'fastercsv'
end

require 'to_csv'
6 changes: 4 additions & 2 deletions lib/to_csv.rb
@@ -1,5 +1,5 @@
class Array
def to_csv(options = {}, fastercsv_options = {})
def to_csv(options = {}, csv_options = {})
return '' if self.empty?

klass = self.first.class
Expand All @@ -15,7 +15,9 @@ def to_csv(options = {}, fastercsv_options = {})

return '' if columns.empty?

output = FasterCSV.generate(fastercsv_options) do |csv|
writer = RUBY_VERSION >= "1.9.0" ? CSV : FasterCSV

output = writer.generate(csv_options) do |csv|
csv << columns.map { |column| klass.human_attribute_name(column) } unless options[:headers] == false
self.each do |item|
csv << columns.collect { |column| item.send(column) }
Expand Down
8 changes: 6 additions & 2 deletions test/test_helper.rb
@@ -1,12 +1,16 @@
begin
require 'rubygems'
require 'test/unit'
require 'fastercsv'
require 'active_support'
require 'active_support/inflector'
if RUBY_VERSION >= "1.9.0"
require 'csv'
else
require 'fastercsv'
end
require File.dirname(__FILE__) + '/../lib/to_csv'
rescue LoadError
puts 'to_csv tests rely on fastercsv, and active_support'
puts 'to_csv tests rely on active_support, and fastercsv if ruby version < 1.9'
end

class User
Expand Down
2 changes: 1 addition & 1 deletion test/to_csv_test.rb
Expand Up @@ -12,7 +12,7 @@ def test_with_empty_array
assert_equal( "", [].to_csv )
end

def test_with_fastercsv_option
def test_with_csv_options
assert_equal( "Age\tId\tName\n25\t1\tAry\n22\t2\tNati\n", @users.to_csv({}, :col_sep => "\t"))
end

Expand Down

0 comments on commit 9dc83a3

Please sign in to comment.