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 +11 -1
- data/lib/active_record_to_csv.rb +3 -2
- data/lib/active_record_to_csv/version.rb +1 -1
- data/spec/active_record_to_csv_spec.rb +14 -2
- data/spec/schema.rb +1 -0
- data/spec/support/fixtures/movies.csv +6 -6
- data/spec/support/fixtures/movies_bad.csv +2 -2
- data/spec/support/models/date.rb +5 -0
- metadata +4 -2
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.
|
48
|
+
Tested with ActiveRecord v3.0.5
|
39
49
|
|
40
50
|
http://gem-testers.org/gems/active_record_to_csv
|
41
51
|
|
data/lib/active_record_to_csv.rb
CHANGED
@@ -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
|
-
|
@@ -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!
|
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
|
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
@@ -1,6 +1,6 @@
|
|
1
|
-
title,director_id
|
2
|
-
Black Swan,
|
3
|
-
Inception,
|
4
|
-
The Fighter,
|
5
|
-
The King's Speech,
|
6
|
-
The Kids Are All Right,
|
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,
|
1
|
+
title,director_id,released_on
|
2
|
+
The Kids Are All Right,5,2010/01/05
|
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.
|
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-
|
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
|