render_csv 2.0.0.beta1 → 2.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0456d41778a62cf6042dcf1afe16f46e54967ae3
4
- data.tar.gz: aeae7731318782ed808fecb6b8eb43c1ac8b03ef
3
+ metadata.gz: 025d2659ed54a44720cc68aa22e350f491b81499
4
+ data.tar.gz: 9a2a6a3adb7d7d6841952f71e25a82435c4e481e
5
5
  SHA512:
6
- metadata.gz: 84a934923c771e3f17aa484e95c9cc4dbfa81fef0fc6d9d4e19db8afd14b70da94b8b22b279a517353abc48f3e546f2581243c8cfc8b5a382b38f4a945b61217
7
- data.tar.gz: b298832ae25962b4c9ade57b9485f63557a6a1512c0eba8f2fde6689e1a8d4e31ef3b5d45b01594fbbad6cc31ab5456dfec064436bff78f91020e13ccebf780d
6
+ metadata.gz: 68b2c4b8dce6649ae31858e30e393c97a76a76d54e9a00a9b3e0f10f635742de15b1a3058da4e86a128d55b3a3f302d2b937ae9e461f427231b42a77916b1186
7
+ data.tar.gz: 60f6154159934f898838e3b2a66a2faf73b6746d91c6866c96992421ea29e253a67eb960e8e46aa9cab567897528b7e0cdf52e965ff7431e5cd5772d983d7a6f
data/.travis.yml CHANGED
@@ -3,6 +3,7 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.0
6
+ - 2.1.1
6
7
  gemfile:
7
8
  - gemfiles/Gemfile.rails-3.0.x
8
9
  - gemfiles/Gemfile.rails-3.1.x
data/CHANGELOG.md CHANGED
@@ -7,6 +7,7 @@
7
7
  loaded before it tries to register itself. Should fix intermittent
8
8
  issues with it not working in production environments.
9
9
  * Use Ruby's CSV library instead of manually building strings.
10
+ * Does not extend the array class anymore
10
11
 
11
12
  ## 1.0.0
12
13
 
data/README.md CHANGED
@@ -11,7 +11,7 @@ Rails CSV renderer for ActiveRecord collections
11
11
 
12
12
  *Rails:* 3.0.x - 4.0.x
13
13
 
14
- *Ruby:* 1.9.2, 1.9.3 and 2.0.0
14
+ *Ruby:* 1.9.3, 2.0.0 and 2.1.0
15
15
 
16
16
  ## Installation
17
17
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module RenderCsv
2
- VERSION = "2.0.0.beta1"
2
+ VERSION = "2.0.0.beta2"
3
3
  end
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/extension'
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/extension'
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.beta1
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-19 00:00:00.000000000 Z
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/extension.rb
90
+ - lib/render_csv/csv_renderable.rb
91
91
  - lib/render_csv/version.rb
92
92
  - render_csv.gemspec
93
- - spec/lib/array_spec.rb
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.1
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/array_spec.rb
120
+ - spec/lib/csv_renderable_spec.rb
121
121
  - spec/spec_helper.rb
@@ -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
@@ -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