useful_renderers 0.0.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -1
- data/README.md +9 -16
- data/lib/useful_renderers.rb +5 -5
- data/lib/useful_renderers/csv_renderable.rb +7 -7
- data/lib/useful_renderers/version.rb +1 -1
- data/spec/lib/csv_renderable_spec.rb +12 -3
- data/useful_renderers.gemspec +1 -1
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b45070a2de797541d5adc36450bb071956c65b10d1318f33d891334e01493bdb
|
4
|
+
data.tar.gz: 42c7b8e6c222f43d719caf545b4cdfa99d3dece8b205a1d8ad622dd8f3acaa79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 594e329714e524b8d006e1a4d5e53e1f97fae8aff7f65b17a7da753f183f3c5594f32ab744a4052e2c8c66de1b14d12cf79b8aad8cbf471a7904f7c48f3753c9
|
7
|
+
data.tar.gz: d096a66632c5a9e8b7c75dfd4b8a9fb9170be3c0a399d8b9707ca16b8918932305016fe577b682b7c3058e827f6df7994c554fe30317ef51af07d3f759a510d7
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# UsefulRenderers
|
2
2
|
|
3
|
-
|
4
|
-
[![
|
5
|
-
[![
|
6
|
-
|
7
|
-
-->
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/useful_renderers.svg)](http://badge.fury.io/rb/useful_renderers)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/blueberryapps/useful_renderers/badges/gpa.svg)](https://codeclimate.com/github/blueberryapps/useful_renderers)
|
5
|
+
[![Dependency Status](https://gemnasium.com/blueberryapps/useful_renderers.svg)](https://gemnasium.com/blueberryapps/useful_renderers)
|
6
|
+
|
8
7
|
Rails renderers
|
9
8
|
|
10
9
|
- CSV renderer for ActiveRecord collections
|
@@ -84,30 +83,24 @@ end
|
|
84
83
|
|
85
84
|
```ruby
|
86
85
|
respond_to do |format|
|
87
|
-
format.csv { render csv: @locations, only: [:address, :zip] }
|
86
|
+
format.csv { render csv: @locations, only: [:address, :zip, 'country.state'] }
|
88
87
|
end
|
89
88
|
```
|
90
89
|
|
91
|
-
###
|
92
|
-
|
93
|
-
```ruby
|
94
|
-
respond_to do |format|
|
95
|
-
format.csv { render csv: @locations, add_methods: [:method1, :method2] }
|
96
|
-
end
|
97
|
-
```
|
90
|
+
### Translate headers
|
98
91
|
|
99
|
-
|
92
|
+
Using `Class.human_attribute_name(column_name)`
|
100
93
|
|
101
94
|
```ruby
|
102
95
|
respond_to do |format|
|
103
|
-
format.csv { render csv: @locations,
|
96
|
+
format.csv { render csv: @locations, translate: true }
|
104
97
|
end
|
105
98
|
```
|
106
99
|
|
107
100
|
|
108
101
|
## Contributing
|
109
102
|
|
110
|
-
1. Fork it ( https://github.com/
|
103
|
+
1. Fork it ( https://github.com/blueberryapps/useful_renderers/fork )
|
111
104
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
112
105
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
113
106
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/useful_renderers.rb
CHANGED
@@ -11,14 +11,14 @@ module UsefulRenderers
|
|
11
11
|
filename = options[:filename] || options[:template]
|
12
12
|
csv.extend UsefulRenderers::CsvRenderable
|
13
13
|
data = csv.respond_to?(:to_csv) ? csv.to_csv(options) : csv
|
14
|
-
send_data data, type: Mime
|
14
|
+
send_data data, type: Mime[:csv], disposition: "attachment; filename=#{filename}.csv"
|
15
15
|
end
|
16
16
|
|
17
|
-
ActionController.add_renderer :zip do |
|
17
|
+
ActionController.add_renderer :zip do |zip, options|
|
18
18
|
filename = options[:filename] || options[:template]
|
19
|
-
|
20
|
-
data =
|
21
|
-
send_data data, type:
|
19
|
+
zip.extend UsefulRenderers::ZipRenderable
|
20
|
+
data = zip.respond_to?(:to_zip) ? zip.to_zip(options) : zip
|
21
|
+
send_data data, type: 'application/zip', disposition: "attachment; filename=#{filename}.zip"
|
22
22
|
end
|
23
23
|
|
24
24
|
end
|
@@ -5,19 +5,17 @@ module UsefulRenderers
|
|
5
5
|
|
6
6
|
# Converts an array to CSV formatted string
|
7
7
|
# Options include:
|
8
|
-
# :only => [:col1, :col2]
|
8
|
+
# :only => [:col1, :col2] # Specify which columns to include
|
9
9
|
# :except => [:col1, :col2] # Specify which columns to exclude
|
10
|
-
# :
|
10
|
+
# :translate => boolean # Translate headers
|
11
11
|
|
12
12
|
def to_csv(options = {})
|
13
13
|
klass = first.class
|
14
14
|
return '' if empty?
|
15
15
|
return join(',') unless klass.respond_to? :column_names
|
16
16
|
|
17
|
-
columns = klass.column_names
|
18
|
-
columns
|
19
|
-
columns -= options[:except].map(&:to_s) if options[:except]
|
20
|
-
columns += options[:add_methods].map(&:to_s) if options[:add_methods]
|
17
|
+
columns = options[:only] ? options[:only].map(&:to_s) : klass.column_names
|
18
|
+
columns -= options[:except].map(&:to_s) if options[:except]
|
21
19
|
|
22
20
|
headers = columns.dup
|
23
21
|
headers.map!{|col| klass.human_attribute_name col } if options[:translate]
|
@@ -30,7 +28,9 @@ module UsefulRenderers
|
|
30
28
|
|
31
29
|
CSV.generate(csv_options) do |row|
|
32
30
|
self.each do |obj|
|
33
|
-
row << columns.map
|
31
|
+
row << columns.map do |column|
|
32
|
+
column.to_s.split('.').inject(obj) { |o, m| o.try(m) }
|
33
|
+
end
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
@@ -44,12 +44,21 @@ describe UsefulRenderers::CsvRenderable do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
context 'options with :only param including methods' do
|
48
|
+
it 'returns only columns specified' do
|
49
|
+
options = { only: [:name, :human_age] }
|
50
|
+
|
51
|
+
expect(csv_renderable_array.to_csv(options))
|
52
|
+
.to eql "name,human_age\nO'Connor,25\nRuby,25\nShelby,33\n"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
47
56
|
context 'options with :exclude param' do
|
48
57
|
it 'excludes columns specified' do
|
49
58
|
options = { except: [:age] }
|
50
59
|
|
51
60
|
expect(csv_renderable_array.to_csv(options))
|
52
|
-
.to eql "id,name,weight\
|
61
|
+
.to eql "id,name,weight\n#{sebastian.id},O'Connor,76.8\n#{ruby.id},Ruby,68.2\n#{shelby.id},Shelby,64.0\n"
|
53
62
|
end
|
54
63
|
end
|
55
64
|
|
@@ -58,7 +67,7 @@ describe UsefulRenderers::CsvRenderable do
|
|
58
67
|
options = { add_methods: [:human_age] }
|
59
68
|
|
60
69
|
expect(csv_renderable_array.to_csv(options))
|
61
|
-
.to eql "id,name,age,weight,human_age\
|
70
|
+
.to eql "id,name,age,weight,human_age\n#{sebastian.id},O'Connor,3,76.8,25\n#{ruby.id},Ruby,3,68.2,25\n#{shelby.id},Shelby,5,64.0,33\n"
|
62
71
|
end
|
63
72
|
end
|
64
73
|
|
@@ -75,7 +84,7 @@ describe UsefulRenderers::CsvRenderable do
|
|
75
84
|
it 'translate header with human_attribute_name' do
|
76
85
|
options = { only: [:id, :name], translate: true }
|
77
86
|
expect(csv_renderable_array.to_csv(options))
|
78
|
-
.to eql "Id,Dog name\
|
87
|
+
.to eql "Id,Dog name\n#{sebastian.id},O'Connor\n#{ruby.id},Ruby\n#{shelby.id},Shelby\n"
|
79
88
|
end
|
80
89
|
end
|
81
90
|
end
|
data/useful_renderers.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency 'rails', '>=
|
21
|
+
spec.add_dependency 'rails', '>= 5.0'
|
22
22
|
spec.add_dependency 'rubyzip', '>= 1.0.0'
|
23
23
|
|
24
24
|
spec.add_development_dependency 'rspec-rails', '>= 2.12'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: useful_renderers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ondrej Bartas
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rubyzip
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -139,7 +139,7 @@ homepage: ''
|
|
139
139
|
licenses:
|
140
140
|
- MIT
|
141
141
|
metadata: {}
|
142
|
-
post_install_message:
|
142
|
+
post_install_message:
|
143
143
|
rdoc_options: []
|
144
144
|
require_paths:
|
145
145
|
- lib
|
@@ -154,9 +154,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
154
|
- !ruby/object:Gem::Version
|
155
155
|
version: '0'
|
156
156
|
requirements: []
|
157
|
-
|
158
|
-
|
159
|
-
signing_key:
|
157
|
+
rubygems_version: 3.0.1
|
158
|
+
signing_key:
|
160
159
|
specification_version: 4
|
161
160
|
summary: Adds a custom CSV and ZIP renderer to Rails applications
|
162
161
|
test_files:
|