useful_renderers 0.0.3 → 0.3.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 -13
- data/lib/useful_renderers/version.rb +1 -1
- 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: bf5f74912ea017f010e6bee5399c8f518681cd17d8418d74c84eef861d34b390
|
4
|
+
data.tar.gz: 06d2801f99a0e34c4d3104cbae3cb75e8ae2a26bcf7028a73b27060ba55a1436
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c02d2b98c06246bd75cf71568426cf02d7e9b7e6d664bad4175963a5fd519026b42812cd93b916f69c2e9462382ac8dc254b81672c6cf03d94e6edb4dedb04b2
|
7
|
+
data.tar.gz: 161b7daf08ff1cdc47dd99b566deed06faeec2c4e3534acac0810f18098a50cb1076a01f6c5498c43c99adb6a6aeadab1998f3f7bc7276d936d3d5657bbeedb4
|
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,25 +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
|
-
if options[:
|
19
|
-
columns = []
|
20
|
-
options[:only].each do |method|
|
21
|
-
columns << method.to_s if first.respond_to?(method)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
columns -= options[:except].map(&:to_s) if options[:except]
|
26
|
-
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]
|
27
19
|
|
28
20
|
headers = columns.dup
|
29
21
|
headers.map!{|col| klass.human_attribute_name col } if options[:translate]
|
@@ -36,7 +28,9 @@ module UsefulRenderers
|
|
36
28
|
|
37
29
|
CSV.generate(csv_options) do |row|
|
38
30
|
self.each do |obj|
|
39
|
-
row << columns.map
|
31
|
+
row << columns.map do |column|
|
32
|
+
column.to_s.split('.').inject(obj) { |o, m| o.try(m) }
|
33
|
+
end
|
40
34
|
end
|
41
35
|
end
|
42
36
|
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.3.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:
|