as_csv 1.0.2 → 2.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +1 -0
- data/.travis.yml +4 -1
- data/Gemfile +2 -0
- data/README.md +8 -4
- data/as_csv.gemspec +11 -10
- data/lib/as_csv/core_ext/array.rb +17 -1
- data/lib/as_csv/csv_builder.rb +27 -17
- data/lib/as_csv/railtie.rb +15 -0
- data/lib/as_csv/version.rb +1 -1
- data/lib/as_csv.rb +1 -7
- data/script/rspec +16 -0
- data/spec/controllers/render_widgets_controller_spec.rb +1 -1
- data/spec/controllers/respond_with_widgets_controller_spec.rb +1 -1
- data/spec/core_ext/array_spec.rb +23 -16
- data/spec/dummy/app/controllers/render_widgets_controller.rb +1 -1
- data/spec/dummy/app/controllers/respond_with_widgets_controller.rb +1 -1
- data/spec/dummy/app/models/widget.rb +0 -1
- data/spec/dummy/config/application.rb +0 -5
- data/spec/dummy/config/environments/test.rb +2 -3
- data/spec/lib/csv_builder_spec.rb +49 -85
- data/spec/models/active_model_spec.rb +1 -1
- data/spec/models/widget_spec.rb +3 -3
- data/spec/models/widget_with_options_spec.rb +2 -2
- data/spec/rails_helper.rb +25 -0
- data/spec/spec_helper.rb +2 -21
- data/spec/support/widgets_controller_mixin.rb +18 -24
- metadata +69 -38
- data/Gemfile.lock +0 -107
- data/lib/as_csv/action_renderer.rb +0 -5
- data/lib/as_csv/active_model.rb +0 -11
- data/lib/as_csv/csv_proxy.rb +0 -13
- data/spec/dummy/config/environments/development.rb +0 -32
- data/spec/dummy/config/environments/production.rb +0 -54
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fab6c26d2cc4b35271b2e283e491551b0b543462
|
4
|
+
data.tar.gz: 57575c488181311eead7ec3f0d06720d542cce3a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ef85d7066f7fab2bed8429e747f00a09d37bf185e27f6811dc352ee49a2b1c3f49c38661ab4cfd372eec297617e34fa39c566a6d8bc2f3c258a3a7d141c6610
|
7
|
+
data.tar.gz: eb9680322c9c6aef090247f6a492b37bb94953ce177c2148df5cb018b9d7a347e2d38d998cd4f56228d4a7b95e172864e80f2cd5c25ee70e48ccb519b84fd604
|
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/danielfone/as_csv)
|
4
4
|
[](https://gemnasium.com/danielfone/as_csv)
|
5
|
-
[](https://codeclimate.com/github/danielfone/as_csv)
|
6
6
|
|
7
7
|
This gem allows you to expose CSV in your apps as you'd expose JSON or XML.
|
8
8
|
|
@@ -14,7 +14,9 @@ Ruby 1.8 will work, but by default the order of the columns will not be guarante
|
|
14
14
|
|
15
15
|
Add this line to your application's Gemfile:
|
16
16
|
|
17
|
-
|
17
|
+
```ruby
|
18
|
+
gem 'as_csv'
|
19
|
+
```
|
18
20
|
|
19
21
|
## Basic Usage
|
20
22
|
|
@@ -77,7 +79,7 @@ id,name,description,code
|
|
77
79
|
> puts Widget.first.to_csv
|
78
80
|
id,name,description,code
|
79
81
|
1,widget-1,widget-description-1,1001
|
80
|
-
=> nil
|
82
|
+
=> nil
|
81
83
|
```
|
82
84
|
|
83
85
|
Behind the scenes, any classes that `include ActiveModel::Serialization` will expose their `attributes` with `to_csv`.
|
@@ -105,7 +107,7 @@ widget-1,1001
|
|
105
107
|
widget-2,1002
|
106
108
|
widget-3,1003
|
107
109
|
widget-4,1004
|
108
|
-
=> nil
|
110
|
+
=> nil
|
109
111
|
>
|
110
112
|
```
|
111
113
|
|
@@ -167,3 +169,5 @@ bar2,bar2-description,,xyz98765
|
|
167
169
|
6. Commit your changes (`git commit -am 'Add some feature'`)
|
168
170
|
7. Push to the branch (`git push origin my-new-feature`)
|
169
171
|
8. Create new Pull Request
|
172
|
+
|
173
|
+
|
data/as_csv.gemspec
CHANGED
@@ -1,24 +1,25 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'as_csv/version'
|
2
|
+
require File.expand_path '../lib/as_csv/version', __FILE__
|
5
3
|
|
6
4
|
Gem::Specification.new do |gem|
|
7
5
|
gem.name = "as_csv"
|
8
6
|
gem.version = AsCSV::VERSION
|
9
7
|
gem.authors = ["Daniel Fone"]
|
10
|
-
gem.email = [
|
8
|
+
gem.email = %w[daniel@fone.net.nz]
|
11
9
|
gem.description = %q{Instant CSV support for Rails}
|
12
10
|
gem.summary = %q{Instant CSV support for Rails}
|
13
11
|
gem.homepage = "https://github.com/danielfone/as_csv"
|
14
12
|
|
15
13
|
gem.files = `git ls-files`.split($/)
|
16
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
14
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
-
gem.require_paths = [
|
15
|
+
gem.require_paths = %w[lib]
|
19
16
|
|
20
|
-
gem.
|
21
|
-
gem.
|
22
|
-
|
23
|
-
|
17
|
+
gem.add_runtime_dependency "activemodel", '>= 3.0'
|
18
|
+
gem.add_runtime_dependency "actionpack", '>= 3.0'
|
19
|
+
|
20
|
+
# Tests
|
21
|
+
gem.add_development_dependency "rails", ">= 3.0"
|
22
|
+
gem.add_development_dependency "sqlite3" unless defined? JRUBY_VERSION
|
23
|
+
gem.add_development_dependency "rspec-rails", "~> 3.0"
|
24
|
+
gem.add_development_dependency "simplecov"
|
24
25
|
end
|
@@ -1,7 +1,23 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'as_csv/csv_builder'
|
3
|
+
|
1
4
|
class Array
|
5
|
+
|
2
6
|
def to_csv_with_builder(opts={})
|
3
|
-
|
7
|
+
use_csv_builder? and csv_builder(opts).to_csv or to_csv_without_builder
|
4
8
|
end
|
9
|
+
|
5
10
|
alias_method :to_csv_without_builder, :to_csv
|
6
11
|
alias_method :to_csv, :to_csv_with_builder
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def use_csv_builder?
|
16
|
+
any? { |e| e.respond_to? :as_csv }
|
17
|
+
end
|
18
|
+
|
19
|
+
def csv_builder(opts)
|
20
|
+
AsCSV::CSVBuilder.new self, opts
|
21
|
+
end
|
22
|
+
|
7
23
|
end
|
data/lib/as_csv/csv_builder.rb
CHANGED
@@ -1,35 +1,45 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
1
3
|
module AsCSV
|
2
4
|
class CSVBuilder
|
3
5
|
attr_reader :records, :options
|
4
6
|
|
5
7
|
def initialize(records, options={})
|
6
|
-
@records =
|
8
|
+
@records = Array(records)
|
7
9
|
@options = options
|
10
|
+
validate
|
8
11
|
end
|
9
12
|
|
10
13
|
def to_csv
|
11
|
-
rows.collect { |row|
|
14
|
+
rows.collect { |row| CSV.generate_line row }.join
|
12
15
|
end
|
13
16
|
|
14
|
-
|
15
|
-
@rows ||= [headers] + data_rows if valid?
|
16
|
-
end
|
17
|
+
private
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
def rows
|
20
|
+
@rows ||= [headers] + data_rows
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def headers
|
24
|
+
@headers ||= csv_hashes.collect(&:keys).flatten.uniq
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def data_rows
|
28
|
+
@data_rows ||= csv_hashes.collect { |csv_hash| data_row csv_hash }
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
+
def validate
|
32
|
+
validate_members
|
33
|
+
validate_hashes
|
34
|
+
end
|
35
|
+
|
36
|
+
def validate_members
|
37
|
+
if records.any? { |r| not r.respond_to? :as_csv }
|
38
|
+
raise TypeError, 'Some elements do not respond to :as_csv'
|
39
|
+
end
|
40
|
+
end
|
31
41
|
|
32
|
-
def validate_hashes
|
42
|
+
def validate_hashes
|
33
43
|
csv_hashes.each do |hash|
|
34
44
|
raise TypeError, "expected as_csv to return Hash" unless hash.is_a? Hash
|
35
45
|
end
|
@@ -42,5 +52,5 @@ module AsCSV
|
|
42
52
|
def data_row(csv_hash)
|
43
53
|
headers.collect { |header| csv_hash[header] }
|
44
54
|
end
|
45
|
-
end
|
55
|
+
end
|
46
56
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'as_csv/csv_builder'
|
2
|
+
|
3
|
+
module ActiveModel::Serialization
|
4
|
+
def as_csv(options={})
|
5
|
+
attributes
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_csv(*args)
|
9
|
+
AsCSV::CSVBuilder.new(self, *args).to_csv
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActionController::Renderers.add :csv do |obj, options|
|
14
|
+
obj.respond_to?(:to_csv) ? obj.to_csv(options) : obj
|
15
|
+
end
|
data/lib/as_csv/version.rb
CHANGED
data/lib/as_csv.rb
CHANGED
data/script/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
data/spec/core_ext/array_spec.rb
CHANGED
@@ -1,27 +1,34 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'as_csv/core_ext/array'
|
2
3
|
|
3
4
|
describe Array do
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
subject(:array) do
|
6
|
+
[
|
7
|
+
double(:foo, :as_csv => {:header => '1'}),
|
8
|
+
double(:foo, :as_csv => {:header => '2'}),
|
9
|
+
double(:foo, :as_csv => {:header1 => '3'}),
|
10
|
+
]
|
11
|
+
end
|
7
12
|
|
8
|
-
|
9
|
-
|
13
|
+
describe '#to_csv' do
|
14
|
+
subject { array.to_csv }
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
subject.should_not_receive(:to_csv_without_builder)
|
14
|
-
subject.to_csv
|
16
|
+
it 'should use :as_csv on its elements if present' do
|
17
|
+
should eq "header,header1\n1,\n2,\n,3\n"
|
15
18
|
end
|
16
|
-
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
+
context 'with mixed elements' do
|
21
|
+
before { array << 1 }
|
22
|
+
it 'should raise a TypeError' do
|
23
|
+
expect { subject }.to raise_error TypeError
|
24
|
+
end
|
25
|
+
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
27
|
+
context 'with literals' do
|
28
|
+
before { array.clear; array << 1 }
|
29
|
+
it 'should not use :as_csv' do
|
30
|
+
should eq "1\n"
|
31
|
+
end
|
25
32
|
end
|
26
33
|
end
|
27
34
|
|
@@ -43,11 +43,6 @@ module Dummy
|
|
43
43
|
# like if you have constraints or database-specific column types
|
44
44
|
# config.active_record.schema_format = :sql
|
45
45
|
|
46
|
-
# Enforce whitelist mode for mass assignment.
|
47
|
-
# This will create an empty whitelist of attributes available for mass-assignment for all models
|
48
|
-
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
|
49
|
-
# parameters by using an attr_accessible or attr_protected declaration.
|
50
|
-
config.active_record.whitelist_attributes = true
|
51
46
|
|
52
47
|
end
|
53
48
|
end
|
@@ -29,9 +29,8 @@ Dummy::Application.configure do
|
|
29
29
|
# ActionMailer::Base.deliveries array.
|
30
30
|
config.action_mailer.delivery_method = :test
|
31
31
|
|
32
|
-
# Raise exception on mass assignment protection for Active Record models
|
33
|
-
config.active_record.mass_assignment_sanitizer = :strict
|
34
|
-
|
35
32
|
# Print deprecation notices to the stderr
|
36
33
|
config.active_support.deprecation = :stderr
|
34
|
+
|
35
|
+
config.eager_load = false
|
37
36
|
end
|
@@ -1,112 +1,76 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'as_csv/csv_builder'
|
2
3
|
|
3
4
|
describe AsCSV::CSVBuilder do
|
4
|
-
subject(:
|
5
|
+
subject(:builder) { described_class.new records, options }
|
5
6
|
let(:records) { nil }
|
6
7
|
let(:options) { Hash.new }
|
7
8
|
|
8
|
-
|
9
|
+
describe '#to_csv' do
|
10
|
+
subject { builder.to_csv }
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
context 'with single record' do
|
13
|
+
let(:records) do
|
14
|
+
double(:foo, :as_csv => {
|
15
|
+
:first => '1',
|
16
|
+
:second => '2',
|
17
|
+
:third => '3'
|
18
|
+
})
|
19
|
+
end
|
15
20
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
context 'with single record' do
|
21
|
-
let(:records) do
|
22
|
-
stub(:foo, :as_csv => {
|
23
|
-
:first => '1',
|
24
|
-
:second => '2',
|
25
|
-
:third => '3'
|
26
|
-
})
|
21
|
+
it 'should render correctly' do
|
22
|
+
should eq "first,second,third\n1,2,3\n"
|
23
|
+
end
|
27
24
|
end
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
first,
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
context 'with homogenous records' do
|
40
|
-
let(:records) { [record1, record2] }
|
41
|
-
2.times do |i|
|
42
|
-
let("record#{i+1}") do
|
43
|
-
stub(:foo, :as_csv => {
|
44
|
-
:first => "1#{i}",
|
45
|
-
:second => "2#{i}",
|
46
|
-
:third => "3#{i}"
|
47
|
-
})
|
26
|
+
context 'with homogenous records' do
|
27
|
+
let(:records) do
|
28
|
+
2.times.map do |i|
|
29
|
+
double(:foo, :as_csv => {
|
30
|
+
:first => "1#{i}",
|
31
|
+
:second => "2#{i}",
|
32
|
+
:third => "3#{i}"
|
33
|
+
})
|
34
|
+
end
|
48
35
|
end
|
49
|
-
end
|
50
36
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
should == <<-CSV
|
55
|
-
first,second,third
|
56
|
-
10,20,30
|
57
|
-
11,21,31
|
58
|
-
CSV
|
37
|
+
it 'should render correctly' do
|
38
|
+
should == "first,second,third\n10,20,30\n11,21,31\n"
|
39
|
+
end
|
59
40
|
end
|
60
|
-
end
|
61
41
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
42
|
+
context 'with hetreogenous records' do
|
43
|
+
let(:records) do
|
44
|
+
2.times.map do |i|
|
45
|
+
double(:foo, :as_csv => {
|
46
|
+
"first#{i}" => "1#{i}",
|
47
|
+
"second#{i}" => "2#{i}",
|
48
|
+
"third#{i}" => "3#{i}"
|
49
|
+
})
|
50
|
+
end
|
71
51
|
end
|
72
|
-
end
|
73
52
|
|
74
|
-
|
75
|
-
|
76
|
-
its(:to_csv) do
|
77
|
-
should == <<-CSV
|
78
|
-
first0,second0,third0,first1,second1,third1
|
53
|
+
it 'should merge headers' do
|
54
|
+
should == "first0,second0,third0,first1,second1,third1
|
79
55
|
10,20,30,,,
|
80
56
|
,,,11,21,31
|
81
|
-
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
context 'with nil record' do
|
86
|
-
let(:records) { nil }
|
87
|
-
|
88
|
-
it { should_not be_valid }
|
89
|
-
EXPOSED_METHODS.each do |method|
|
90
|
-
its(method) { should == nil }
|
57
|
+
"
|
58
|
+
end
|
91
59
|
end
|
92
|
-
end
|
93
60
|
|
94
|
-
|
95
|
-
|
61
|
+
context 'with record not respond_to? `as_csv`' do
|
62
|
+
let(:records) { double(:foo) }
|
96
63
|
|
97
|
-
|
98
|
-
|
99
|
-
|
64
|
+
it 'should raise an error' do
|
65
|
+
expect { builder.to_csv }.to raise_error TypeError
|
66
|
+
end
|
100
67
|
end
|
101
|
-
end
|
102
68
|
|
103
|
-
|
104
|
-
|
69
|
+
context 'with record `as_csv` != Hash' do
|
70
|
+
let(:records) { double(:foo, :as_csv => 'test') }
|
105
71
|
|
106
|
-
|
107
|
-
|
108
|
-
specify(%Q{`#{method}` should raise "#{message}"}) do
|
109
|
-
expect { csv_builder.send method }.to raise_error TypeError, message
|
72
|
+
it 'should raise an error' do
|
73
|
+
expect { builder.to_csv }.to raise_error TypeError
|
110
74
|
end
|
111
75
|
end
|
112
76
|
end
|
data/spec/models/widget_spec.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
require '
|
1
|
+
require 'rails_helper'
|
2
2
|
|
3
3
|
describe Widget do
|
4
4
|
it { should respond_to :to_csv }
|
5
5
|
it { should respond_to :as_csv }
|
6
6
|
|
7
7
|
describe 'dummy' do
|
8
|
-
subject(:dummy_widget) { Widget.
|
8
|
+
subject(:dummy_widget) { Widget.new :name => "widget-name", :description => 'widget-description', :code => 1234 }
|
9
9
|
|
10
10
|
describe :to_csv do
|
11
11
|
subject { dummy_widget.to_csv }
|
12
12
|
it do
|
13
13
|
should == <<-CSV.strip_heredoc
|
14
14
|
id,name,description,code
|
15
|
-
|
15
|
+
,widget-name,widget-description,1234
|
16
16
|
CSV
|
17
17
|
end
|
18
18
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require '
|
1
|
+
require 'rails_helper'
|
2
2
|
|
3
3
|
describe WidgetWithOptions do
|
4
|
-
subject(:widget) {
|
4
|
+
subject(:widget) { WidgetWithOptions.new :name => 'widget-name', :code => 1234 }
|
5
5
|
|
6
6
|
describe :to_csv do
|
7
7
|
subject { widget.to_csv(options) }
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Configure Rails Environment
|
4
|
+
ENV["RAILS_ENV"] = "test"
|
5
|
+
|
6
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
7
|
+
require 'rspec/rails'
|
8
|
+
|
9
|
+
Rails.backtrace_cleaner.remove_silencers!
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
13
|
+
# examples within a transaction, remove the following line or assign false
|
14
|
+
# instead of true.
|
15
|
+
config.use_transactional_fixtures = true
|
16
|
+
|
17
|
+
# If true, the base class of anonymous controllers will be inferred
|
18
|
+
# automatically. This will be the default behavior in future versions of
|
19
|
+
# rspec-rails.
|
20
|
+
config.infer_base_class_for_anonymous_controllers = false
|
21
|
+
|
22
|
+
config.infer_spec_type_from_file_location!
|
23
|
+
|
24
|
+
config.extend WidgetsControllerMixin, :widgets_controllers
|
25
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,29 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
|
-
require 'rspec/rails'
|
6
|
-
|
7
|
-
Rails.backtrace_cleaner.remove_silencers!
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
8
3
|
|
9
4
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
10
5
|
# in spec/support/ and its subdirectories.
|
11
6
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
12
7
|
|
13
8
|
RSpec.configure do |config|
|
14
|
-
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
15
|
-
# examples within a transaction, remove the following line or assign false
|
16
|
-
# instead of true.
|
17
|
-
config.use_transactional_fixtures = true
|
18
|
-
|
19
|
-
# If true, the base class of anonymous controllers will be inferred
|
20
|
-
# automatically. This will be the default behavior in future versions of
|
21
|
-
# rspec-rails.
|
22
|
-
config.infer_base_class_for_anonymous_controllers = false
|
23
|
-
|
24
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
25
|
-
|
26
9
|
config.order = "random"
|
27
|
-
|
28
|
-
config.extend WidgetsControllerMixin, :widgets_controllers
|
29
10
|
end
|
@@ -9,36 +9,30 @@ module WidgetsControllerMixin
|
|
9
9
|
]
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
describe 'GET show/1.csv' do
|
13
13
|
before { get :show, :id => 1, :format => :csv }
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
1,widget-1,widget-description-1,1001
|
22
|
-
CSV
|
23
|
-
end
|
14
|
+
it 'should respond with the CSV' do
|
15
|
+
expect(response.code).to eq "200"
|
16
|
+
expect(response.content_type).to eq "text/csv"
|
17
|
+
expect(response.body).to eq <<-CSV.strip_heredoc
|
18
|
+
id,name,description,code
|
19
|
+
1,widget-1,widget-description-1,1001
|
20
|
+
CSV
|
24
21
|
end
|
25
22
|
end
|
26
23
|
|
27
24
|
context 'GET index.csv' do
|
28
25
|
before { get :index, :format => :csv }
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
widget-4,1004,true
|
40
|
-
CSV
|
41
|
-
end
|
26
|
+
it 'should respond with the CSV' do
|
27
|
+
expect(response.code).to eq "200"
|
28
|
+
expect(response.content_type).to eq "text/csv"
|
29
|
+
expect(response.body).to eq <<-CSV.strip_heredoc
|
30
|
+
name,code,full
|
31
|
+
widget-1,1001,true
|
32
|
+
widget-2,1002,true
|
33
|
+
widget-3,1003,true
|
34
|
+
widget-4,1004,true
|
35
|
+
CSV
|
42
36
|
end
|
43
37
|
end
|
44
38
|
end
|
metadata
CHANGED
@@ -1,64 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: as_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Daniel Fone
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-08-06 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
14
41
|
- !ruby/object:Gem::Dependency
|
15
42
|
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
16
49
|
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
17
57
|
requirement: !ruby/object:Gem::Requirement
|
18
58
|
requirements:
|
19
|
-
- -
|
59
|
+
- - ">="
|
20
60
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
22
|
-
none: false
|
61
|
+
version: '0'
|
23
62
|
type: :development
|
63
|
+
prerelease: false
|
24
64
|
version_requirements: !ruby/object:Gem::Requirement
|
25
65
|
requirements:
|
26
|
-
- -
|
66
|
+
- - ">="
|
27
67
|
- !ruby/object:Gem::Version
|
28
|
-
version: '
|
29
|
-
none: false
|
68
|
+
version: '0'
|
30
69
|
- !ruby/object:Gem::Dependency
|
31
70
|
name: rspec-rails
|
32
|
-
prerelease: false
|
33
71
|
requirement: !ruby/object:Gem::Requirement
|
34
72
|
requirements:
|
35
|
-
- - ~>
|
73
|
+
- - "~>"
|
36
74
|
- !ruby/object:Gem::Version
|
37
|
-
version: '
|
38
|
-
none: false
|
75
|
+
version: '3.0'
|
39
76
|
type: :development
|
77
|
+
prerelease: false
|
40
78
|
version_requirements: !ruby/object:Gem::Requirement
|
41
79
|
requirements:
|
42
|
-
- - ~>
|
80
|
+
- - "~>"
|
43
81
|
- !ruby/object:Gem::Version
|
44
|
-
version: '
|
45
|
-
none: false
|
82
|
+
version: '3.0'
|
46
83
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
48
|
-
prerelease: false
|
84
|
+
name: simplecov
|
49
85
|
requirement: !ruby/object:Gem::Requirement
|
50
86
|
requirements:
|
51
|
-
- -
|
87
|
+
- - ">="
|
52
88
|
- !ruby/object:Gem::Version
|
53
89
|
version: '0'
|
54
|
-
none: false
|
55
90
|
type: :development
|
91
|
+
prerelease: false
|
56
92
|
version_requirements: !ruby/object:Gem::Requirement
|
57
93
|
requirements:
|
58
|
-
- -
|
94
|
+
- - ">="
|
59
95
|
- !ruby/object:Gem::Version
|
60
96
|
version: '0'
|
61
|
-
none: false
|
62
97
|
description: Instant CSV support for Rails
|
63
98
|
email:
|
64
99
|
- daniel@fone.net.nz
|
@@ -66,21 +101,20 @@ executables: []
|
|
66
101
|
extensions: []
|
67
102
|
extra_rdoc_files: []
|
68
103
|
files:
|
69
|
-
- .gitignore
|
70
|
-
- .
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
71
107
|
- Gemfile
|
72
|
-
- Gemfile.lock
|
73
108
|
- LICENSE.txt
|
74
109
|
- README.md
|
75
110
|
- Rakefile
|
76
111
|
- as_csv.gemspec
|
77
112
|
- lib/as_csv.rb
|
78
|
-
- lib/as_csv/action_renderer.rb
|
79
|
-
- lib/as_csv/active_model.rb
|
80
113
|
- lib/as_csv/core_ext/array.rb
|
81
114
|
- lib/as_csv/csv_builder.rb
|
82
|
-
- lib/as_csv/
|
115
|
+
- lib/as_csv/railtie.rb
|
83
116
|
- lib/as_csv/version.rb
|
117
|
+
- script/rspec
|
84
118
|
- spec/controllers/render_widgets_controller_spec.rb
|
85
119
|
- spec/controllers/respond_with_widgets_controller_spec.rb
|
86
120
|
- spec/core_ext/array_spec.rb
|
@@ -101,8 +135,6 @@ files:
|
|
101
135
|
- spec/dummy/config/boot.rb
|
102
136
|
- spec/dummy/config/database.yml
|
103
137
|
- spec/dummy/config/environment.rb
|
104
|
-
- spec/dummy/config/environments/development.rb
|
105
|
-
- spec/dummy/config/environments/production.rb
|
106
138
|
- spec/dummy/config/environments/test.rb
|
107
139
|
- spec/dummy/config/initializers/backtrace_silencers.rb
|
108
140
|
- spec/dummy/config/initializers/inflections.rb
|
@@ -125,31 +157,31 @@ files:
|
|
125
157
|
- spec/models/active_model_spec.rb
|
126
158
|
- spec/models/widget_spec.rb
|
127
159
|
- spec/models/widget_with_options_spec.rb
|
160
|
+
- spec/rails_helper.rb
|
128
161
|
- spec/spec_helper.rb
|
129
162
|
- spec/support/widgets_controller_mixin.rb
|
130
163
|
homepage: https://github.com/danielfone/as_csv
|
131
164
|
licenses: []
|
165
|
+
metadata: {}
|
132
166
|
post_install_message:
|
133
167
|
rdoc_options: []
|
134
168
|
require_paths:
|
135
169
|
- lib
|
136
170
|
required_ruby_version: !ruby/object:Gem::Requirement
|
137
171
|
requirements:
|
138
|
-
- -
|
172
|
+
- - ">="
|
139
173
|
- !ruby/object:Gem::Version
|
140
174
|
version: '0'
|
141
|
-
none: false
|
142
175
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
176
|
requirements:
|
144
|
-
- -
|
177
|
+
- - ">="
|
145
178
|
- !ruby/object:Gem::Version
|
146
179
|
version: '0'
|
147
|
-
none: false
|
148
180
|
requirements: []
|
149
181
|
rubyforge_project:
|
150
|
-
rubygems_version:
|
182
|
+
rubygems_version: 2.4.1
|
151
183
|
signing_key:
|
152
|
-
specification_version:
|
184
|
+
specification_version: 4
|
153
185
|
summary: Instant CSV support for Rails
|
154
186
|
test_files:
|
155
187
|
- spec/controllers/render_widgets_controller_spec.rb
|
@@ -172,8 +204,6 @@ test_files:
|
|
172
204
|
- spec/dummy/config/boot.rb
|
173
205
|
- spec/dummy/config/database.yml
|
174
206
|
- spec/dummy/config/environment.rb
|
175
|
-
- spec/dummy/config/environments/development.rb
|
176
|
-
- spec/dummy/config/environments/production.rb
|
177
207
|
- spec/dummy/config/environments/test.rb
|
178
208
|
- spec/dummy/config/initializers/backtrace_silencers.rb
|
179
209
|
- spec/dummy/config/initializers/inflections.rb
|
@@ -196,5 +226,6 @@ test_files:
|
|
196
226
|
- spec/models/active_model_spec.rb
|
197
227
|
- spec/models/widget_spec.rb
|
198
228
|
- spec/models/widget_with_options_spec.rb
|
229
|
+
- spec/rails_helper.rb
|
199
230
|
- spec/spec_helper.rb
|
200
231
|
- spec/support/widgets_controller_mixin.rb
|
data/Gemfile.lock
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
as_csv (1.0.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
actionmailer (3.2.11)
|
10
|
-
actionpack (= 3.2.11)
|
11
|
-
mail (~> 2.4.4)
|
12
|
-
actionpack (3.2.11)
|
13
|
-
activemodel (= 3.2.11)
|
14
|
-
activesupport (= 3.2.11)
|
15
|
-
builder (~> 3.0.0)
|
16
|
-
erubis (~> 2.7.0)
|
17
|
-
journey (~> 1.0.4)
|
18
|
-
rack (~> 1.4.0)
|
19
|
-
rack-cache (~> 1.2)
|
20
|
-
rack-test (~> 0.6.1)
|
21
|
-
sprockets (~> 2.2.1)
|
22
|
-
activemodel (3.2.11)
|
23
|
-
activesupport (= 3.2.11)
|
24
|
-
builder (~> 3.0.0)
|
25
|
-
activerecord (3.2.11)
|
26
|
-
activemodel (= 3.2.11)
|
27
|
-
activesupport (= 3.2.11)
|
28
|
-
arel (~> 3.0.2)
|
29
|
-
tzinfo (~> 0.3.29)
|
30
|
-
activeresource (3.2.11)
|
31
|
-
activemodel (= 3.2.11)
|
32
|
-
activesupport (= 3.2.11)
|
33
|
-
activesupport (3.2.11)
|
34
|
-
i18n (~> 0.6)
|
35
|
-
multi_json (~> 1.0)
|
36
|
-
arel (3.0.2)
|
37
|
-
builder (3.0.4)
|
38
|
-
diff-lcs (1.1.3)
|
39
|
-
erubis (2.7.0)
|
40
|
-
hike (1.2.1)
|
41
|
-
i18n (0.6.1)
|
42
|
-
journey (1.0.4)
|
43
|
-
json (1.7.6)
|
44
|
-
mail (2.4.4)
|
45
|
-
i18n (>= 0.4.0)
|
46
|
-
mime-types (~> 1.16)
|
47
|
-
treetop (~> 1.4.8)
|
48
|
-
mime-types (1.19)
|
49
|
-
multi_json (1.5.0)
|
50
|
-
polyglot (0.3.3)
|
51
|
-
rack (1.4.3)
|
52
|
-
rack-cache (1.2)
|
53
|
-
rack (>= 0.4)
|
54
|
-
rack-ssl (1.3.2)
|
55
|
-
rack
|
56
|
-
rack-test (0.6.2)
|
57
|
-
rack (>= 1.0)
|
58
|
-
rails (3.2.11)
|
59
|
-
actionmailer (= 3.2.11)
|
60
|
-
actionpack (= 3.2.11)
|
61
|
-
activerecord (= 3.2.11)
|
62
|
-
activeresource (= 3.2.11)
|
63
|
-
activesupport (= 3.2.11)
|
64
|
-
bundler (~> 1.0)
|
65
|
-
railties (= 3.2.11)
|
66
|
-
railties (3.2.11)
|
67
|
-
actionpack (= 3.2.11)
|
68
|
-
activesupport (= 3.2.11)
|
69
|
-
rack-ssl (~> 1.3.2)
|
70
|
-
rake (>= 0.8.7)
|
71
|
-
rdoc (~> 3.4)
|
72
|
-
thor (>= 0.14.6, < 2.0)
|
73
|
-
rake (10.0.3)
|
74
|
-
rdoc (3.12)
|
75
|
-
json (~> 1.4)
|
76
|
-
rspec-core (2.12.2)
|
77
|
-
rspec-expectations (2.12.1)
|
78
|
-
diff-lcs (~> 1.1.3)
|
79
|
-
rspec-mocks (2.12.1)
|
80
|
-
rspec-rails (2.12.2)
|
81
|
-
actionpack (>= 3.0)
|
82
|
-
activesupport (>= 3.0)
|
83
|
-
railties (>= 3.0)
|
84
|
-
rspec-core (~> 2.12.0)
|
85
|
-
rspec-expectations (~> 2.12.0)
|
86
|
-
rspec-mocks (~> 2.12.0)
|
87
|
-
sprockets (2.2.2)
|
88
|
-
hike (~> 1.2)
|
89
|
-
multi_json (~> 1.0)
|
90
|
-
rack (~> 1.0)
|
91
|
-
tilt (~> 1.1, != 1.3.0)
|
92
|
-
sqlite3 (1.3.7)
|
93
|
-
thor (0.16.0)
|
94
|
-
tilt (1.3.3)
|
95
|
-
treetop (1.4.12)
|
96
|
-
polyglot
|
97
|
-
polyglot (>= 0.3.1)
|
98
|
-
tzinfo (0.3.35)
|
99
|
-
|
100
|
-
PLATFORMS
|
101
|
-
ruby
|
102
|
-
|
103
|
-
DEPENDENCIES
|
104
|
-
as_csv!
|
105
|
-
rails (~> 3.2)
|
106
|
-
rspec-rails (~> 2.12)
|
107
|
-
sqlite3
|
data/lib/as_csv/active_model.rb
DELETED
data/lib/as_csv/csv_proxy.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
if RUBY_VERSION >= '1.9'
|
2
|
-
require 'csv'
|
3
|
-
AsCSV::CSVProxy = CSV
|
4
|
-
else
|
5
|
-
begin
|
6
|
-
gem 'fastercsv'
|
7
|
-
require 'fastercsv'
|
8
|
-
|
9
|
-
AsCSV::CSVProxy = FasterCSV
|
10
|
-
rescue LoadError => e
|
11
|
-
raise "Error : FasterCSV not installed, please `gem install fastercsv` for faster processing on Ruby 1.8"
|
12
|
-
end
|
13
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
Dummy::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 web server 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_controller.perform_caching = false
|
15
|
-
|
16
|
-
# Don't care if the mailer can't send
|
17
|
-
config.action_mailer.raise_delivery_errors = false
|
18
|
-
|
19
|
-
# Print deprecation notices to the Rails logger
|
20
|
-
config.active_support.deprecation = :log
|
21
|
-
|
22
|
-
# Only use best-standards-support built into browsers
|
23
|
-
config.action_dispatch.best_standards_support = :builtin
|
24
|
-
|
25
|
-
# Raise exception on mass assignment protection for Active Record models
|
26
|
-
config.active_record.mass_assignment_sanitizer = :strict
|
27
|
-
|
28
|
-
# Log the query plan for queries taking more than this (works
|
29
|
-
# with SQLite, MySQL, and PostgreSQL)
|
30
|
-
config.active_record.auto_explain_threshold_in_seconds = 0.5
|
31
|
-
|
32
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
Dummy::Application.configure do
|
2
|
-
# Settings specified here will take precedence over those in config/application.rb
|
3
|
-
|
4
|
-
# Code is not reloaded between requests
|
5
|
-
config.cache_classes = true
|
6
|
-
|
7
|
-
# Full error reports are disabled and caching is turned on
|
8
|
-
config.consider_all_requests_local = false
|
9
|
-
config.action_controller.perform_caching = true
|
10
|
-
|
11
|
-
# Disable Rails's static asset server (Apache or nginx will already do this)
|
12
|
-
config.serve_static_assets = false
|
13
|
-
|
14
|
-
|
15
|
-
# Specifies the header that your server uses for sending files
|
16
|
-
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
|
17
|
-
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
|
18
|
-
|
19
|
-
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
|
20
|
-
# config.force_ssl = true
|
21
|
-
|
22
|
-
# See everything in the log (default is :info)
|
23
|
-
# config.log_level = :debug
|
24
|
-
|
25
|
-
# Prepend all log lines with the following tags
|
26
|
-
# config.log_tags = [ :subdomain, :uuid ]
|
27
|
-
|
28
|
-
# Use a different logger for distributed setups
|
29
|
-
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
|
30
|
-
|
31
|
-
# Use a different cache store in production
|
32
|
-
# config.cache_store = :mem_cache_store
|
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
|
-
|
38
|
-
# Disable delivery errors, bad email addresses will be ignored
|
39
|
-
# config.action_mailer.raise_delivery_errors = false
|
40
|
-
|
41
|
-
# Enable threaded mode
|
42
|
-
# config.threadsafe!
|
43
|
-
|
44
|
-
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
45
|
-
# the I18n.default_locale when a translation can not be found)
|
46
|
-
config.i18n.fallbacks = true
|
47
|
-
|
48
|
-
# Send deprecation notices to registered listeners
|
49
|
-
config.active_support.deprecation = :notify
|
50
|
-
|
51
|
-
# Log the query plan for queries taking more than this (works
|
52
|
-
# with SQLite, MySQL, and PostgreSQL)
|
53
|
-
# config.active_record.auto_explain_threshold_in_seconds = 0.5
|
54
|
-
end
|