geetarista-export_csv 0.0.4 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +3 -3
- data/lib/export_csv.rb +3 -3
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
== export_csv
|
2
2
|
|
3
|
-
Very simple tool to export all records of an ActiveRecord model.
|
3
|
+
Very simple tool to export all records of an ActiveRecord model to a CSV file.
|
4
4
|
|
5
5
|
== Examples
|
6
6
|
|
@@ -10,11 +10,11 @@ The only required parameter is the model name:
|
|
10
10
|
|
11
11
|
The name of the generated file can be overriden by passing it with the :filename option:
|
12
12
|
|
13
|
-
export_csv(User, :filename => "
|
13
|
+
export_csv(User, :filename => "People")
|
14
14
|
|
15
15
|
Find options for the model may also be passed with :parameters
|
16
16
|
|
17
|
-
export_csv(User, :parameters => { :limit => 20 })
|
17
|
+
export_csv(User, :parameters => { :limit => 20, :order => "name asc" })
|
18
18
|
|
19
19
|
|
20
20
|
Copyright (c) 2009 Robby Colvin, released under the MIT license
|
data/lib/export_csv.rb
CHANGED
@@ -3,7 +3,7 @@ def export_csv(klass, options = {})
|
|
3
3
|
|
4
4
|
opts = { :filename => klass, :parameters => {} }.merge(options)
|
5
5
|
|
6
|
-
records = klass.find(:all,
|
6
|
+
records = klass.find(:all, opts[:parameters])
|
7
7
|
headers = klass.columns.collect{ |a| a.name }
|
8
8
|
data = []
|
9
9
|
records.each do |r|
|
@@ -16,6 +16,6 @@ def export_csv(klass, options = {})
|
|
16
16
|
table = Ruport::Data::Table(:column_names => headers.collect{ |h| h.humanize }, :data => data)
|
17
17
|
csv = table.to_csv
|
18
18
|
time = Time.now
|
19
|
-
filename = "#{filename}-#{time.strftime('%m%d%Y-%H%M')}.csv"
|
20
|
-
send_data csv, :filename =>
|
19
|
+
filename = "#{opts[:filename]}-#{time.strftime('%m%d%Y-%H%M')}.csv"
|
20
|
+
send_data csv, :filename => filename, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; #{filename}"
|
21
21
|
end
|