potatosalad-csv_builder 2.0.2
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/.gitignore +17 -0
- data/.rspec +1 -0
- data/CHANGELOG.rdoc +45 -0
- data/Gemfile +10 -0
- data/MIT-LICENSE +20 -0
- data/README.md +101 -0
- data/Rakefile +23 -0
- data/csv_builder.gemspec +22 -0
- data/lib/csv_builder/csv_proxy.rb +40 -0
- data/lib/csv_builder/filter_proxy.rb +36 -0
- data/lib/csv_builder/proxy.rb +26 -0
- data/lib/csv_builder/railtie.rb +5 -0
- data/lib/csv_builder/template_handler.rb +47 -0
- data/lib/csv_builder/version.rb +3 -0
- data/lib/csv_builder.rb +19 -0
- data/lib/potatosalad-csv_builder.rb +1 -0
- data/rails/init.rb +3 -0
- data/spec/controllers/csv_builder_spec.rb +69 -0
- data/spec/rails_app/.gitignore +5 -0
- data/spec/rails_app/Gemfile +7 -0
- data/spec/rails_app/README +1 -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/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/routes.rb +58 -0
- data/spec/rails_app/config.ru +4 -0
- data/spec/rails_app/db/seeds.rb +7 -0
- data/spec/spec_helper.rb +22 -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 +129 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
* Added dependencies and simplified contributor setup
|
2
|
+
|
3
|
+
== 2.1.potatosalad release 2011-06-28
|
4
|
+
* Updates for Rails 3.1
|
5
|
+
* Clean up gem packaging
|
6
|
+
|
7
|
+
== 2.0.1 release 2011-04-22
|
8
|
+
* Remove cruft from test app
|
9
|
+
* Exclude spec from release
|
10
|
+
|
11
|
+
== 2.0.0 release 2011-04-22
|
12
|
+
* Move to Jeweler for release management
|
13
|
+
* Official Rails 3 release without regard for Rails 2
|
14
|
+
* Fix specs for Rspec 2.5 and Rails 3.0.7
|
15
|
+
|
16
|
+
== 1.1.8 (not released to gemcutter)
|
17
|
+
* Specs (Vidmantas Kabosis)
|
18
|
+
* Ruby 1.9 compability (Vidmantas Kabosis)
|
19
|
+
* CSV options are again passed to the builder (Vidmantas Kabosis)
|
20
|
+
|
21
|
+
== 1.1.7 release 2009-12-27
|
22
|
+
|
23
|
+
* Forgot to add transliterating filter to gem spec
|
24
|
+
|
25
|
+
== 1.1.6 release 2009-12-18
|
26
|
+
|
27
|
+
* Generate csv before setting headers so that errors are displayed in-browser instead of downloaded (Gabe da Silveira)
|
28
|
+
* Tweak README for gemcutter download. (Gabe da Silveira)
|
29
|
+
|
30
|
+
== 1.1.5 release 2009-12-18
|
31
|
+
|
32
|
+
* Do transliteration before exporting to CSV, not after (Tom Stuart)
|
33
|
+
* Released on gemcutter (Gabe da Silveira)
|
34
|
+
|
35
|
+
== 1.1.4 release 2009-07-27
|
36
|
+
|
37
|
+
* Merged some patches:
|
38
|
+
* Added gem spec (Jan De Poorter)
|
39
|
+
* Set the correct response headers to work with IE, made the existing ones a little more robust (Jose Fernandez)
|
40
|
+
* Created changelog, updated docs (Michael Reinsch)
|
41
|
+
|
42
|
+
== 1.0 released 2008
|
43
|
+
|
44
|
+
* The original csv_builder plugin, released by Econsultancy.com
|
45
|
+
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Econsultancy.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
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, and the current version is not backwards compatible, but I will maintain a 2.3.x branch as well
|
4
|
+
if anyone has patches.
|
5
|
+
|
6
|
+
# CSV Builder
|
7
|
+
|
8
|
+
The CSV Builder Rails plugin provides a simple templating system for serving dynamically generated CSV files from your
|
9
|
+
application.
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
## Requirements
|
14
|
+
|
15
|
+
The current version of CSV Builder works with:
|
16
|
+
|
17
|
+
* Rails 3.x
|
18
|
+
* Ruby 1.8 or 1.9
|
19
|
+
|
20
|
+
The legacy version (1.1.x) was originally developed and tested for Rails 2.1. See [the legacy
|
21
|
+
docs](https://github.com/econsultancy/csv_builder) for more details.
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## Install
|
26
|
+
|
27
|
+
### Install as a gem (recommended)
|
28
|
+
|
29
|
+
$ gem install csv_builder
|
30
|
+
|
31
|
+
If you are using Bundler then [you know what to do](http://gembundler.com).
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
## Example
|
36
|
+
|
37
|
+
CSV template files are suffixed with `.csv.csvbuilder`, for example `index.csv.csvbuilder`
|
38
|
+
|
39
|
+
Add rows to your CSV file in the template by pushing arrays of columns into the csv object.
|
40
|
+
|
41
|
+
# First row
|
42
|
+
csv << [ 'cell 1', 'cell 2' ]
|
43
|
+
# Second row
|
44
|
+
csv << [ 'another cell value', 'and another' ]
|
45
|
+
# etc...
|
46
|
+
|
47
|
+
You can set the default filename for that a browser will use for 'save as' by setting `@filename` instance variable in
|
48
|
+
your controller's action method e.g.
|
49
|
+
|
50
|
+
@filename = 'report.csv'
|
51
|
+
|
52
|
+
You can set the input encoding and output encoding by setting `@input_encoding` and `@output_encoding` instance
|
53
|
+
variables. These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
54
|
+
|
55
|
+
@output_encoding = 'UTF-8'
|
56
|
+
|
57
|
+
You can set `@csv_options` instance variable to define options for FasterCSV generator. For example:
|
58
|
+
|
59
|
+
@csv_options = { :force_quotes => true, :col_sep => ';' }
|
60
|
+
|
61
|
+
You can respond with csv in your controller as well:
|
62
|
+
|
63
|
+
respond_to do |format|
|
64
|
+
format.html
|
65
|
+
format.csv # make sure you have action_name.csv.csvbuilder template in place
|
66
|
+
end
|
67
|
+
|
68
|
+
You can also attach a csv file to mail sent out by your application by
|
69
|
+
including a snippet like the following in your mailer method
|
70
|
+
|
71
|
+
attachment "text/csv" do |attachment|
|
72
|
+
attachment.body = render(:file => 'example/index.csv.csvbuilder')
|
73
|
+
attachment.filename = 'report.csv'
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
## Contributions
|
79
|
+
|
80
|
+
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
|
81
|
+
and 1.9. The requirements are in the Gemfile within the test spec directory. You will need Bundler installed and then
|
82
|
+
you can run:
|
83
|
+
|
84
|
+
cd spec/rails_app && bundle install && cd ../..
|
85
|
+
|
86
|
+
To install the main testing requirements. Then return back to the root directory and run:
|
87
|
+
|
88
|
+
rake spec
|
89
|
+
|
90
|
+
I will also take patches for Rails 2.3.x, though I personally have no further need of that branch.
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
## Troubleshooting
|
95
|
+
|
96
|
+
There's a known bug of encoding error in Ruby 1.9
|
97
|
+
|
98
|
+
For more details see https://rails.lighthouseapp.com/projects/8994/tickets/2188-i18n-fails-with-multibyte-strings-in-ruby-19-similar-to-2038
|
99
|
+
|
100
|
+
|
101
|
+
Copyright (c) 2008 Econsultancy.com, 2009 Vidmantas Kabošis & 2011 Gabe da Silveira released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rdoc/task'
|
4
|
+
require 'rspec/core'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
desc 'Generate documentation for the csv_builder plugin.'
|
8
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
9
|
+
rdoc.rdoc_dir = 'rdoc'
|
10
|
+
rdoc.title = 'CSV Builder'
|
11
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
12
|
+
rdoc.rdoc_files.include('README.rdoc')
|
13
|
+
rdoc.rdoc_files.include('CHANGELOG.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Test the csv builder'
|
18
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
19
|
+
t.pattern = "./spec/**/*_spec.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Default: run specs.'
|
23
|
+
task :default => :spec
|
data/csv_builder.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/csv_builder/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = [%q{Econsultancy}, %q{Vidmantas Kabosis}, %q{Gabe da Silveira}, %q{Andrew Bennett}]
|
6
|
+
gem.email = ["andrew@delorum.com", %q{gabe@websaviour.com}]
|
7
|
+
gem.description = %q{CSV template handler for Rails. Enables :format => 'csv' in controllers, with templates of the form report.csv.csvbuilder.}
|
8
|
+
gem.summary = %q{CSV template handler for Rails}
|
9
|
+
gem.homepage = 'http://github.com/potatosalad/csv_builder'
|
10
|
+
gem.licenses = [%q{MIT}]
|
11
|
+
|
12
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
13
|
+
gem.files = `git ls-files`.split("\n")
|
14
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
gem.name = "potatosalad-csv_builder"
|
16
|
+
gem.require_paths = ['lib']
|
17
|
+
gem.version = CsvBuilder::VERSION
|
18
|
+
gem.requirements = [%q{iconv}, %q{Ruby 1.9.x or FasterCSV}]
|
19
|
+
|
20
|
+
gem.add_dependency "rails", ">= 3.0.0"
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CsvBuilder
|
4
|
+
class CsvProxy < FilterProxy
|
5
|
+
|
6
|
+
def initialize(data, options = {})
|
7
|
+
super(data, options).tap do
|
8
|
+
|
9
|
+
init(CSV.new(data, @options), data) do
|
10
|
+
|
11
|
+
@ivar_names = base.instance_variables.select do |ivar|
|
12
|
+
CSV::DEFAULT_OPTIONS.keys.map { |key| "@#{key}" }.include?(ivar.to_s)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def [](key)
|
20
|
+
if @ivar_names.include?(ivar_key = "@#{key}".intern)
|
21
|
+
base.instance_variable_get(ivar_key)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def []=(key, value)
|
26
|
+
if @ivar_names.include?(ivar_key = "@#{key}".intern)
|
27
|
+
base.instance_variable_set(ivar_key, value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def settings
|
32
|
+
@ivar_names.inject({}) do |hash,key|
|
33
|
+
hash.tap do
|
34
|
+
hash[key.to_s[1..-1].intern] = base.instance_variable_get(key)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CsvBuilder
|
4
|
+
class FilterProxy < Proxy
|
5
|
+
|
6
|
+
# Transliterate into the required encoding if necessary
|
7
|
+
def initialize(data, options = {})
|
8
|
+
@options = options.dup
|
9
|
+
|
10
|
+
@options.reverse_merge!(:input_encoding => 'UTF-8', :output_encoding => 'LATIN1')
|
11
|
+
|
12
|
+
@input_encoding = @options.delete(:input_encoding)
|
13
|
+
@output_encoding = @options.delete(:output_encoding)
|
14
|
+
|
15
|
+
super(data, @options)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Transliterate before passing to CSV so that the right characters (e.g. quotes) get escaped
|
19
|
+
def <<(row)
|
20
|
+
if @input_encoding != @output_encoding
|
21
|
+
# TODO: do some checking to make sure iconv works correctly in
|
22
|
+
# current environment. See ActiveSupport::Inflector#transliterate
|
23
|
+
# definition for details
|
24
|
+
#
|
25
|
+
# Not using the more standard //IGNORE//TRANSLIT because it raises
|
26
|
+
# Iconv::IllegalSequence for some inputs
|
27
|
+
@iconv = Iconv.new("#{@output_encoding}//TRANSLIT//IGNORE", @input_encoding)
|
28
|
+
end
|
29
|
+
|
30
|
+
base << if @iconv then row.map { |value| @iconv.iconv(value.to_s) } else row end
|
31
|
+
end
|
32
|
+
|
33
|
+
alias :add_row :<<
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CsvBuilder
|
4
|
+
class Proxy
|
5
|
+
|
6
|
+
# We undefine most methods to get them sent through to the target.
|
7
|
+
instance_methods.each do |method|
|
8
|
+
undef_method(method) unless
|
9
|
+
method =~ /(^__|^send$|^object_id$|^extend$|^tap$)/
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_accessor :base, :target, :options
|
13
|
+
|
14
|
+
def init(base, target, options = {}, &block)
|
15
|
+
@base, @target, @options = base, target, options
|
16
|
+
block.call if block
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def method_missing(name, *args, &block)
|
22
|
+
base.send(name, *args, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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
|
27
|
+
class_attribute :default_format
|
28
|
+
self.default_format = Mime::CSV
|
29
|
+
|
30
|
+
def call(template)
|
31
|
+
<<-EOV
|
32
|
+
begin
|
33
|
+
self.output_buffer = String.new;
|
34
|
+
csv = CsvBuilder::CsvProxy.new(self.output_buffer, @csv_builder || {});
|
35
|
+
#{template.source};
|
36
|
+
if @csv_filename;
|
37
|
+
controller.response.headers['Content-Disposition'] = %Q{attachment; filename="\#{@csv_filename}"};
|
38
|
+
end;
|
39
|
+
self.output_buffer;
|
40
|
+
rescue Exception => e
|
41
|
+
Rails.logger.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
|
42
|
+
raise e
|
43
|
+
end
|
44
|
+
EOV
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/csv_builder.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CsvBuilder
|
4
|
+
if RUBY_VERSION.to_f >= 1.9
|
5
|
+
require 'csv'
|
6
|
+
CSV = ::CSV
|
7
|
+
else
|
8
|
+
require 'fastercsv'
|
9
|
+
CSV = ::FasterCSV
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'action_view'
|
14
|
+
require 'iconv'
|
15
|
+
require 'csv_builder/proxy'
|
16
|
+
require 'csv_builder/filter_proxy'
|
17
|
+
require 'csv_builder/csv_proxy'
|
18
|
+
require 'csv_builder/template_handler'
|
19
|
+
require 'csv_builder/railtie'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'csv_builder'
|
data/rails/init.rb
ADDED
@@ -0,0 +1,69 @@
|
|
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
|
+
before_filter { @csv_builder ||= {} }
|
8
|
+
|
9
|
+
def simple
|
10
|
+
# dummy
|
11
|
+
respond_to do |format|
|
12
|
+
format.html
|
13
|
+
format.csv
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def complex
|
18
|
+
respond_to do |format|
|
19
|
+
format.csv do
|
20
|
+
@csv_filename = 'some_complex_filename.csv'
|
21
|
+
@csv_builder[:col_sep] = "\t"
|
22
|
+
@data = TEST_DATA
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def encoding
|
28
|
+
respond_to do |format|
|
29
|
+
format.csv { @csv_builder[:output_encoding] = 'UTF-16' }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
Rails.application.routes.draw { match ':controller/:action' }
|
35
|
+
|
36
|
+
|
37
|
+
describe CsvBuilderReportsController do
|
38
|
+
render_views
|
39
|
+
|
40
|
+
describe "Simple layout" do
|
41
|
+
it "still responds to HTML" do
|
42
|
+
get 'simple'
|
43
|
+
response.should be_success
|
44
|
+
end
|
45
|
+
|
46
|
+
it "responds to CSV" do
|
47
|
+
get 'simple', :format => 'csv'
|
48
|
+
response.should be_success
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "Layout with options" do
|
53
|
+
it "sets output encoding correctly" do
|
54
|
+
get 'encoding', :format => 'csv'
|
55
|
+
correct_output = generate({}, [Iconv.iconv('UTF-16//TRANSLIT//IGNORE', 'UTF-8', 'ąčęėįšųūž')])
|
56
|
+
response.body.to_s.should == correct_output
|
57
|
+
end
|
58
|
+
|
59
|
+
it "passes csv options" do
|
60
|
+
get 'complex', :format => 'csv'
|
61
|
+
response.body.to_s.should == generate({ :col_sep => "\t" })
|
62
|
+
end
|
63
|
+
|
64
|
+
it "sets filename" do
|
65
|
+
get 'complex', :format => 'csv'
|
66
|
+
response.headers['Content-Disposition'].should match(/filename="some_complex_filename.csv"/)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Test app for csvbuilder
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
# If you have a Gemfile, require the gems listed there, including any gems
|
6
|
+
# you've limited to :test, :development, or :production.
|
7
|
+
Bundler.require(:default, Rails.env) if defined?(Bundler)
|
8
|
+
|
9
|
+
module RailsApp
|
10
|
+
class Application < Rails::Application
|
11
|
+
# Settings in config/environments/* take precedence over those specified here.
|
12
|
+
# Application configuration should go into files in config/initializers
|
13
|
+
# -- all .rb files in that directory are automatically loaded.
|
14
|
+
|
15
|
+
# Custom directories with classes and modules you want to be autoloadable.
|
16
|
+
# config.autoload_paths += %W(#{config.root}/extras)
|
17
|
+
|
18
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
19
|
+
# :all can be used as a placeholder for all plugins not explicitly named.
|
20
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
21
|
+
|
22
|
+
# Activate observers that should always be running.
|
23
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
24
|
+
|
25
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
26
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
27
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
28
|
+
|
29
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
30
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
31
|
+
# config.i18n.default_locale = :de
|
32
|
+
|
33
|
+
# JavaScript files you want as :defaults (application.js is always included).
|
34
|
+
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
|
35
|
+
|
36
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
37
|
+
config.encoding = "utf-8"
|
38
|
+
|
39
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
40
|
+
config.filter_parameters += [:password]
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
development:
|
4
|
+
adapter: sqlite3
|
5
|
+
database: db/development.sqlite3
|
6
|
+
pool: 5
|
7
|
+
timeout: 5000
|
8
|
+
|
9
|
+
# Warning: The database defined as "test" will be erased and
|
10
|
+
# re-generated from your development database when you run "rake".
|
11
|
+
# Do not set this db to the same as development or production.
|
12
|
+
test:
|
13
|
+
adapter: sqlite3
|
14
|
+
database: db/test.sqlite3
|
15
|
+
pool: 5
|
16
|
+
timeout: 5000
|
17
|
+
|
18
|
+
production:
|
19
|
+
adapter: sqlite3
|
20
|
+
database: db/production.sqlite3
|
21
|
+
pool: 5
|
22
|
+
timeout: 5000
|
@@ -0,0 +1,26 @@
|
|
1
|
+
RailsApp::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# In the development environment your application's code is reloaded on
|
5
|
+
# every request. This slows down response time but is perfect for development
|
6
|
+
# since you don't have to restart the webserver when you make code changes.
|
7
|
+
config.cache_classes = false
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
config.action_view.debug_rjs = true
|
15
|
+
config.action_controller.perform_caching = false
|
16
|
+
|
17
|
+
# Don't care if the mailer can't send
|
18
|
+
config.action_mailer.raise_delivery_errors = false
|
19
|
+
|
20
|
+
# Print deprecation notices to the Rails logger
|
21
|
+
config.active_support.deprecation = :log
|
22
|
+
|
23
|
+
# Only use best-standards-support built into browsers
|
24
|
+
config.action_dispatch.best_standards_support = :builtin
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
RailsApp::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# The production environment is meant for finished, "live" apps.
|
5
|
+
# Code is not reloaded between requests
|
6
|
+
config.cache_classes = true
|
7
|
+
|
8
|
+
# Full error reports are disabled and caching is turned on
|
9
|
+
config.consider_all_requests_local = false
|
10
|
+
config.action_controller.perform_caching = true
|
11
|
+
|
12
|
+
# Specifies the header that your server uses for sending files
|
13
|
+
config.action_dispatch.x_sendfile_header = "X-Sendfile"
|
14
|
+
|
15
|
+
# For nginx:
|
16
|
+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
|
17
|
+
|
18
|
+
# If you have no front-end server that supports something like X-Sendfile,
|
19
|
+
# just comment this out and Rails will serve the files
|
20
|
+
|
21
|
+
# See everything in the log (default is :info)
|
22
|
+
# config.log_level = :debug
|
23
|
+
|
24
|
+
# Use a different logger for distributed setups
|
25
|
+
# config.logger = SyslogLogger.new
|
26
|
+
|
27
|
+
# Use a different cache store in production
|
28
|
+
# config.cache_store = :mem_cache_store
|
29
|
+
|
30
|
+
# Disable Rails's static asset server
|
31
|
+
# In production, Apache or nginx will already do this
|
32
|
+
config.serve_static_assets = false
|
33
|
+
|
34
|
+
# Enable serving of images, stylesheets, and javascripts from an asset server
|
35
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
36
|
+
|
37
|
+
# Disable delivery errors, bad email addresses will be ignored
|
38
|
+
# config.action_mailer.raise_delivery_errors = false
|
39
|
+
|
40
|
+
# Enable threaded mode
|
41
|
+
# config.threadsafe!
|
42
|
+
|
43
|
+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
44
|
+
# the I18n.default_locale when a translation can not be found)
|
45
|
+
config.i18n.fallbacks = true
|
46
|
+
|
47
|
+
# Send deprecation notices to registered listeners
|
48
|
+
config.active_support.deprecation = :notify
|
49
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
RailsApp::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# The test environment is used exclusively to run your application's
|
5
|
+
# test suite. You never need to work with it otherwise. Remember that
|
6
|
+
# your test database is "scratch space" for the test suite and is wiped
|
7
|
+
# and recreated between test runs. Don't rely on the data there!
|
8
|
+
config.cache_classes = true
|
9
|
+
|
10
|
+
# Log error messages when you accidentally call methods on nil.
|
11
|
+
config.whiny_nils = true
|
12
|
+
|
13
|
+
# Show full error reports and disable caching
|
14
|
+
config.consider_all_requests_local = true
|
15
|
+
config.action_controller.perform_caching = false
|
16
|
+
|
17
|
+
# Raise exceptions instead of rendering exception templates
|
18
|
+
config.action_dispatch.show_exceptions = false
|
19
|
+
|
20
|
+
# Disable request forgery protection in test environment
|
21
|
+
config.action_controller.allow_forgery_protection = false
|
22
|
+
|
23
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
24
|
+
# The :test delivery method accumulates sent emails in the
|
25
|
+
# ActionMailer::Base.deliveries array.
|
26
|
+
config.action_mailer.delivery_method = :test
|
27
|
+
|
28
|
+
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
29
|
+
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
30
|
+
# like if you have constraints or database-specific column types
|
31
|
+
# config.active_record.schema_format = :sql
|
32
|
+
|
33
|
+
# Print deprecation notices to the stderr
|
34
|
+
config.active_support.deprecation = :stderr
|
35
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
RailsApp::Application.routes.draw do
|
2
|
+
# The priority is based upon order of creation:
|
3
|
+
# first created -> highest priority.
|
4
|
+
|
5
|
+
# Sample of regular route:
|
6
|
+
# match 'products/:id' => 'catalog#view'
|
7
|
+
# Keep in mind you can assign values other than :controller and :action
|
8
|
+
|
9
|
+
# Sample of named route:
|
10
|
+
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
|
11
|
+
# This route can be invoked with purchase_url(:id => product.id)
|
12
|
+
|
13
|
+
# Sample resource route (maps HTTP verbs to controller actions automatically):
|
14
|
+
# resources :products
|
15
|
+
|
16
|
+
# Sample resource route with options:
|
17
|
+
# resources :products do
|
18
|
+
# member do
|
19
|
+
# get 'short'
|
20
|
+
# post 'toggle'
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# collection do
|
24
|
+
# get 'sold'
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
|
28
|
+
# Sample resource route with sub-resources:
|
29
|
+
# resources :products do
|
30
|
+
# resources :comments, :sales
|
31
|
+
# resource :seller
|
32
|
+
# end
|
33
|
+
|
34
|
+
# Sample resource route with more complex sub-resources
|
35
|
+
# resources :products do
|
36
|
+
# resources :comments
|
37
|
+
# resources :sales do
|
38
|
+
# get 'recent', :on => :collection
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
|
42
|
+
# Sample resource route within a namespace:
|
43
|
+
# namespace :admin do
|
44
|
+
# # Directs /admin/products/* to Admin::ProductsController
|
45
|
+
# # (app/controllers/admin/products_controller.rb)
|
46
|
+
# resources :products
|
47
|
+
# end
|
48
|
+
|
49
|
+
# You can have the root of your site routed with "root"
|
50
|
+
# just remember to delete public/index.html.
|
51
|
+
# root :to => "welcome#index"
|
52
|
+
|
53
|
+
# See how all your routes lay out with "rake routes"
|
54
|
+
|
55
|
+
# This is a legacy wild controller route that's not recommended for RESTful applications.
|
56
|
+
# Note: This route will make all actions in every controller accessible via GET requests.
|
57
|
+
# match ':controller(/:action(/:id(.:format)))'
|
58
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# This file should contain all the record creation needed to seed the database with its default values.
|
2
|
+
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
|
3
|
+
#
|
4
|
+
# Examples:
|
5
|
+
#
|
6
|
+
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
|
7
|
+
# Mayor.create(:name => 'Daley', :city => cities.first)
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
ENV["RAILS_ENV"] ||= 'test'
|
4
|
+
rails_root = File.expand_path('../rails_app', __FILE__)
|
5
|
+
require rails_root + '/config/environment.rb'
|
6
|
+
|
7
|
+
require 'rspec/rails'
|
8
|
+
|
9
|
+
TEST_DATA = [
|
10
|
+
['Lorem', 'ipsum'],
|
11
|
+
['Lorem ipsum dolor sit amet,' 'consectetur adipiscing elit. Sed id '],
|
12
|
+
['augue! "3" !@#$^&*()_+_', 'sed risus laoreet condimentum ac nec dui.'],
|
13
|
+
['\'Aenean sagittis lorem ac', 'lorem comm<s>odo nec eleifend risus']
|
14
|
+
]
|
15
|
+
|
16
|
+
def generate(options = {}, data = TEST_DATA)
|
17
|
+
CsvBuilder::CSV.generate(options) do |csv|
|
18
|
+
data.each do |row|
|
19
|
+
csv << row
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
csv << ['ąčęėįšųūž']
|
@@ -0,0 +1 @@
|
|
1
|
+
csv << [ 'cell 1', 'cell 2' ]
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello world
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: potatosalad-csv_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 2.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Econsultancy
|
9
|
+
- Vidmantas Kabosis
|
10
|
+
- Gabe da Silveira
|
11
|
+
- Andrew Bennett
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2011-06-28 00:00:00 Z
|
17
|
+
dependencies:
|
18
|
+
- !ruby/object:Gem::Dependency
|
19
|
+
name: rails
|
20
|
+
prerelease: false
|
21
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
type: :runtime
|
28
|
+
version_requirements: *id001
|
29
|
+
description: CSV template handler for Rails. Enables :format => 'csv' in controllers, with templates of the form report.csv.csvbuilder.
|
30
|
+
email:
|
31
|
+
- andrew@delorum.com
|
32
|
+
- gabe@websaviour.com
|
33
|
+
executables: []
|
34
|
+
|
35
|
+
extensions: []
|
36
|
+
|
37
|
+
extra_rdoc_files: []
|
38
|
+
|
39
|
+
files:
|
40
|
+
- .gitignore
|
41
|
+
- .rspec
|
42
|
+
- CHANGELOG.rdoc
|
43
|
+
- Gemfile
|
44
|
+
- MIT-LICENSE
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- csv_builder.gemspec
|
48
|
+
- lib/csv_builder.rb
|
49
|
+
- lib/csv_builder/csv_proxy.rb
|
50
|
+
- lib/csv_builder/filter_proxy.rb
|
51
|
+
- lib/csv_builder/proxy.rb
|
52
|
+
- lib/csv_builder/railtie.rb
|
53
|
+
- lib/csv_builder/template_handler.rb
|
54
|
+
- lib/csv_builder/version.rb
|
55
|
+
- lib/potatosalad-csv_builder.rb
|
56
|
+
- rails/init.rb
|
57
|
+
- spec/controllers/csv_builder_spec.rb
|
58
|
+
- spec/rails_app/.gitignore
|
59
|
+
- spec/rails_app/Gemfile
|
60
|
+
- spec/rails_app/README
|
61
|
+
- spec/rails_app/app/controllers/application_controller.rb
|
62
|
+
- spec/rails_app/app/helpers/application_helper.rb
|
63
|
+
- spec/rails_app/app/views/layouts/application.html.erb
|
64
|
+
- spec/rails_app/config.ru
|
65
|
+
- spec/rails_app/config/application.rb
|
66
|
+
- spec/rails_app/config/boot.rb
|
67
|
+
- spec/rails_app/config/database.yml
|
68
|
+
- spec/rails_app/config/environment.rb
|
69
|
+
- spec/rails_app/config/environments/development.rb
|
70
|
+
- spec/rails_app/config/environments/production.rb
|
71
|
+
- spec/rails_app/config/environments/test.rb
|
72
|
+
- spec/rails_app/config/routes.rb
|
73
|
+
- spec/rails_app/db/seeds.rb
|
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
|
+
homepage: http://github.com/potatosalad/csv_builder
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: "0"
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: "0"
|
99
|
+
requirements:
|
100
|
+
- iconv
|
101
|
+
- Ruby 1.9.x or FasterCSV
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.5
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: CSV template handler for Rails
|
107
|
+
test_files:
|
108
|
+
- spec/controllers/csv_builder_spec.rb
|
109
|
+
- spec/rails_app/.gitignore
|
110
|
+
- spec/rails_app/Gemfile
|
111
|
+
- spec/rails_app/README
|
112
|
+
- spec/rails_app/app/controllers/application_controller.rb
|
113
|
+
- spec/rails_app/app/helpers/application_helper.rb
|
114
|
+
- spec/rails_app/app/views/layouts/application.html.erb
|
115
|
+
- spec/rails_app/config.ru
|
116
|
+
- spec/rails_app/config/application.rb
|
117
|
+
- spec/rails_app/config/boot.rb
|
118
|
+
- spec/rails_app/config/database.yml
|
119
|
+
- spec/rails_app/config/environment.rb
|
120
|
+
- spec/rails_app/config/environments/development.rb
|
121
|
+
- spec/rails_app/config/environments/production.rb
|
122
|
+
- spec/rails_app/config/environments/test.rb
|
123
|
+
- spec/rails_app/config/routes.rb
|
124
|
+
- spec/rails_app/db/seeds.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/templates/csv_builder_reports/complex.csv.csvbuilder
|
127
|
+
- spec/templates/csv_builder_reports/encoding.csv.csvbuilder
|
128
|
+
- spec/templates/csv_builder_reports/simple.csv.csvbuilder
|
129
|
+
- spec/templates/csv_builder_reports/simple.html.erb
|