did_csv_builder 0.1.4.beta
Sign up to get free protection for your applications and to get access to all the features.
- data/README +58 -0
- data/lib/csv_builder.rb +12 -0
- data/lib/csv_builder/action_controller.rb +22 -0
- data/lib/csv_builder/template_handler/base.rb +154 -0
- metadata +73 -0
data/README
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= CSV Builder
|
2
|
+
|
3
|
+
The CSV Builder Rails plugin provides a simple templating system for serving
|
4
|
+
dynamically generated CSV files from your application.
|
5
|
+
|
6
|
+
|
7
|
+
== Requirements
|
8
|
+
|
9
|
+
CSV Builder requires Rails v2.1.
|
10
|
+
|
11
|
+
It also depends upon the FasterCSV gem http://fastercsv.rubyforge.org,
|
12
|
+
which you can install with
|
13
|
+
|
14
|
+
$ sudo gem install fastercsv
|
15
|
+
|
16
|
+
|
17
|
+
== Example
|
18
|
+
|
19
|
+
CSV template files are suffixed with '.csv.csvbuilder', for example
|
20
|
+
'index.csv.csvbuilder'
|
21
|
+
|
22
|
+
Add rows to your CSV file in the template by pushing arrays of columns into the
|
23
|
+
csv object.
|
24
|
+
|
25
|
+
# First row
|
26
|
+
csv << [ 'cell 1', 'cell 2' ]
|
27
|
+
# Second row
|
28
|
+
csv << [ 'another cell value', 'and another' ]
|
29
|
+
# etc...
|
30
|
+
|
31
|
+
You can set the default filename for that a browser will use for 'save as' by
|
32
|
+
setting <tt>@filename</tt> instance variable in your controller's action method
|
33
|
+
e.g.
|
34
|
+
|
35
|
+
@filename = 'report.csv'
|
36
|
+
|
37
|
+
You can also set the input encoding and output encoding by setting
|
38
|
+
<tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
39
|
+
These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
40
|
+
|
41
|
+
@output_encoding = 'UTF-8'
|
42
|
+
|
43
|
+
You can also attach a csv file to mail sent out by your application by
|
44
|
+
including a snippet like the following in your mailer method
|
45
|
+
|
46
|
+
attachment "text/csv" do |attachment|
|
47
|
+
attachment.body = render(:file => 'example/index.csv.csvbuilder')
|
48
|
+
attachment.filename = 'report.csv'
|
49
|
+
end
|
50
|
+
|
51
|
+
You can change the separator character and that is as simple as writing this in your controller's
|
52
|
+
action method:
|
53
|
+
|
54
|
+
csv_builder :sep => ';'
|
55
|
+
|
56
|
+
Actually, all default FasterCSV options can be overriden thanks to the previous statement.
|
57
|
+
|
58
|
+
Copyright (c) 2008 Econsultancy.com, released under the MIT license
|
data/lib/csv_builder.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
require 'action_view'
|
3
|
+
|
4
|
+
require 'fastercsv'
|
5
|
+
require 'iconv'
|
6
|
+
|
7
|
+
require 'csv_builder/action_controller'
|
8
|
+
require 'csv_builder/template_handler/base'
|
9
|
+
|
10
|
+
class ActionController::Base
|
11
|
+
include CsvBuilder::ActionController
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module CsvBuilder
|
2
|
+
module ActionController
|
3
|
+
|
4
|
+
DEFAULT_CSV_BUILDER_OPTIONS = { :col_sep => ';' }
|
5
|
+
|
6
|
+
def csv_builder(options)
|
7
|
+
@csv_builder_options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def compute_csv_builder_options
|
13
|
+
options = DEFAULT_CSV_BUILDER_OPTIONS.dup
|
14
|
+
options.merge!(@csv_builder_options || {})
|
15
|
+
options
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module CsvBuilder
|
2
|
+
module TemplateHandler
|
3
|
+
|
4
|
+
# Template handler for csv templates
|
5
|
+
#
|
6
|
+
# Add rows to your CSV file in the template by pushing arrays of columns into csv
|
7
|
+
#
|
8
|
+
# # First row
|
9
|
+
# csv << [ 'cell 1', 'cell 2' ]
|
10
|
+
# # Second row
|
11
|
+
# csv << [ 'another cell value', 'and another' ]
|
12
|
+
# # etc...
|
13
|
+
#
|
14
|
+
# You can set the default filename for that a browser will use for 'save as' by
|
15
|
+
# setting <tt>@filename</tt> instance variable in your controller's action method
|
16
|
+
# e.g.
|
17
|
+
#
|
18
|
+
# @filename = 'report.csv'
|
19
|
+
#
|
20
|
+
# You can also set the input encoding and output encoding by setting
|
21
|
+
# <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
22
|
+
# These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
23
|
+
#
|
24
|
+
# @output_encoding = 'UTF-8'
|
25
|
+
|
26
|
+
class Base < ActionView::TemplateHandler
|
27
|
+
|
28
|
+
include ActionView::TemplateHandlers::Compilable
|
29
|
+
|
30
|
+
def self.line_offset
|
31
|
+
9
|
32
|
+
end
|
33
|
+
|
34
|
+
def compile(template)
|
35
|
+
<<-EOV
|
36
|
+
begin
|
37
|
+
|
38
|
+
unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
|
39
|
+
@filename ||= "\#{controller.action_name}.csv"
|
40
|
+
controller.response.headers["Content-Type"] ||= 'text/csv'
|
41
|
+
controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}"
|
42
|
+
end
|
43
|
+
|
44
|
+
result = FasterCSV.generate(controller.send(:compute_csv_builder_options)) do |csv|
|
45
|
+
#{template.source}
|
46
|
+
end
|
47
|
+
|
48
|
+
# Transliterate into the required encoding if necessary
|
49
|
+
# TODO: make defaults configurable
|
50
|
+
@input_encoding ||= 'UTF-8'
|
51
|
+
@output_encoding ||= 'LATIN1'
|
52
|
+
|
53
|
+
if @input_encoding == @output_encoding
|
54
|
+
result
|
55
|
+
else
|
56
|
+
# TODO: do some checking to make sure iconv works correctly in
|
57
|
+
# current environment. See ActiveSupport::Inflector#transliterate
|
58
|
+
# definition for details
|
59
|
+
#
|
60
|
+
# Not using the more standard //IGNORE//TRANLIST because it raises
|
61
|
+
# Iconv::IllegalSequence for some inputs
|
62
|
+
c = Iconv.new("\#{@output_encoding}//TRANSLIT//IGNORE", @input_encoding)
|
63
|
+
c.iconv(result)
|
64
|
+
end
|
65
|
+
|
66
|
+
rescue Exception => e
|
67
|
+
RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
|
68
|
+
raise e
|
69
|
+
end
|
70
|
+
EOV
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
module ActionView # :nodoc:
|
80
|
+
module TemplateHandlers
|
81
|
+
# Template handler for csv templates
|
82
|
+
#
|
83
|
+
# Add rows to your CSV file in the template by pushing arrays of columns into csv
|
84
|
+
#
|
85
|
+
# # First row
|
86
|
+
# csv << [ 'cell 1', 'cell 2' ]
|
87
|
+
# # Second row
|
88
|
+
# csv << [ 'another cell value', 'and another' ]
|
89
|
+
# # etc...
|
90
|
+
#
|
91
|
+
# You can set the default filename for that a browser will use for 'save as' by
|
92
|
+
# setting <tt>@filename</tt> instance variable in your controller's action method
|
93
|
+
# e.g.
|
94
|
+
#
|
95
|
+
# @filename = 'report.csv'
|
96
|
+
#
|
97
|
+
# You can also set the input encoding and output encoding by setting
|
98
|
+
# <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
99
|
+
# These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
100
|
+
#
|
101
|
+
# @output_encoding = 'UTF-8'
|
102
|
+
|
103
|
+
class CsvBuilder < TemplateHandler
|
104
|
+
|
105
|
+
include Compilable
|
106
|
+
|
107
|
+
attr_reader :csv_builder_options
|
108
|
+
|
109
|
+
def self.line_offset
|
110
|
+
9
|
111
|
+
end
|
112
|
+
|
113
|
+
def compile(template)
|
114
|
+
<<-EOV
|
115
|
+
begin
|
116
|
+
|
117
|
+
unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
|
118
|
+
@filename ||= "\#{controller.action_name}.csv"
|
119
|
+
controller.response.headers["Content-Type"] ||= 'text/csv'
|
120
|
+
controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}"
|
121
|
+
end
|
122
|
+
|
123
|
+
result = FasterCSV.generate do |csv|
|
124
|
+
#{template.source}
|
125
|
+
end
|
126
|
+
|
127
|
+
# Transliterate into the required encoding if necessary
|
128
|
+
# TODO: make defaults configurable
|
129
|
+
@input_encoding ||= 'UTF-8'
|
130
|
+
@output_encoding ||= 'LATIN1'
|
131
|
+
|
132
|
+
if @input_encoding == @output_encoding
|
133
|
+
result
|
134
|
+
else
|
135
|
+
# TODO: do some checking to make sure iconv works correctly in
|
136
|
+
# current environment. See ActiveSupport::Inflector#transliterate
|
137
|
+
# definition for details
|
138
|
+
#
|
139
|
+
# Not using the more standard //IGNORE//TRANLIST because it raises
|
140
|
+
# Iconv::IllegalSequence for some inputs
|
141
|
+
c = Iconv.new("\#{@output_encoding}//TRANSLIT//IGNORE", @input_encoding)
|
142
|
+
c.iconv(result)
|
143
|
+
end
|
144
|
+
|
145
|
+
rescue Exception => e
|
146
|
+
RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
|
147
|
+
raise e
|
148
|
+
end
|
149
|
+
EOV
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: did_csv_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31098201
|
5
|
+
prerelease: 6
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 4
|
10
|
+
- beta
|
11
|
+
version: 0.1.4.beta
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Tom Stuart
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-02-14 00:00:00 +01:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: CSV template Rails plugin
|
24
|
+
email: didier@nocoffee.fr
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
30
|
+
- README
|
31
|
+
files:
|
32
|
+
- lib/csv_builder.rb
|
33
|
+
- lib/csv_builder/action_controller.rb
|
34
|
+
- lib/csv_builder/template_handler/base.rb
|
35
|
+
- README
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: https://github.com/did/csv_builder
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 25
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 3
|
63
|
+
- 1
|
64
|
+
version: 1.3.1
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.4.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: CSV template Rails plugin
|
72
|
+
test_files: []
|
73
|
+
|