jeffleeismyhero-render_csv 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd71cef27aadeeaa0b23e8e2dd0351954fdffac2
4
+ data.tar.gz: 755f0d8b9925d4472ffbb68ca318d7d30f9c6ace
5
+ SHA512:
6
+ metadata.gz: bd18388cc7de7f1ba789cf9a1f88c322d1f1a1eba7604a8c2046b78bb2eb155910942c3131d04523aa3d73530a4e88e7eaed7040b19043b9b272ef1edcc6e137
7
+ data.tar.gz: 2c356fe2931b947fab9ac4b0fcc4f6a44347d291b5aebdce6e1614145885e2a7f5ed5fe34c32c8bf5a522a077597d7fecc6c7528749f3453df607ad0d48e6e32
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.1.1
7
+ gemfile:
8
+ - gemfiles/Gemfile.rails-3.0.x
9
+ - gemfiles/Gemfile.rails-3.1.x
10
+ - gemfiles/Gemfile.rails-3.2.x
11
+ - gemfiles/Gemfile.rails-4.0.x
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ ## 2.1.0
4
+
5
+ * Support for custom header row
6
+
7
+ ## 2.1.0 (Beta)
8
+
9
+ * Support rendering objects that already respond to `to_csv`
10
+
11
+ ## 2.0.0
12
+
13
+ * Removes support for Ruby 1.8.7.
14
+ * Moves renderer into an after_initialize block so the Rails app is
15
+ loaded before it tries to register itself. Should fix intermittent
16
+ issues with it not working in production environments.
17
+ * Use Ruby's CSV library instead of manually building strings.
18
+ * Does not extend the array class anymore
19
+
20
+ ## 1.0.0
21
+
22
+ * Initial Release
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,8 @@
1
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
2
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
3
+ * Fork the project
4
+ * Start a feature/bugfix branch
5
+ * Commit and push until you are happy with your contribution
6
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
7
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
8
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Peter Brown
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,100 @@
1
+ # render_csv
2
+
3
+ [![Build Status](https://travis-ci.org/beerlington/render_csv.png?branch=master)](https://travis-ci.org/beerlington/render_csv)
4
+ [![Gem Version](https://badge.fury.io/rb/render_csv.png)](http://badge.fury.io/rb/render_csv)
5
+ [![Code Climate](https://codeclimate.com/github/beerlington/render_csv.png)](https://codeclimate.com/github/beerlington/render_csv)
6
+ [![Dependency Status](https://gemnasium.com/beerlington/render_csv.png)](https://gemnasium.com/beerlington/render_csv)
7
+
8
+ Rails CSV renderer for ActiveRecord collections
9
+
10
+ ## Rails & Ruby Versions Supported
11
+
12
+ *Rails:* 3.0.x - 4.0.x
13
+
14
+ *Ruby:* 1.9.3, 2.0.0 and 2.1.0
15
+
16
+ ## Installation
17
+
18
+ The gem is hosted at [rubygems.org](https://rubygems.org/gems/render_csv)
19
+
20
+ ## What is it?
21
+
22
+ The CSV renderer allows you to render any collection as CSV data.
23
+
24
+ ```ruby
25
+ class LocationsController < ApplicationController
26
+ def index
27
+ @locations = Location.all
28
+
29
+ respond_to do |format|
30
+ format.csv { render csv: @locations }
31
+ end
32
+ end
33
+ end
34
+ ```
35
+
36
+ Will render a CSV file similar to:
37
+
38
+ <table>
39
+ <tr>
40
+ <th>name</th><th>address</th><th>city</th><th>state</th><th>zip</th><th>created_at</th><th>updated_at</th>
41
+ </tr>
42
+ <tr>
43
+ <td>Pete's House</td><td>555 House Ln</td><td>Burlington</td><td>VT</td><td>05401</td><td>2011-07-26 03:12:44 UTC</td><td>2011-07-26 03:12:44 UTC</td>
44
+ </tr>
45
+ <tr>
46
+ <td>Sebastians's House</td><td>123 Pup St</td><td>Burlington</td><td>VT</td><td>05401</td><td>2011-07-26 03:30:44 UTC</td><td>2011-07-26 03:30:44 UTC</td>
47
+ </tr>
48
+ <tr>
49
+ <td>Someone Else</td><td>999 Herp Derp</td><td>Burlington</td><td>VT</td><td>05401</td><td>2011-07-26 03:30:44 UTC</td><td>2011-07-26 03:30:44 UTC</td>
50
+ </tr>
51
+ </table>
52
+
53
+ ## Usage Options
54
+
55
+ There are a few options you can use to customize which columns are included in the CSV file
56
+
57
+ ### Exclude columns
58
+
59
+ ```ruby
60
+ respond_to do |format|
61
+ format.csv { render csv: @locations, except: [:id] }
62
+ end
63
+ ```
64
+
65
+ ### Limit columns
66
+
67
+ ```ruby
68
+ respond_to do |format|
69
+ format.csv { render csv: @locations, only: [:address, :zip] }
70
+ end
71
+ ```
72
+
73
+ ### Add methods as columns
74
+
75
+ ```ruby
76
+ respond_to do |format|
77
+ format.csv { render csv: @locations, add_methods: [:method1, :method2] }
78
+ end
79
+ ```
80
+
81
+ ### Add methods as columns and exclude columns
82
+
83
+ ```ruby
84
+ respond_to do |format|
85
+ format.csv { render csv: @locations, except: [:id], add_methods: [:method1, :method2] }
86
+ end
87
+ ```
88
+
89
+ ### Specify a custom header row
90
+
91
+ ```ruby
92
+ respond_to do |format|
93
+ format.csv { render csv: @locations, header: 'Address', 'Zip Code', only: [:address, :zip] }
94
+ end
95
+ ```
96
+
97
+ ## Copyright
98
+
99
+ Copyright © 2011-2014 Peter Brown. See LICENSE.txt for
100
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '~> 3.0.0'
4
+ gem 'rspec-rails', '~> 2.11.0'
5
+ gem 'sqlite3-ruby', :require => 'sqlite3'
6
+ gem 'json', '~> 1.6.5'
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '~> 3.1.0'
4
+ gem 'rspec-rails', '~> 2.11.0'
5
+ gem 'sqlite3'
6
+ gem 'json', '~> 1.6.5'
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '~> 3.2.0'
4
+ gem 'rspec-rails', '~> 2.11.0'
5
+ gem 'sqlite3'
6
+ gem 'json', '~> 1.6.5'
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '~> 4.0.0'
4
+ gem 'rspec-rails', '~> 2.11'
5
+ gem 'sqlite3'
6
+ gem 'json', '~> 1.7'
data/lib/render_csv.rb ADDED
@@ -0,0 +1,15 @@
1
+ module RenderCsv
2
+ class RenderCsvRailtie < ::Rails::Railtie
3
+ config.after_initialize do
4
+ require 'render_csv/csv_renderable'
5
+ require 'action_controller/metal/renderers'
6
+
7
+ ActionController.add_renderer :csv do |csv, options|
8
+ filename = options[:filename] || options[:template]
9
+ csv.extend RenderCsv::CsvRenderable unless csv.respond_to?(:to_csv)
10
+ data = csv.to_csv(options)
11
+ send_data data, type: Mime::CSV, disposition: "attachment; filename=#{filename}.csv"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
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
+ # :header => ['Col1', 'Col2'] # Explicitly set the header
11
+ def to_csv(options = {})
12
+ return '' if empty?
13
+ return join(',') unless first.class.respond_to? :column_names
14
+
15
+ columns = first.class.column_names
16
+ columns &= options[:only].map(&:to_s) if options[:only]
17
+ columns -= options[:except].map(&:to_s) if options[:except]
18
+ columns += options[:add_methods].map(&:to_s) if options[:add_methods]
19
+
20
+ CSV.generate(encoding: 'utf-8') do |row|
21
+ row << options[:header] ||= columns
22
+ self.each do |obj|
23
+ row << columns.map { |c| obj.send(c) }
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module RenderCsv
2
+ VERSION = "2.1.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/render_csv/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Peter Brown"]
6
+ gem.email = ["github@lette.us"]
7
+ gem.description = "Adds a custom CSV renderer to Rails applications"
8
+ gem.summary = "Adds a custom CSV renderer to Rails applications"
9
+ gem.homepage = "http://github.com/beerlington/render_csv"
10
+ gem.license = "MIT"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "jeffleeismyhero-render_csv"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RenderCsv::VERSION
17
+
18
+ gem.add_dependency('rails', '>= 3.0')
19
+
20
+ gem.add_development_dependency('rspec-rails', '>= 2.12')
21
+ gem.add_development_dependency('sqlite3', '>= 1.3')
22
+ gem.add_development_dependency('json', '>= 1.6')
23
+
24
+ end
@@ -0,0 +1,77 @@
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
+
69
+ context 'options with :header param' do
70
+ it 'returns header specified' do
71
+ options = { header: ['Name', 'Age'], only: [:name, :age] }
72
+
73
+ expect(csv_renderable_array.to_csv(options)).to eql "Name,Age\nSebastian O'Connor,3\nRuby,3\nShelby,5\n"
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rails/all'
5
+ require 'rspec/rails'
6
+ require 'render_csv/csv_renderable'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+ config.color = true
14
+ end
15
+
16
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
17
+
18
+ ActiveRecord::Schema.define(:version => 1) do
19
+ create_table :dogs, :force => true do |t|
20
+ t.string :name
21
+ t.integer :age
22
+ t.float :weight
23
+ end
24
+ end
25
+
26
+ class Dog < ActiveRecord::Base
27
+ validates_presence_of :name, :age, :weight
28
+
29
+ def human_age
30
+ if age <= 2
31
+ (age * 10.5).to_i
32
+ else
33
+ (2 * 10.5 + (age - 2) * 4).to_i
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jeffleeismyhero-render_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Brown
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
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: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '2.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '2.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ description: Adds a custom CSV renderer to Rails applications
70
+ email:
71
+ - github@lette.us
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - CHANGELOG.md
80
+ - CONTRIBUTING.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - gemfiles/Gemfile.rails-3.0.x
86
+ - gemfiles/Gemfile.rails-3.1.x
87
+ - gemfiles/Gemfile.rails-3.2.x
88
+ - gemfiles/Gemfile.rails-4.0.x
89
+ - lib/render_csv.rb
90
+ - lib/render_csv/csv_renderable.rb
91
+ - lib/render_csv/version.rb
92
+ - render_csv.gemspec
93
+ - spec/lib/csv_renderable_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://github.com/beerlington/render_csv
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.0.14
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Adds a custom CSV renderer to Rails applications
119
+ test_files:
120
+ - spec/lib/csv_renderable_spec.rb
121
+ - spec/spec_helper.rb
122
+ has_rdoc: