useful_renderers 0.1.0 → 0.2.0

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
- SHA1:
3
- metadata.gz: d4cc0e25e2da4afd30b9328ea2b968e8b8164ed6
4
- data.tar.gz: 7d06d8d91830e2c81d1bde58a1330d1d37f32f13
2
+ SHA256:
3
+ metadata.gz: b45070a2de797541d5adc36450bb071956c65b10d1318f33d891334e01493bdb
4
+ data.tar.gz: 42c7b8e6c222f43d719caf545b4cdfa99d3dece8b205a1d8ad622dd8f3acaa79
5
5
  SHA512:
6
- metadata.gz: fbff36bec0c5856e1566ed9d154f36233ef0ab97201dbcf69034f6b42713088b8a92ee9368076fd7decc90aeabdf9a0546efe1736b3c968ab0f7cebae563b84d
7
- data.tar.gz: 9a6498cfa4376f5ece5f6bb6f6af5edc4bdb63834986608ff5468cdbf5e6f68f37297c3040367d7304a5db2f1b50b9601b2e8324acf7fc0af5b5832894d073ae
6
+ metadata.gz: 594e329714e524b8d006e1a4d5e53e1f97fae8aff7f65b17a7da753f183f3c5594f32ab744a4052e2c8c66de1b14d12cf79b8aad8cbf471a7904f7c48f3753c9
7
+ data.tar.gz: d096a66632c5a9e8b7c75dfd4b8a9fb9170be3c0a399d8b9707ca16b8918932305016fe577b682b7c3058e827f6df7994c554fe30317ef51af07d3f759a510d7
data/README.md CHANGED
@@ -83,23 +83,17 @@ end
83
83
 
84
84
  ```ruby
85
85
  respond_to do |format|
86
- format.csv { render csv: @locations, only: [:address, :zip] }
86
+ format.csv { render csv: @locations, only: [:address, :zip, 'country.state'] }
87
87
  end
88
88
  ```
89
89
 
90
- ### Add methods as columns
90
+ ### Translate headers
91
91
 
92
- ```ruby
93
- respond_to do |format|
94
- format.csv { render csv: @locations, add_methods: [:method1, :method2] }
95
- end
96
- ```
97
-
98
- ### Add methods as columns and exclude columns
92
+ Using `Class.human_attribute_name(column_name)`
99
93
 
100
94
  ```ruby
101
95
  respond_to do |format|
102
- format.csv { render csv: @locations, except: [:id], add_methods: [:method1, :method2] }
96
+ format.csv { render csv: @locations, translate: true }
103
97
  end
104
98
  ```
105
99
 
@@ -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] # Specify which columns to include
8
+ # :only => [:col1, :col2] # Specify which columns to include
9
9
  # :except => [:col1, :col2] # Specify which columns to exclude
10
- # :add_methods => [:method1, :method2] # Include addtional methods that aren't columns
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[:only]
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 { |c| obj.send(c) }
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
@@ -1,3 +1,3 @@
1
1
  module UsefulRenderers
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -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
- rubyforge_project:
158
- rubygems_version: 2.6.11
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: