poormans_export 0.1.0.1 → 0.2
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 +4 -4
- data/README.md +8 -0
- data/lib/poormans_export/exporter.rb +28 -46
- data/lib/poormans_export/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a41131e0bd8a33a2a11671007e8c19523cb695411eddd596783809d15fa07486
|
4
|
+
data.tar.gz: d7cb04173536814d851f6c4742aba11305fbeb0d9d6525980964b05ddffe04dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37c2930f4fdc097863f59526e5d0806a6a5a00a3f8db446f5954055a33a8ef16fc8b9abbc50b41050c1620156a916e181ebd9ebd4697f38de80267729e1ed694
|
7
|
+
data.tar.gz: fc47a7e0d8ab60ede1503012a63d12fd1dc8625210509a2e1f84ad2664c9bff220deb5fb0e3aeb9737e2e9f08549100ef0f4550a05194726ab86ce4f722f574c
|
data/README.md
CHANGED
@@ -32,6 +32,14 @@ end
|
|
32
32
|
|
33
33
|
Poorman's Export will process the collection and convert its fields into something readable by a human, automatically processing dates, times, booleans and even relations, by using the `to_s` method of the related object.
|
34
34
|
|
35
|
+
### Custom headers
|
36
|
+
|
37
|
+
By default, Poorman's Export will extract the localized name of each of the fields of the exportation for its header by using `human_attribute_name`. You can override this behavior by adding an extra parameter in the constructor with your own custom headers:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
PoormansExport::Exporter.new(@collection, ['full_name', 'date_of_birth', 'city_name'], ['Name', 'Birthday', 'City'])
|
41
|
+
```
|
42
|
+
|
35
43
|
## Installation
|
36
44
|
Add this line to your application's Gemfile:
|
37
45
|
|
@@ -21,28 +21,14 @@ module PoormansExport
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
@headers = headers
|
24
|
+
@header = get_header
|
24
25
|
end
|
25
26
|
|
26
27
|
def csv_string(params = {})
|
27
28
|
options = { col_sep: ',' }
|
28
29
|
options.merge! params
|
29
30
|
csv_string = CSV.generate(options) do |csv|
|
30
|
-
|
31
|
-
class_name = @collection
|
32
|
-
elsif @collection.first.class != NilClass
|
33
|
-
class_name = @collection.first.class
|
34
|
-
end
|
35
|
-
|
36
|
-
header = []
|
37
|
-
@head.each do |header_field|
|
38
|
-
header <<
|
39
|
-
if class_name
|
40
|
-
class_name.human_attribute_name(header_field.gsub(/_ids?/, ''))
|
41
|
-
else
|
42
|
-
header_field
|
43
|
-
end
|
44
|
-
end
|
45
|
-
csv << header
|
31
|
+
csv << @header
|
46
32
|
|
47
33
|
@collection.each do |item|
|
48
34
|
row_array = []
|
@@ -59,7 +45,6 @@ module PoormansExport
|
|
59
45
|
template_path = params[:template]
|
60
46
|
start_on_row = params[:start_on_row] || 0
|
61
47
|
formats = params[:formats] || { 0 => { weight: :bold } }
|
62
|
-
formats[@headers.size + 1] = { weight: :bold } unless @headers.empty?
|
63
48
|
workbook = params[:workbook]
|
64
49
|
workbook ||=
|
65
50
|
if template_path.present?
|
@@ -71,35 +56,7 @@ module PoormansExport
|
|
71
56
|
sheet.name = params[:sheet_name] if params[:sheet_name]
|
72
57
|
current_row = start_on_row
|
73
58
|
|
74
|
-
|
75
|
-
class_name = @collection
|
76
|
-
elsif @collection.first.class != NilClass
|
77
|
-
class_name = @collection.first.class
|
78
|
-
end
|
79
|
-
|
80
|
-
unless @headers.empty?
|
81
|
-
if @headers[0]
|
82
|
-
sheet.row(current_row).concat [@headers[0]]
|
83
|
-
current_row += 1
|
84
|
-
end
|
85
|
-
sheet.row(current_row).concat [I18n.l(Time.zone.now, format: :compact)]
|
86
|
-
current_row += 1
|
87
|
-
if @headers[1]
|
88
|
-
sheet.row(current_row).concat [@headers[1]]
|
89
|
-
current_row += 1
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
header = []
|
94
|
-
@head.each do |header_field|
|
95
|
-
header <<
|
96
|
-
if class_name
|
97
|
-
class_name.human_attribute_name(header_field.gsub(/_ids?/, ''))
|
98
|
-
else
|
99
|
-
header_field
|
100
|
-
end
|
101
|
-
end
|
102
|
-
sheet.row(current_row).concat header
|
59
|
+
sheet.row(current_row).concat @header
|
103
60
|
current_row += 1
|
104
61
|
|
105
62
|
@collection.each do |item|
|
@@ -182,5 +139,30 @@ module PoormansExport
|
|
182
139
|
val
|
183
140
|
end
|
184
141
|
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def get_header
|
146
|
+
class_name =
|
147
|
+
if @collection.first.class.nil?
|
148
|
+
@collection
|
149
|
+
elsif @collection.first.class != NilClass
|
150
|
+
@collection.first.class
|
151
|
+
end
|
152
|
+
header = []
|
153
|
+
@head.each_with_index do |header_field, index|
|
154
|
+
header <<
|
155
|
+
if class_name
|
156
|
+
if @headers[index]
|
157
|
+
@headers[index]
|
158
|
+
else
|
159
|
+
class_name.human_attribute_name(header_field.gsub(/_ids?/, ''))
|
160
|
+
end
|
161
|
+
else
|
162
|
+
header_field
|
163
|
+
end
|
164
|
+
end
|
165
|
+
header
|
166
|
+
end
|
185
167
|
end
|
186
168
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: poormans_export
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Imobach González Sosa
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-10-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
|
-
rubygems_version: 3.
|
78
|
+
rubygems_version: 3.0.8
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: A simple but powerful exporter
|