active_record_to_csv 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -29,13 +29,23 @@ Note that #to_csv is called like a scope or query. The following will NOT give y
29
29
 
30
30
  This will use Ruby's Array#to_csv method.
31
31
 
32
+ === Attribute#to_csv
33
+
34
+ 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:
35
+
36
+ class Date
37
+ def to_csv
38
+ strftime('%Y/%m/%d')
39
+ end
40
+ end
41
+
32
42
  == TODO
33
43
 
34
44
  Options to specify columns to be included (currently, id and timestamp columns are excluded).
35
45
 
36
46
  == Compatibility
37
47
 
38
- Tested with ActiveRecord v3.0.4
48
+ Tested with ActiveRecord v3.0.5
39
49
 
40
50
  http://gem-testers.org/gems/active_record_to_csv
41
51
 
@@ -9,7 +9,9 @@ module ActiveRecordToCSV
9
9
  header_row = csv_columns.to_csv
10
10
  records_rows = all.map do |record|
11
11
  csv_columns.map do |column|
12
- record[column]
12
+ value = record[column]
13
+ value = value.to_csv if value.respond_to?(:to_csv)
14
+ value
13
15
  end.to_csv
14
16
  end.join
15
17
  header_row + records_rows
@@ -27,4 +29,3 @@ class ActiveRecord::Relation
27
29
  end
28
30
  end
29
31
  end
30
-
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordToCsv
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -10,7 +10,11 @@ describe ActiveRecordToCSV do
10
10
  "The King's Speech",
11
11
  'The Kids Are All Right'
12
12
  ].each_with_index do |title, i|
13
- Movie.create! :title => title, :director_id => i
13
+ Movie.create!(
14
+ :title => title,
15
+ :director_id => i + 1,
16
+ :released_on => Date.new(2010, 1, i + 1)
17
+ )
14
18
  end
15
19
  end
16
20
 
@@ -23,7 +27,15 @@ describe ActiveRecordToCSV do
23
27
  end
24
28
 
25
29
  it 'should not include id and timestamp fields' do
26
- Movie.to_csv.lines.first.should eq("title,director_id\n")
30
+ header = Movie.to_csv.lines.first
31
+ header.should_not =~ /^id|created_at|updated_at/
32
+ end
33
+
34
+ it 'should call to_csv on each attribute if it responds to it' do
35
+ date = Movie.limit(1).first.released_on
36
+ csv_string = Movie.limit(1).to_csv
37
+ csv_string.should include(date.to_csv)
38
+ csv_string.should_not include(date.to_s)
27
39
  end
28
40
 
29
41
  end
data/spec/schema.rb CHANGED
@@ -6,6 +6,7 @@ ActiveRecord::Schema.define do
6
6
  create_table :movies, :force => true do |t|
7
7
  t.string :title
8
8
  t.integer :director_id
9
+ t.date :released_on
9
10
  t.timestamps
10
11
  end
11
12
  end
@@ -1,6 +1,6 @@
1
- title,director_id
2
- Black Swan,0
3
- Inception,1
4
- The Fighter,2
5
- The King's Speech,3
6
- The Kids Are All Right,4
1
+ title,director_id,released_on
2
+ Black Swan,1,2010/01/01
3
+ Inception,2,2010/01/02
4
+ The Fighter,3,2010/01/03
5
+ The King's Speech,4,2010/01/04
6
+ The Kids Are All Right,5,2010/01/05
@@ -1,2 +1,2 @@
1
- title,director_id
2
- The Kids Are All Right,4
1
+ title,director_id,released_on
2
+ The Kids Are All Right,5,2010/01/05
@@ -0,0 +1,5 @@
1
+ class Date
2
+ def to_csv
3
+ strftime('%Y/%m/%d')
4
+ end
5
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: active_record_to_csv
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jared Ning
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-01 00:00:00 -06:00
13
+ date: 2011-03-02 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -71,6 +71,7 @@ files:
71
71
  - spec/support/fixtures/movies.csv
72
72
  - spec/support/fixtures/movies_bad.csv
73
73
  - spec/support/macros.rb
74
+ - spec/support/models/date.rb
74
75
  - spec/support/models/movie.rb
75
76
  has_rdoc: true
76
77
  homepage: https://github.com/ordinaryzelig/active_record_to_csv
@@ -107,4 +108,5 @@ test_files:
107
108
  - spec/support/fixtures/movies.csv
108
109
  - spec/support/fixtures/movies_bad.csv
109
110
  - spec/support/macros.rb
111
+ - spec/support/models/date.rb
110
112
  - spec/support/models/movie.rb