rails_csv_renderer 0.1.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 +22 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +86 -0
- data/Rakefile +6 -0
- data/lib/rails_csv_renderer/renderable.rb +62 -0
- data/lib/rails_csv_renderer/renderer.rb +14 -0
- data/lib/rails_csv_renderer/version.rb +3 -0
- data/lib/rails_csv_renderer.rb +3 -0
- data/rails_csv_renderer.gemspec +25 -0
- data/spec/lib/renderable_spec.rb +88 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/cat.rb +4 -0
- data/spec/support/create_db.rb +15 -0
- data/spec/support/locales/ru.yml +14 -0
- data/spec/support/person.rb +18 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 660d1de18d4f98f836f8ece40ad26cfa0a39a075
|
4
|
+
data.tar.gz: 71d8cf91962e28da4c43efee012d2720db554217
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 874f2818485fe8e655fb0c5d4b161407b7e837777e47431272271a698b9d89d2c43292908b69b7d92dfa9699fcbe8d7f52b9124647344fca52433c4903e76cab
|
7
|
+
data.tar.gz: 032513b1c958d5d2effcec6732fc1ea66fc1f0836a5fcae397673531a8bc934095e39caf37d994ae63f733aa9b485faab92396c1ae5c8e56374a30c21eecf68a
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Dennis Sheleh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# rails_csv_renderer
|
2
|
+
|
3
|
+
Custom CSV renderer for ActiveRecord collections
|
4
|
+
|
5
|
+
## Rails & Ruby Versions Supported
|
6
|
+
|
7
|
+
*Rails:* 4.x
|
8
|
+
|
9
|
+
*Ruby:* 2.x
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'rails_csv_renderer'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install rails_csv_renderer
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
The rails_csv_renderer allows you to render any collection as CSV data. Result CSV includes all columns of model with localized column's names
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class ReportsController < ApplicationController
|
31
|
+
def index
|
32
|
+
@reports = Report.all
|
33
|
+
|
34
|
+
respond_to do |format|
|
35
|
+
format.csv { render csv: @reports }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Will render a CSV file similar to:
|
42
|
+
|
43
|
+
<table>
|
44
|
+
<tr>
|
45
|
+
<th>Title</th><th>Content</th><th>Created at</th><th>Updated at</th>
|
46
|
+
</tr>
|
47
|
+
<tr>
|
48
|
+
<td>House report</td><td>Report about house devices</td><td>2014-07-14 01:00:42 UTC</td><td>2014-07-14 01:00:42 UTC</td>
|
49
|
+
</tr>
|
50
|
+
<tr>
|
51
|
+
<td>Work report</td><td>Report about work devices</td><td>2014-07-14 12:42:42 UTC</td><td>2014-07-14 12:42:42 UTC</td>
|
52
|
+
</tr>
|
53
|
+
</table>
|
54
|
+
|
55
|
+
### Localize column's names
|
56
|
+
|
57
|
+
If you have translations for model's attributes under scope [:activerecord, :attributes, \*model_name\*] names of columns will be
|
58
|
+
automatically translated.
|
59
|
+
|
60
|
+
### Customize CSV
|
61
|
+
|
62
|
+
To customize your CSV file you should add next methods to model **csv_header**(class method), **csv_row**.
|
63
|
+
|
64
|
+
**csv_header** should return array with column's titles.
|
65
|
+
|
66
|
+
**csv_row** should return array with values for columns.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class Cat < ActiveRecord::Base
|
70
|
+
def self.csv_header
|
71
|
+
["ID", "Cat's name"]
|
72
|
+
end
|
73
|
+
|
74
|
+
def csv_row
|
75
|
+
[id, name]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
1. Fork it ( https://github.com/[my-github-username]/rails_csv_renderer/fork )
|
83
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
84
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
85
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
86
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module RailsCsvRenderer
|
5
|
+
module Renderable
|
6
|
+
# Converts an collection of active records to CSV formatted string
|
7
|
+
# Options is configuration set for generated CSV file
|
8
|
+
|
9
|
+
def to_custom_csv(options = {})
|
10
|
+
csv_options = default_csv_options.merge(options)
|
11
|
+
|
12
|
+
if is_active_record?
|
13
|
+
if !(model.respond_to?(:csv_header) || model.respond_to?(:csv_row)) || model.class_variable_defined?(:@@dynamic_generated_csv_methods)
|
14
|
+
define_csv_methods(options)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
CSV.generate(csv_options) do |csv|
|
19
|
+
if is_active_record?
|
20
|
+
csv << model.csv_header
|
21
|
+
self.each do |obj|
|
22
|
+
csv << obj.csv_row
|
23
|
+
end
|
24
|
+
else
|
25
|
+
csv << self if respond_to?(:to_csv)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def define_csv_methods(options)
|
33
|
+
columns = model.column_names
|
34
|
+
|
35
|
+
model.class_variable_set(:@@dynamic_generated_csv_methods, true)
|
36
|
+
model.class_eval(%Q{
|
37
|
+
class << self
|
38
|
+
def csv_header
|
39
|
+
["#{ columns.map { |column_name| model.human_attribute_name(column_name) }.join('", "') }"]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
})
|
43
|
+
model.class_eval(%Q{
|
44
|
+
def csv_row
|
45
|
+
[#{ columns.join(', ') }]
|
46
|
+
end
|
47
|
+
})
|
48
|
+
end
|
49
|
+
|
50
|
+
def is_active_record?
|
51
|
+
is_a?(ActiveRecord::Relation) || (present? && first.is_a?(ActiveRecord::Base))
|
52
|
+
end
|
53
|
+
|
54
|
+
def model
|
55
|
+
@model ||= is_a?(ActiveRecord::Relation) ? klass : first.class
|
56
|
+
end
|
57
|
+
|
58
|
+
def default_csv_options
|
59
|
+
{ encoding: 'utf-8' }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails_csv_renderer/renderable'
|
2
|
+
require 'action_controller/metal/renderers'
|
3
|
+
|
4
|
+
module RailsCsvRenderer
|
5
|
+
class Renderer
|
6
|
+
def initialize!
|
7
|
+
ActionController::Renderers.add :csv do |obj, options|
|
8
|
+
filename = options[:filename] || "#{ Rails.application.class.parent_name }-report-#{ Time.current }.csv"
|
9
|
+
csv.extend RailsCsvRenderer::Renderable
|
10
|
+
data = csv.to_custom_csv(options[:csv_options])
|
11
|
+
send_data data, type: Mime::CSV, disposition: "attachment; filename=#{filename}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_csv_renderer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_csv_renderer"
|
8
|
+
spec.version = RailsCsvRenderer::VERSION
|
9
|
+
spec.authors = ["Dennis Sheleh"]
|
10
|
+
spec.email = ["den.sheleh@gmail.com"]
|
11
|
+
spec.summary = %q{Add custom csv renderer for Rails application}
|
12
|
+
spec.description = %q{Add custom csv renderer for Rails application}
|
13
|
+
spec.homepage = "http://github.com/den-sheleh/rails_csv_renderer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'rails', '>= 4.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'sqlite3'
|
25
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require './spec/spec_helper'
|
3
|
+
|
4
|
+
describe RailsCsvRenderer::Renderable do
|
5
|
+
before(:all) do
|
6
|
+
Person.create(first_name: 'John', last_name: 'Smith')
|
7
|
+
Person.create(first_name: 'Adam', last_name: 'Kowalczyk')
|
8
|
+
Person.create(first_name: 'Dennis', last_name: 'Menace')
|
9
|
+
Cat.create(name: 'Kitty')
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:renderable_collection) { collection.extend RailsCsvRenderer::Renderable }
|
13
|
+
|
14
|
+
context 'object is a ' do
|
15
|
+
|
16
|
+
context 'nil' do
|
17
|
+
let(:collection) { nil }
|
18
|
+
|
19
|
+
it 'returns an empty string' do
|
20
|
+
expect(renderable_collection.to_custom_csv).to eql("")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'empty array' do
|
25
|
+
let(:collection) { Array.new }
|
26
|
+
|
27
|
+
it 'returns an empty string' do
|
28
|
+
expect(renderable_collection.to_custom_csv).to eql("\n")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'object is not an instance of AR class' do
|
33
|
+
let(:collection) { [1, 2, 3] }
|
34
|
+
|
35
|
+
it 'includes a csv of the collection' do
|
36
|
+
expect(renderable_collection.to_custom_csv).to eql("1,2,3\n")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'colection of Active Records' do
|
41
|
+
context 'empty ActiveRelation' do
|
42
|
+
let(:collection) { Person.where(first_name: 'qwerty') }
|
43
|
+
|
44
|
+
it 'includes header' do
|
45
|
+
expect(renderable_collection.to_custom_csv).to eql "Id,Full name\n"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with elements' do
|
50
|
+
let(:collection) { Person.all }
|
51
|
+
|
52
|
+
context 'with localized name of atributes' do
|
53
|
+
before { I18n.locale = :ru }
|
54
|
+
after { I18n.locale = :en }
|
55
|
+
|
56
|
+
it 'includes localized column names in header' do
|
57
|
+
expect(renderable_collection.to_custom_csv).to eql "ID,Полное имя\n1,John Smith\n2,Adam Kowalczyk\n3,Dennis Menace\n"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with csv options' do
|
62
|
+
it 'includes columns and methods with configured separators' do
|
63
|
+
options = { col_sep: "\t", row_sep: "\r\n" }
|
64
|
+
expect(renderable_collection.to_custom_csv(options)).to eql "Id\tFull name\r\n1\tJohn Smith\r\n2\tAdam Kowalczyk\r\n3\tDennis Menace\r\n"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'with undefined configuration methods' do
|
69
|
+
let(:owner) { Person.last }
|
70
|
+
let(:collection) { Cat.all }
|
71
|
+
|
72
|
+
it 'includes csv with all model columns' do
|
73
|
+
owner.cats << Cat.first
|
74
|
+
expect(renderable_collection.to_custom_csv).to eql "Id,Name,Person\n1,Kitty,#{ owner.id }\n"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'object is array of ActiveRecords' do
|
80
|
+
let(:collection) { [Person.first] }
|
81
|
+
|
82
|
+
it 'includes a csv of the collection' do
|
83
|
+
expect(renderable_collection.to_custom_csv).to eql "Id,Full name\n1,John Smith\n"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'i18n'
|
5
|
+
require 'rspec'
|
6
|
+
require 'rails_csv_renderer/renderable'
|
7
|
+
|
8
|
+
if defined? I18n
|
9
|
+
I18n.enforce_available_locales = false
|
10
|
+
I18n.load_path << File.expand_path('../support/locales/ru.yml', __FILE__)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Requires supporting files with custom matchers and macros, etc,
|
14
|
+
# in ./support/ and its subdirectories.
|
15
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
data/spec/support/cat.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
4
|
+
|
5
|
+
ActiveRecord::Schema.define(version: 1) do
|
6
|
+
create_table :people, force: true do |t|
|
7
|
+
t.string :first_name
|
8
|
+
t.string :last_name
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :cats, force: true do |t|
|
12
|
+
t.string :name
|
13
|
+
t.integer :person_id
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Person < ActiveRecord::Base
|
2
|
+
validates_presence_of :first_name, :last_name
|
3
|
+
has_many :cats
|
4
|
+
|
5
|
+
def full_name
|
6
|
+
"#{ first_name} #{ last_name }"
|
7
|
+
end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def csv_header
|
11
|
+
[human_attribute_name(:id), human_attribute_name(:full_name)]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def csv_row
|
16
|
+
[id, full_name]
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_csv_renderer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dennis Sheleh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-14 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: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Add custom csv renderer for Rails application
|
56
|
+
email:
|
57
|
+
- den.sheleh@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/rails_csv_renderer.rb
|
69
|
+
- lib/rails_csv_renderer/renderable.rb
|
70
|
+
- lib/rails_csv_renderer/renderer.rb
|
71
|
+
- lib/rails_csv_renderer/version.rb
|
72
|
+
- rails_csv_renderer.gemspec
|
73
|
+
- spec/lib/renderable_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/support/cat.rb
|
76
|
+
- spec/support/create_db.rb
|
77
|
+
- spec/support/locales/ru.yml
|
78
|
+
- spec/support/person.rb
|
79
|
+
homepage: http://github.com/den-sheleh/rails_csv_renderer
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.2.2
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Add custom csv renderer for Rails application
|
103
|
+
test_files:
|
104
|
+
- spec/lib/renderable_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/support/cat.rb
|
107
|
+
- spec/support/create_db.rb
|
108
|
+
- spec/support/locales/ru.yml
|
109
|
+
- spec/support/person.rb
|