geetarista-export_csv 0.0.3 → 0.0.4
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 +5 -4
- data/lib/export_csv.rb +6 -3
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -8,12 +8,13 @@ The only required parameter is the model name:
|
|
8
8
|
|
9
9
|
export_csv(User)
|
10
10
|
|
11
|
-
The name of the generated file can be overriden by passing it
|
11
|
+
The name of the generated file can be overriden by passing it with the :filename option:
|
12
12
|
|
13
|
-
export_csv(User, "User Backup")
|
13
|
+
export_csv(User, :filename => "User Backup")
|
14
14
|
|
15
|
-
Find options may also be passed
|
15
|
+
Find options for the model may also be passed with :parameters
|
16
|
+
|
17
|
+
export_csv(User, :parameters => { :limit => 20 })
|
16
18
|
|
17
|
-
export_csv(User, "User Backup", { :limit => 20 })
|
18
19
|
|
19
20
|
Copyright (c) 2009 Robby Colvin, released under the MIT license
|
data/lib/export_csv.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
def export_csv(klass,
|
1
|
+
def export_csv(klass, options = {})
|
2
2
|
require 'ruport'
|
3
|
-
|
3
|
+
|
4
|
+
opts = { :filename => klass, :parameters => {} }.merge(options)
|
5
|
+
|
6
|
+
records = klass.find(:all, :parameters => opts[:parameters])
|
4
7
|
headers = klass.columns.collect{ |a| a.name }
|
5
8
|
data = []
|
6
9
|
records.each do |r|
|
@@ -14,5 +17,5 @@ def export_csv(klass, filename=klass, conditions={})
|
|
14
17
|
csv = table.to_csv
|
15
18
|
time = Time.now
|
16
19
|
filename = "#{filename}-#{time.strftime('%m%d%Y-%H%M')}.csv"
|
17
|
-
send_data csv, :filename => filename, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; #{filename}"
|
20
|
+
send_data csv, :filename => opts[:filename], :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; #{filename}"
|
18
21
|
end
|