csv_builder 1.1.7 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +10 -0
- data/README.md +99 -0
- data/Rakefile +22 -1
- data/VERSION +1 -0
- data/csv_builder.gemspec +116 -0
- data/lib/csv_builder.rb +13 -63
- data/lib/csv_builder/railtie.rb +5 -0
- data/lib/csv_builder/template_handler.rb +60 -0
- data/lib/{transliterating_filter.rb → csv_builder/transliterating_filter.rb} +3 -2
- data/rails/init.rb +1 -1
- data/spec/controllers/csv_builder_spec.rb +68 -0
- data/spec/rails_app/.gitignore +5 -0
- data/spec/rails_app/Gemfile +12 -0
- data/spec/rails_app/README +256 -0
- data/spec/rails_app/Rakefile +7 -0
- data/spec/rails_app/app/controllers/application_controller.rb +3 -0
- data/spec/rails_app/app/helpers/application_helper.rb +2 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +14 -0
- data/spec/rails_app/config.ru +4 -0
- data/spec/rails_app/config/application.rb +42 -0
- data/spec/rails_app/config/boot.rb +6 -0
- data/spec/rails_app/config/database.yml +22 -0
- data/spec/rails_app/config/environment.rb +5 -0
- data/spec/rails_app/config/environments/development.rb +26 -0
- data/spec/rails_app/config/environments/production.rb +49 -0
- data/spec/rails_app/config/environments/test.rb +35 -0
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_app/config/initializers/inflections.rb +10 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/initializers/secret_token.rb +7 -0
- data/spec/rails_app/config/initializers/session_store.rb +8 -0
- data/spec/rails_app/config/locales/en.yml +5 -0
- data/spec/rails_app/config/routes.rb +58 -0
- data/spec/rails_app/db/seeds.rb +7 -0
- data/spec/rails_app/doc/README_FOR_APP +2 -0
- data/spec/rails_app/lib/tasks/.gitkeep +0 -0
- data/spec/rails_app/public/404.html +26 -0
- data/spec/rails_app/public/422.html +26 -0
- data/spec/rails_app/public/500.html +26 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/rails_app/public/images/rails.png +0 -0
- data/spec/rails_app/public/index.html +239 -0
- data/spec/rails_app/public/javascripts/application.js +2 -0
- data/spec/rails_app/public/javascripts/controls.js +965 -0
- data/spec/rails_app/public/javascripts/dragdrop.js +974 -0
- data/spec/rails_app/public/javascripts/effects.js +1123 -0
- data/spec/rails_app/public/javascripts/prototype.js +6001 -0
- data/spec/rails_app/public/javascripts/rails.js +191 -0
- data/spec/rails_app/public/robots.txt +5 -0
- data/spec/rails_app/public/stylesheets/.gitkeep +0 -0
- data/spec/rails_app/script/rails +6 -0
- data/spec/rails_app/test/performance/browsing_test.rb +9 -0
- data/spec/rails_app/test/test_helper.rb +13 -0
- data/spec/rails_app/vendor/plugins/.gitkeep +0 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/templates/csv_builder_reports/complex.csv.csvbuilder +3 -0
- data/spec/templates/csv_builder_reports/encoding.csv.csvbuilder +1 -0
- data/spec/templates/csv_builder_reports/simple.csv.csvbuilder +1 -0
- data/spec/templates/csv_builder_reports/simple.html.erb +1 -0
- metadata +106 -33
- data/README.rdoc +0 -74
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
== 2.0.0 release 2011-04-22
|
2
|
+
* Move to Jeweler for release management
|
3
|
+
* Official Rails 3 release without regard for Rails 2
|
4
|
+
* Fix specs for Rspec 2.5 and Rails 3.0.7
|
5
|
+
|
6
|
+
== 1.1.8 (not released to gemcutter)
|
7
|
+
* Specs (Vidmantas Kabosis)
|
8
|
+
* Ruby 1.9 compability (Vidmantas Kabosis)
|
9
|
+
* CSV options are again passed to the builder (Vidmantas Kabosis)
|
10
|
+
|
1
11
|
== 1.1.7 release 2009-12-27
|
2
12
|
|
3
13
|
* Forgot to add transliterating filter to gem spec
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
**Important Note** Though I did not write this code, I did convert it to a gem and I'm currently the sole owner on
|
2
|
+
rubygems. With Joel Chippindale's approval I've taken over defacto maintainership of this gem. My use case is
|
3
|
+
primarily under rails 3, but I will accept patches for the 2.3.x version as well.
|
4
|
+
|
5
|
+
# CSV Builder
|
6
|
+
|
7
|
+
The CSV Builder Rails plugin provides a simple templating system for serving dynamically generated CSV files from your
|
8
|
+
application.
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
## Requirements
|
13
|
+
|
14
|
+
The current version of CSV Builder works with:
|
15
|
+
|
16
|
+
* Rails 3.x
|
17
|
+
* Ruby 1.8 or 1.9
|
18
|
+
|
19
|
+
The legacy version (1.1.x) was originally developed and tested for Rails 2.1. See [the legacy
|
20
|
+
docs](https://github.com/econsultancy/csv_builder) for more details.
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
## Install
|
25
|
+
|
26
|
+
### Install as a gem (recommended)
|
27
|
+
|
28
|
+
$ gem install csv_builder
|
29
|
+
|
30
|
+
If you are using Bundler then [you know what to do](http://gembundler.com).
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
## Example
|
35
|
+
|
36
|
+
CSV template files are suffixed with `.csv.csvbuilder`, for example `index.csv.csvbuilder`
|
37
|
+
|
38
|
+
Add rows to your CSV file in the template by pushing arrays of columns into the csv object.
|
39
|
+
|
40
|
+
# First row
|
41
|
+
csv << [ 'cell 1', 'cell 2' ]
|
42
|
+
# Second row
|
43
|
+
csv << [ 'another cell value', 'and another' ]
|
44
|
+
# etc...
|
45
|
+
|
46
|
+
You can set the default filename for that a browser will use for 'save as' by setting `@filename` instance variable in
|
47
|
+
your controller's action method e.g.
|
48
|
+
|
49
|
+
@filename = 'report.csv'
|
50
|
+
|
51
|
+
You can set the input encoding and output encoding by setting `@input_encoding` and `@output_encoding` instance
|
52
|
+
variables. These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
53
|
+
|
54
|
+
@output_encoding = 'UTF-8'
|
55
|
+
|
56
|
+
You can set `@csv_options` instance variable to define options for FasterCSV generator. For example:
|
57
|
+
|
58
|
+
@csv_options = { :force_quotes => true, :col_sep => ';' }
|
59
|
+
|
60
|
+
You can respond with csv in your controller as well:
|
61
|
+
|
62
|
+
respond_to do |format|
|
63
|
+
format.html
|
64
|
+
format.csv # make sure you have action_name.csv.csvbuilder template in place
|
65
|
+
end
|
66
|
+
|
67
|
+
You can also attach a csv file to mail sent out by your application by
|
68
|
+
including a snippet like the following in your mailer method
|
69
|
+
|
70
|
+
attachment "text/csv" do |attachment|
|
71
|
+
attachment.body = render(:file => 'example/index.csv.csvbuilder')
|
72
|
+
attachment.filename = 'report.csv'
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
## Contributions
|
78
|
+
|
79
|
+
As of version 2.0 this gem has a rudimentary spec suite for Rails 3. The test suite has been run under both Ruby 1.8
|
80
|
+
and 1.9. The requirements are in the Gemfile within the test spec directory. So first you can run:
|
81
|
+
|
82
|
+
cd spec/rails_app && bundle install
|
83
|
+
|
84
|
+
To install the main testing requirements. Then return back to the root directory and run:
|
85
|
+
|
86
|
+
rake spec
|
87
|
+
|
88
|
+
I will also take patches for Rails 2.3.x, though I personally have no further need of that branch.
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
== Troubleshooting
|
93
|
+
|
94
|
+
There's a known bug of encoding error in Ruby 1.9
|
95
|
+
|
96
|
+
For more details see https://rails.lighthouseapp.com/projects/8994/tickets/2188-i18n-fails-with-multibyte-strings-in-ruby-19-similar-to-2038
|
97
|
+
|
98
|
+
|
99
|
+
Copyright (c) 2008 Econsultancy.com, 2009 Vidmantas Kabošis & 2011 Gabe da Silveira released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
require 'rake'
|
2
1
|
require 'rake/rdoctask'
|
2
|
+
require 'rspec/core'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require 'jeweler'
|
3
5
|
|
4
6
|
desc 'Generate documentation for the csv_builder plugin.'
|
5
7
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
@@ -10,3 +12,22 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
10
12
|
rdoc.rdoc_files.include('CHANGELOG.rdoc')
|
11
13
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
12
14
|
end
|
15
|
+
|
16
|
+
desc 'Test the csv builder'
|
17
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
18
|
+
t.pattern = "./spec/**/*_spec.rb"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Default: run specs.'
|
22
|
+
task :default => :spec
|
23
|
+
|
24
|
+
Jeweler::Tasks.new do |gem|
|
25
|
+
gem.name = "csv_builder"
|
26
|
+
gem.homepage = "http://github.com/dasil003/csv_builder"
|
27
|
+
gem.license = "MIT"
|
28
|
+
gem.summary = %Q{CSV template handler for Rails}
|
29
|
+
gem.description = %Q{CSV template handler for Rails. Enables :format => 'csv' in controllers, with templates of the form report.csv.csvbuilder.}
|
30
|
+
gem.email = "gabe@websaviour.com"
|
31
|
+
gem.authors = ['Econsultancy', 'Vidmantas Kabosis', "Gabe da Silveira"]
|
32
|
+
end
|
33
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/csv_builder.gemspec
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{csv_builder}
|
8
|
+
s.version = "2.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Econsultancy", "Vidmantas Kabosis", "Gabe da Silveira"]
|
12
|
+
s.date = %q{2011-04-22}
|
13
|
+
s.description = %q{CSV template handler for Rails. Enables :format => 'csv' in controllers, with templates of the form report.csv.csvbuilder.}
|
14
|
+
s.email = %q{gabe@websaviour.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"CHANGELOG.rdoc",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.md",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"csv_builder.gemspec",
|
25
|
+
"lib/csv_builder.rb",
|
26
|
+
"lib/csv_builder/railtie.rb",
|
27
|
+
"lib/csv_builder/template_handler.rb",
|
28
|
+
"lib/csv_builder/transliterating_filter.rb",
|
29
|
+
"rails/init.rb",
|
30
|
+
"spec/controllers/csv_builder_spec.rb",
|
31
|
+
"spec/rails_app/.gitignore",
|
32
|
+
"spec/rails_app/Gemfile",
|
33
|
+
"spec/rails_app/README",
|
34
|
+
"spec/rails_app/Rakefile",
|
35
|
+
"spec/rails_app/app/controllers/application_controller.rb",
|
36
|
+
"spec/rails_app/app/helpers/application_helper.rb",
|
37
|
+
"spec/rails_app/app/views/layouts/application.html.erb",
|
38
|
+
"spec/rails_app/config.ru",
|
39
|
+
"spec/rails_app/config/application.rb",
|
40
|
+
"spec/rails_app/config/boot.rb",
|
41
|
+
"spec/rails_app/config/database.yml",
|
42
|
+
"spec/rails_app/config/environment.rb",
|
43
|
+
"spec/rails_app/config/environments/development.rb",
|
44
|
+
"spec/rails_app/config/environments/production.rb",
|
45
|
+
"spec/rails_app/config/environments/test.rb",
|
46
|
+
"spec/rails_app/config/initializers/backtrace_silencers.rb",
|
47
|
+
"spec/rails_app/config/initializers/inflections.rb",
|
48
|
+
"spec/rails_app/config/initializers/mime_types.rb",
|
49
|
+
"spec/rails_app/config/initializers/secret_token.rb",
|
50
|
+
"spec/rails_app/config/initializers/session_store.rb",
|
51
|
+
"spec/rails_app/config/locales/en.yml",
|
52
|
+
"spec/rails_app/config/routes.rb",
|
53
|
+
"spec/rails_app/db/seeds.rb",
|
54
|
+
"spec/rails_app/doc/README_FOR_APP",
|
55
|
+
"spec/rails_app/lib/tasks/.gitkeep",
|
56
|
+
"spec/rails_app/public/404.html",
|
57
|
+
"spec/rails_app/public/422.html",
|
58
|
+
"spec/rails_app/public/500.html",
|
59
|
+
"spec/rails_app/public/favicon.ico",
|
60
|
+
"spec/rails_app/public/images/rails.png",
|
61
|
+
"spec/rails_app/public/index.html",
|
62
|
+
"spec/rails_app/public/javascripts/application.js",
|
63
|
+
"spec/rails_app/public/javascripts/controls.js",
|
64
|
+
"spec/rails_app/public/javascripts/dragdrop.js",
|
65
|
+
"spec/rails_app/public/javascripts/effects.js",
|
66
|
+
"spec/rails_app/public/javascripts/prototype.js",
|
67
|
+
"spec/rails_app/public/javascripts/rails.js",
|
68
|
+
"spec/rails_app/public/robots.txt",
|
69
|
+
"spec/rails_app/public/stylesheets/.gitkeep",
|
70
|
+
"spec/rails_app/script/rails",
|
71
|
+
"spec/rails_app/test/performance/browsing_test.rb",
|
72
|
+
"spec/rails_app/test/test_helper.rb",
|
73
|
+
"spec/rails_app/vendor/plugins/.gitkeep",
|
74
|
+
"spec/spec_helper.rb",
|
75
|
+
"spec/templates/csv_builder_reports/complex.csv.csvbuilder",
|
76
|
+
"spec/templates/csv_builder_reports/encoding.csv.csvbuilder",
|
77
|
+
"spec/templates/csv_builder_reports/simple.csv.csvbuilder",
|
78
|
+
"spec/templates/csv_builder_reports/simple.html.erb"
|
79
|
+
]
|
80
|
+
s.homepage = %q{http://github.com/dasil003/csv_builder}
|
81
|
+
s.licenses = ["MIT"]
|
82
|
+
s.require_paths = ["lib"]
|
83
|
+
s.rubygems_version = %q{1.5.2}
|
84
|
+
s.summary = %q{CSV template handler for Rails}
|
85
|
+
s.test_files = [
|
86
|
+
"spec/controllers/csv_builder_spec.rb",
|
87
|
+
"spec/rails_app/app/controllers/application_controller.rb",
|
88
|
+
"spec/rails_app/app/helpers/application_helper.rb",
|
89
|
+
"spec/rails_app/config/application.rb",
|
90
|
+
"spec/rails_app/config/boot.rb",
|
91
|
+
"spec/rails_app/config/environment.rb",
|
92
|
+
"spec/rails_app/config/environments/development.rb",
|
93
|
+
"spec/rails_app/config/environments/production.rb",
|
94
|
+
"spec/rails_app/config/environments/test.rb",
|
95
|
+
"spec/rails_app/config/initializers/backtrace_silencers.rb",
|
96
|
+
"spec/rails_app/config/initializers/inflections.rb",
|
97
|
+
"spec/rails_app/config/initializers/mime_types.rb",
|
98
|
+
"spec/rails_app/config/initializers/secret_token.rb",
|
99
|
+
"spec/rails_app/config/initializers/session_store.rb",
|
100
|
+
"spec/rails_app/config/routes.rb",
|
101
|
+
"spec/rails_app/db/seeds.rb",
|
102
|
+
"spec/rails_app/test/performance/browsing_test.rb",
|
103
|
+
"spec/rails_app/test/test_helper.rb",
|
104
|
+
"spec/spec_helper.rb"
|
105
|
+
]
|
106
|
+
|
107
|
+
if s.respond_to? :specification_version then
|
108
|
+
s.specification_version = 3
|
109
|
+
|
110
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
111
|
+
else
|
112
|
+
end
|
113
|
+
else
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
data/lib/csv_builder.rb
CHANGED
@@ -1,66 +1,16 @@
|
|
1
|
-
|
2
|
-
require 'iconv'
|
3
|
-
|
4
|
-
module ActionView # :nodoc:
|
5
|
-
module TemplateHandlers
|
6
|
-
# Template handler for csv templates
|
7
|
-
#
|
8
|
-
# Add rows to your CSV file in the template by pushing arrays of columns into csv
|
9
|
-
#
|
10
|
-
# # First row
|
11
|
-
# csv << [ 'cell 1', 'cell 2' ]
|
12
|
-
# # Second row
|
13
|
-
# csv << [ 'another cell value', 'and another' ]
|
14
|
-
# # etc...
|
15
|
-
#
|
16
|
-
# You can set the default filename for that a browser will use for 'save as' by
|
17
|
-
# setting <tt>@filename</tt> instance variable in your controller's action method
|
18
|
-
# e.g.
|
19
|
-
#
|
20
|
-
# @filename = 'report.csv'
|
21
|
-
#
|
22
|
-
# You can also set the input encoding and output encoding by setting
|
23
|
-
# <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
24
|
-
# These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
25
|
-
#
|
26
|
-
# @output_encoding = 'UTF-8'
|
27
|
-
class CsvBuilder < TemplateHandler
|
28
|
-
include Compilable
|
29
|
-
|
30
|
-
def self.line_offset
|
31
|
-
9
|
32
|
-
end
|
33
|
-
|
34
|
-
def compile(template)
|
35
|
-
<<-EOV
|
36
|
-
begin
|
37
|
-
output = FasterCSV.generate do |faster_csv|
|
38
|
-
csv = TransliteratingFilter.new(faster_csv)
|
39
|
-
#{template.source}
|
40
|
-
end
|
1
|
+
# encoding: utf-8
|
41
2
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
controller.response.headers['Expires'] = "0"
|
50
|
-
else
|
51
|
-
controller.response.headers["Content-Type"] ||= 'text/csv'
|
52
|
-
controller.response.headers["Content-Disposition"] = "attachment; filename=\#{@filename}"
|
53
|
-
controller.response.headers["Content-Transfer-Encoding"] = "binary"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
output
|
58
|
-
rescue Exception => e
|
59
|
-
RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
|
60
|
-
raise e
|
61
|
-
end
|
62
|
-
EOV
|
63
|
-
end
|
64
|
-
end
|
3
|
+
module CsvBuilder
|
4
|
+
if RUBY_VERSION.to_f >= 1.9
|
5
|
+
require 'csv'
|
6
|
+
CSV_LIB = CSV
|
7
|
+
else
|
8
|
+
require 'fastercsv'
|
9
|
+
CSV_LIB = FasterCSV
|
65
10
|
end
|
66
11
|
end
|
12
|
+
|
13
|
+
require 'iconv'
|
14
|
+
require 'csv_builder/transliterating_filter'
|
15
|
+
require 'csv_builder/template_handler'
|
16
|
+
require 'csv_builder/railtie'
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CsvBuilder # :nodoc:
|
4
|
+
|
5
|
+
# Template handler for csv templates
|
6
|
+
#
|
7
|
+
# Add rows to your CSV file in the template by pushing arrays of columns into csv
|
8
|
+
#
|
9
|
+
# # First row
|
10
|
+
# csv << [ 'cell 1', 'cell 2' ]
|
11
|
+
# # Second row
|
12
|
+
# csv << [ 'another cell value', 'and another' ]
|
13
|
+
# # etc...
|
14
|
+
#
|
15
|
+
# You can set the default filename for that a browser will use for 'save as' by
|
16
|
+
# setting <tt>@filename</tt> instance variable in your controller's action method
|
17
|
+
# e.g.
|
18
|
+
#
|
19
|
+
# @filename = 'report.csv'
|
20
|
+
#
|
21
|
+
# You can also set the input encoding and output encoding by setting
|
22
|
+
# <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
23
|
+
# These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
24
|
+
#
|
25
|
+
# @output_encoding = 'UTF-8'
|
26
|
+
class TemplateHandler < ActionView::Template::Handler
|
27
|
+
include ActionView::Template::Handlers::Compilable
|
28
|
+
|
29
|
+
def compile(template)
|
30
|
+
<<-EOV
|
31
|
+
begin
|
32
|
+
output = CsvBuilder::CSV_LIB.generate(@csv_options || {}) do |faster_csv|
|
33
|
+
csv = CsvBuilder::TransliteratingFilter.new(faster_csv, @input_encoding || 'UTF-8', @output_encoding || 'LATIN1')
|
34
|
+
#{template.source}
|
35
|
+
end
|
36
|
+
|
37
|
+
unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
|
38
|
+
@filename ||= "\#{controller.action_name}.csv"
|
39
|
+
if controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
|
40
|
+
controller.response.headers['Pragma'] = 'public'
|
41
|
+
controller.response.headers["Content-type"] = "text/plain"
|
42
|
+
controller.response.headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
|
43
|
+
controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}"
|
44
|
+
controller.response.headers['Expires'] = "0"
|
45
|
+
else
|
46
|
+
controller.response.headers["Content-Type"] ||= 'text/csv'
|
47
|
+
controller.response.headers["Content-Disposition"] = "attachment; filename=\#{@filename}"
|
48
|
+
controller.response.headers["Content-Transfer-Encoding"] = "binary"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
output
|
53
|
+
rescue Exception => e
|
54
|
+
Rails.logger.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
|
55
|
+
raise e
|
56
|
+
end
|
57
|
+
EOV
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
|
-
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
class CsvBuilder::TransliteratingFilter
|
2
4
|
# Transliterate into the required encoding if necessary
|
3
|
-
# TODO: make defaults configurable
|
4
5
|
def initialize(faster_csv, input_encoding = 'UTF-8', output_encoding = 'LATIN1')
|
5
6
|
@faster_csv = faster_csv
|
6
7
|
|
data/rails/init.rb
CHANGED
@@ -0,0 +1,68 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
4
|
+
|
5
|
+
class CsvBuilderReportsController < ApplicationController
|
6
|
+
before_filter {|c| c.prepend_view_path(File.expand_path(File.dirname(__FILE__) + '/../templates')) }
|
7
|
+
|
8
|
+
def simple
|
9
|
+
# dummy
|
10
|
+
respond_to do |format|
|
11
|
+
format.html
|
12
|
+
format.csv
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def complex
|
17
|
+
respond_to do |format|
|
18
|
+
format.csv do
|
19
|
+
@filename = 'some_complex_filename.csv'
|
20
|
+
@csv_options = { :col_sep => "\t" }
|
21
|
+
@data = TEST_DATA
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def encoding
|
27
|
+
respond_to do |format|
|
28
|
+
format.csv { @output_encoding = 'UTF-16' }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
ActionController::Routing::Routes.draw { |map| map.connect ':controller/:action' }
|
34
|
+
|
35
|
+
|
36
|
+
describe CsvBuilderReportsController do
|
37
|
+
render_views
|
38
|
+
|
39
|
+
describe "Simple layout" do
|
40
|
+
it "still responds to HTML" do
|
41
|
+
get 'simple'
|
42
|
+
response.should be_success
|
43
|
+
end
|
44
|
+
|
45
|
+
it "responds to CSV" do
|
46
|
+
get 'simple', :format => 'csv'
|
47
|
+
response.should be_success
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "Layout with options" do
|
52
|
+
it "sets output encoding correctly" do
|
53
|
+
get 'encoding', :format => 'csv'
|
54
|
+
correct_output = generate({}, [Iconv.iconv('UTF-16//TRANSLIT//IGNORE', 'UTF-8', 'ąčęėįšųūž')])
|
55
|
+
response.body.to_s.should == correct_output
|
56
|
+
end
|
57
|
+
|
58
|
+
it "passes csv options" do
|
59
|
+
get 'complex', :format => 'csv'
|
60
|
+
response.body.to_s.should == generate({ :col_sep => "\t" })
|
61
|
+
end
|
62
|
+
|
63
|
+
it "sets filename" do
|
64
|
+
get 'complex', :format => 'csv'
|
65
|
+
response.headers['Content-Disposition'].should match(/filename=some_complex_filename.csv/)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|