render_csv 2.0.0.beta1 → 2.0.0.beta2
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 +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +1 -0
- data/README.md +1 -1
- data/lib/render_csv/csv_renderable.rb +27 -0
- data/lib/render_csv/version.rb +1 -1
- data/lib/render_csv.rb +2 -1
- data/spec/lib/csv_renderable_spec.rb +69 -0
- data/spec/spec_helper.rb +1 -1
- metadata +6 -6
- data/lib/render_csv/extension.rb +0 -26
- data/spec/lib/array_spec.rb +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 025d2659ed54a44720cc68aa22e350f491b81499
|
4
|
+
data.tar.gz: 9a2a6a3adb7d7d6841952f71e25a82435c4e481e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68b2c4b8dce6649ae31858e30e393c97a76a76d54e9a00a9b3e0f10f635742de15b1a3058da4e86a128d55b3a3f302d2b937ae9e461f427231b42a77916b1186
|
7
|
+
data.tar.gz: 60f6154159934f898838e3b2a66a2faf73b6746d91c6866c96992421ea29e253a67eb960e8e46aa9cab567897528b7e0cdf52e965ff7431e5cd5772d983d7a6f
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module RenderCsv
|
4
|
+
module CsvRenderable
|
5
|
+
# Converts an array to CSV formatted string
|
6
|
+
# Options include:
|
7
|
+
# :only => [:col1, :col2] # Specify which columns to include
|
8
|
+
# :except => [:col1, :col2] # Specify which columns to exclude
|
9
|
+
# :add_methods => [:method1, :method2] # Include addtional methods that aren't columns
|
10
|
+
def to_csv(options = {})
|
11
|
+
return '' if empty?
|
12
|
+
return join(',') unless first.class.respond_to? :column_names
|
13
|
+
|
14
|
+
columns = first.class.column_names
|
15
|
+
columns &= options[:only].map(&:to_s) if options[:only]
|
16
|
+
columns -= options[:except].map(&:to_s) if options[:except]
|
17
|
+
columns += options[:add_methods].map(&:to_s) if options[:add_methods]
|
18
|
+
|
19
|
+
CSV.generate(encoding: 'utf-8') do |row|
|
20
|
+
row << columns
|
21
|
+
self.each do |obj|
|
22
|
+
row << columns.map { |c| obj.send(c) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/render_csv/version.rb
CHANGED
data/lib/render_csv.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
module RenderCsv
|
2
2
|
class RenderCsvRailtie < ::Rails::Railtie
|
3
3
|
config.after_initialize do
|
4
|
-
require 'render_csv/
|
4
|
+
require 'render_csv/csv_renderable'
|
5
5
|
require 'action_controller/metal/renderers'
|
6
6
|
|
7
7
|
ActionController.add_renderer :csv do |csv, options|
|
8
8
|
filename = options[:filename] || options[:template]
|
9
|
+
csv.extend RenderCsv::CsvRenderable
|
9
10
|
data = csv.respond_to?(:to_csv) ? csv.to_csv(options) : csv
|
10
11
|
send_data data, type: Mime::CSV, disposition: "attachment; filename=#{filename}.csv"
|
11
12
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require './spec/spec_helper'
|
2
|
+
|
3
|
+
describe RenderCsv::CsvRenderable do
|
4
|
+
let(:csv_renderable_array) { array.extend RenderCsv::CsvRenderable }
|
5
|
+
|
6
|
+
context 'object is an array' do
|
7
|
+
|
8
|
+
context 'array is empty' do
|
9
|
+
let(:array) { Array.new }
|
10
|
+
|
11
|
+
it 'returns an empty string' do
|
12
|
+
expect(csv_renderable_array.to_csv).to eql('')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'object is not an instance of AR class' do
|
17
|
+
let(:array) { [1, 2, 3] }
|
18
|
+
|
19
|
+
it 'returns a csv of the array' do
|
20
|
+
expect(csv_renderable_array.to_csv).to eql('1,2,3')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'object is an ActiveRecord object' do
|
26
|
+
let(:sebastian) { Dog.create!(name: "Sebastian O'Connor", age: 3, weight: 76.8) }
|
27
|
+
let(:ruby) { Dog.create!(name: 'Ruby', age: 3, weight: 68.2) }
|
28
|
+
let(:shelby) { Dog.create!(name: 'Shelby', age: 5, weight: 64.0) }
|
29
|
+
let(:array) { [sebastian, ruby, shelby] }
|
30
|
+
|
31
|
+
context 'options is blank' do
|
32
|
+
it 'returns all columns' do
|
33
|
+
expect(csv_renderable_array.to_csv).to eql "id,name,age,weight\n1,Sebastian O'Connor,3,76.8\n2,Ruby,3,68.2\n3,Shelby,5,64.0\n"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'options with :only param' do
|
38
|
+
it 'returns only columns specified' do
|
39
|
+
options = { only: [:name, :age] }
|
40
|
+
|
41
|
+
expect(csv_renderable_array.to_csv(options)).to eql "name,age\nSebastian O'Connor,3\nRuby,3\nShelby,5\n"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'options with :exclude param' do
|
46
|
+
it 'excludes columns specified' do
|
47
|
+
options = { except: [:age] }
|
48
|
+
|
49
|
+
expect(csv_renderable_array.to_csv(options)).to eql "id,name,weight\n7,Sebastian O'Connor,76.8\n8,Ruby,68.2\n9,Shelby,64.0\n"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'options with :add_methods' do
|
54
|
+
it 'includes specified method values' do
|
55
|
+
options = { add_methods: [:human_age] }
|
56
|
+
|
57
|
+
expect(csv_renderable_array.to_csv(options)).to eql "id,name,age,weight,human_age\n10,Sebastian O'Connor,3,76.8,25\n11,Ruby,3,68.2,25\n12,Shelby,5,64.0,33\n"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'options with :expect and :add_methods' do
|
62
|
+
it 'includes method values with other options' do
|
63
|
+
options = { except: [:id,:name], add_methods: [:human_age] }
|
64
|
+
|
65
|
+
expect(csv_renderable_array.to_csv(options)).to eql "age,weight,human_age\n3,76.8,25\n3,68.2,25\n5,64.0,33\n"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
3
3
|
|
4
4
|
require 'rails/all'
|
5
5
|
require 'rspec/rails'
|
6
|
-
require 'render_csv/
|
6
|
+
require 'render_csv/csv_renderable'
|
7
7
|
|
8
8
|
# Requires supporting files with custom matchers and macros, etc,
|
9
9
|
# in ./support/ and its subdirectories.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.
|
4
|
+
version: 2.0.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Brown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -87,10 +87,10 @@ files:
|
|
87
87
|
- gemfiles/Gemfile.rails-3.2.x
|
88
88
|
- gemfiles/Gemfile.rails-4.0.x
|
89
89
|
- lib/render_csv.rb
|
90
|
-
- lib/render_csv/
|
90
|
+
- lib/render_csv/csv_renderable.rb
|
91
91
|
- lib/render_csv/version.rb
|
92
92
|
- render_csv.gemspec
|
93
|
-
- spec/lib/
|
93
|
+
- spec/lib/csv_renderable_spec.rb
|
94
94
|
- spec/spec_helper.rb
|
95
95
|
homepage: http://github.com/beerlington/render_csv
|
96
96
|
licenses:
|
@@ -112,10 +112,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
112
|
version: 1.3.1
|
113
113
|
requirements: []
|
114
114
|
rubyforge_project:
|
115
|
-
rubygems_version: 2.2.
|
115
|
+
rubygems_version: 2.2.2
|
116
116
|
signing_key:
|
117
117
|
specification_version: 4
|
118
118
|
summary: Adds a custom CSV renderer to Rails applications
|
119
119
|
test_files:
|
120
|
-
- spec/lib/
|
120
|
+
- spec/lib/csv_renderable_spec.rb
|
121
121
|
- spec/spec_helper.rb
|
data/lib/render_csv/extension.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'csv'
|
2
|
-
|
3
|
-
class Array
|
4
|
-
|
5
|
-
# Converts an array to CSV formatted string
|
6
|
-
# Options include:
|
7
|
-
# :only => [:col1, :col2] # Specify which columns to include
|
8
|
-
# :except => [:col1, :col2] # Specify which columns to exclude
|
9
|
-
# :add_methods => [:method1, :method2] # Include addtional methods that aren't columns
|
10
|
-
def to_csv(options={})
|
11
|
-
return '' if empty?
|
12
|
-
return join(',') unless first.class.respond_to? :column_names
|
13
|
-
|
14
|
-
columns = first.class.column_names
|
15
|
-
columns &= options[:only].map(&:to_s) if options[:only]
|
16
|
-
columns -= options[:except].map(&:to_s) if options[:except]
|
17
|
-
columns += options[:add_methods].map(&:to_s) if options[:add_methods]
|
18
|
-
|
19
|
-
CSV.generate(encoding: 'utf-8') do |row|
|
20
|
-
row << columns
|
21
|
-
self.each do |obj|
|
22
|
-
row << columns.map { |c| obj.send(c) }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/spec/lib/array_spec.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
require './spec/spec_helper'
|
2
|
-
|
3
|
-
describe Array do
|
4
|
-
let(:sebastian) { Dog.create!(name: "Sebastian O'Connor", age: 3, weight: 76.8) }
|
5
|
-
let(:ruby) { Dog.create!(name: 'Ruby', age: 3, weight: 68.2) }
|
6
|
-
let(:shelby) { Dog.create!(name: 'Shelby', age: 5, weight: 64.0) }
|
7
|
-
let(:array) { [sebastian, ruby, shelby] }
|
8
|
-
|
9
|
-
context 'array is empty' do
|
10
|
-
it 'returns an empty string' do
|
11
|
-
expect([].to_csv).to eql('')
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context 'object is not an instance of AR class' do
|
16
|
-
it 'returns a csv of the array' do
|
17
|
-
expect([1, 2, 3].to_csv).to eql('1,2,3')
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'options is blank' do
|
22
|
-
it 'returns all columns' do
|
23
|
-
expect(array.to_csv).to eql "id,name,age,weight\n1,Sebastian O'Connor,3,76.8\n2,Ruby,3,68.2\n3,Shelby,5,64.0\n"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
context 'options with :only param' do
|
28
|
-
it 'returns only columns specified' do
|
29
|
-
options = { only: [:name, :age] }
|
30
|
-
|
31
|
-
expect(array.to_csv(options)).to eql "name,age\nSebastian O'Connor,3\nRuby,3\nShelby,5\n"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'options with :exclude param' do
|
36
|
-
it 'excludes columns specified' do
|
37
|
-
options = { except: [:age] }
|
38
|
-
|
39
|
-
expect(array.to_csv(options)).to eql "id,name,weight\n7,Sebastian O'Connor,76.8\n8,Ruby,68.2\n9,Shelby,64.0\n"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context 'options with :add_methods' do
|
44
|
-
it 'includes specified method values' do
|
45
|
-
options = { add_methods: [:human_age] }
|
46
|
-
|
47
|
-
expect(array.to_csv(options)).to eql "id,name,age,weight,human_age\n10,Sebastian O'Connor,3,76.8,25\n11,Ruby,3,68.2,25\n12,Shelby,5,64.0,33\n"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'options with :expect and :add_methods' do
|
52
|
-
it 'includes method values with other options' do
|
53
|
-
options = { except: [:id,:name], add_methods: [:human_age] }
|
54
|
-
|
55
|
-
expect(array.to_csv(options)).to eql "age,weight,human_age\n3,76.8,25\n3,68.2,25\n5,64.0,33\n"
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|