useful_renderers 0.0.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 63c4b461671436a8635c0dcb32cf225d0e348ce6
4
- data.tar.gz: 8372d535dd0b4a04a083d2c7fc9cb326d436f039
2
+ SHA256:
3
+ metadata.gz: b45070a2de797541d5adc36450bb071956c65b10d1318f33d891334e01493bdb
4
+ data.tar.gz: 42c7b8e6c222f43d719caf545b4cdfa99d3dece8b205a1d8ad622dd8f3acaa79
5
5
  SHA512:
6
- metadata.gz: 2c5d3fd4049896dc2309faf428467de4486dd9e8c190f71d16bab39a5bc7f5b6483d213f05357e3a3d3dbfdb8155b0d8f3c60b9e696c9fb63c251eee104c7981
7
- data.tar.gz: 36bd0e1b55359baf50e907a6eba4f4d846c0c4d6edfad8360fa6e604e268181263014c473d4be760a8d77ee9c81a708ec7d3d015308ec3e42f31b199452ef42c
6
+ metadata.gz: 594e329714e524b8d006e1a4d5e53e1f97fae8aff7f65b17a7da753f183f3c5594f32ab744a4052e2c8c66de1b14d12cf79b8aad8cbf471a7904f7c48f3753c9
7
+ data.tar.gz: d096a66632c5a9e8b7c75dfd4b8a9fb9170be3c0a399d8b9707ca16b8918932305016fe577b682b7c3058e827f6df7994c554fe30317ef51af07d3f759a510d7
data/.gitignore CHANGED
@@ -1,7 +1,7 @@
1
1
  .DS_Store
2
2
  /.bundle/
3
3
  /.yardoc
4
- /Gemfile.lock
4
+ Gemfile.lock
5
5
  /_yardoc/
6
6
  /coverage/
7
7
  /doc/
data/README.md CHANGED
@@ -1,10 +1,9 @@
1
1
  # UsefulRenderers
2
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
- -->
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
- ### Add methods as columns
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
- ### Add methods as columns and exclude columns
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, except: [:id], add_methods: [:method1, :method2] }
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/[my-github-username]/useful_renderers/fork )
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`)
@@ -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::CSV, disposition: "attachment; filename=#{filename}.csv"
14
+ send_data data, type: Mime[:csv], disposition: "attachment; filename=#{filename}.csv"
15
15
  end
16
16
 
17
- ActionController.add_renderer :zip do |csv, options|
17
+ ActionController.add_renderer :zip do |zip, options|
18
18
  filename = options[:filename] || options[:template]
19
- csv.extend UsefulRenderers::ZipRenderable
20
- data = csv.respond_to?(:to_zip) ? csv.to_csv(options) : csv
21
- send_data data, type: Mime::Zip, disposition: "attachment; filename=#{filename}.zip"
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] # 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
- columns &= options[:only].map(&:to_s) if options[:only]
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 { |c| obj.send(c) }
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
@@ -1,3 +1,3 @@
1
1
  module UsefulRenderers
2
- VERSION = "0.0.2"
2
+ VERSION = "0.2.0"
3
3
  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\n7,O'Connor,76.8\n8,Ruby,68.2\n9,Shelby,64.0\n"
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\n10,O'Connor,3,76.8,25\n11,Ruby,3,68.2,25\n12,Shelby,5,64.0,33\n"
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\n16,O'Connor\n17,Ruby\n18,Shelby\n"
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
@@ -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', '>= 3.0'
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.2
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: 2014-12-04 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
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.0'
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: '3.0'
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
- rubyforge_project:
158
- rubygems_version: 2.2.2
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: