spreadsheet_architect 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -1
- data/README.md +18 -6
- data/lib/spreadsheet_architect.rb +33 -27
- data/lib/spreadsheet_architect/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aecd3d727c30feaf79f0838b51fbab5d2a2d4179
|
4
|
+
data.tar.gz: 120005b9f5b0b987d7d66dada11f306003bab336
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae91411764f7d32eebbc13b99b6b37bbe560a4221dbbb7a489a4db37e9c7c0c0d868ef3054ef1f5dbefe23485b2f448e5ae9c75ec49dba6a4025e7a651bd8967
|
7
|
+
data.tar.gz: 68058e1003fe7ab9c4ee244dd870886b1b89afc9719df958fea6c130d89c1f63d384be97bc672e5e1f1db0a8e04b094d321b4a7bef789924e74cb8a514196516
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,19 @@
|
|
1
1
|
CHANGELOG
|
2
2
|
---------
|
3
3
|
|
4
|
+
- **March.3.2016**: 1.1.0 - Breaking Changes
|
5
|
+
- Move spreadsheet_columns method from the class to the instance
|
6
|
+
- Fix Bug: remove default underline on cells
|
7
|
+
- **March.1.2016**: 1.0.4
|
8
|
+
- Extract helper methods to seperate module
|
9
|
+
- Improve readme
|
10
|
+
- **March.1.2016**: 1.0.3
|
11
|
+
- Fix/Improve renderers
|
12
|
+
- Fix header default background color
|
13
|
+
- Fix default columns
|
14
|
+
- **February.26.2016**: 1.0.2
|
15
|
+
- Enhance Style options
|
4
16
|
- **February.26.2016**: 1.0.1
|
5
|
-
- Fix
|
17
|
+
- Fix bug in renderers
|
6
18
|
- **February.26.2016**: 1.0.0
|
7
19
|
- Gem Initial Release
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# Spreadsheet Architect
|
2
|
+
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=VKY8YAWAS5XRQ&lc=CA&item_name=Weston%20Ganger&item_number=spreadsheet_architect¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHostedGuest" target="_blank" title="Donate"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="Donate"/></a>
|
2
3
|
|
3
4
|
Spreadsheet Architect lets you turn any activerecord relation or plain ruby class object into a XLSX, ODS, or CSV spreadsheets. Generates columns from model activerecord column_names or from an array of ruby methods.
|
4
5
|
|
@@ -15,6 +16,9 @@ Post.order(name: :asc).where(published: true).to_ods
|
|
15
16
|
Post.order(name: :asc).where(published: true).to_csv
|
16
17
|
```
|
17
18
|
|
19
|
+
## Note: Breaking Changes in 1.1.0
|
20
|
+
The `spreadseheet_columns` method has been moved from the class to the instance. So now you can use string interpolation in your values. Please re-read the Model section below to see the changes. The side effect of this is if you are using the spreadsheet_columns option directly on the .to_* methods.
|
21
|
+
|
18
22
|
|
19
23
|
# Install
|
20
24
|
```ruby
|
@@ -30,17 +34,25 @@ class Post < ActiveRecord::Base #activerecord not required
|
|
30
34
|
include SpreadsheetArchitect
|
31
35
|
|
32
36
|
belongs_to :author
|
37
|
+
belongs_to :category
|
38
|
+
has_many :tags
|
33
39
|
|
34
40
|
#optional for activerecord classes, defaults to the models column_names
|
35
|
-
def
|
41
|
+
def spreadsheet_columns
|
36
42
|
#[[Label, Method/Statement to Call on each Instance]....]
|
37
|
-
[
|
43
|
+
[
|
44
|
+
['Title', :title],
|
45
|
+
['Content', content],
|
46
|
+
['Author', (author.name rescue nil)],
|
47
|
+
['Published?', (published ? 'Yes' : 'No')],
|
48
|
+
['Category/Tags', "#{category.name} - #{tags.collect(&:name).join(', ')}"]
|
49
|
+
]
|
38
50
|
|
39
51
|
# OR just humanize the method to use as the label ex. "Title", "Content", "Author Name", "Published"
|
40
|
-
[:title,
|
52
|
+
[:title, content, (author.name rescue nil), :published]
|
41
53
|
|
42
54
|
# OR a Combination of Both
|
43
|
-
[:title, :content, ['Author',
|
55
|
+
[:title, :content, ['Author',(author.name rescue nil)], :published, ]
|
44
56
|
end
|
45
57
|
end
|
46
58
|
```
|
@@ -119,11 +131,11 @@ end
|
|
119
131
|
# Method Options
|
120
132
|
|
121
133
|
### to_xlsx, to_ods, to_csv
|
122
|
-
**data** - *Array* - Mainly for Plain Ruby objects pass in an array of instances. Optional for ActiveRecord relations, you can just chain the method to the end of your relation.
|
134
|
+
**data** - *Array* - Mainly for Plain Ruby objects pass in an array of instances. Optional for ActiveRecord relations, you can just chain the method to the end of your relation. If Plain Ruby object it defaults to the instances `to_a` method.
|
123
135
|
|
124
136
|
**headers** - *Boolean* - Default: true - Pass in false if you do not want a header row.
|
125
137
|
|
126
|
-
**spreadsheet_columns** - *Array* -
|
138
|
+
**spreadsheet_columns** - *Array* - Use this to override the models spreadsheet_columns/column_names method for one time. Must use symbols that correspond to instance methods of the object. Ex: `[:name, :title, :address]` or `[['Name',:name],['Post Title', :title],['Address', :address]]`
|
127
139
|
|
128
140
|
### to_xlsx
|
129
141
|
**sheet_name** - *String*
|
@@ -20,27 +20,38 @@ module SpreadsheetArchitect
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.get_options(options={}, klass)
|
23
|
-
|
23
|
+
has_custom_columns = klass.instance_methods.include?(:spreadsheet_columns)
|
24
|
+
|
25
|
+
if !options[:data] && klass.ancestors.include?(ActiveRecord::Base)
|
26
|
+
options[:data] = klass.where(options[:where]).order(options[:order]).to_a
|
27
|
+
else
|
28
|
+
raise 'No data option was defined. This is required for plain ruby objects.'
|
29
|
+
end
|
30
|
+
|
31
|
+
if !has_custom_columns && klass.ancestors.include?(ActiveRecord::Base)
|
24
32
|
the_column_names = (klass.column_names - ["id","created_at","updated_at","deleted_at"])
|
25
33
|
headers = the_column_names.map{|x| str_humanize(x)}
|
26
|
-
columns = the_column_names.map{|x| x.
|
27
|
-
elsif
|
34
|
+
columns = the_column_names.map{|x| x.to_sym}
|
35
|
+
elsif has_custom_columns
|
28
36
|
headers = []
|
29
37
|
columns = []
|
30
|
-
|
31
|
-
array = options[:spreadsheet_columns] || klass.spreadsheet_columns || []
|
38
|
+
array = options[:spreadsheet_columns] || options[:data].first.spreadsheet_columns
|
32
39
|
array.each do |x|
|
33
40
|
if x.is_a?(Array)
|
34
41
|
headers.push x[0].to_s
|
35
|
-
columns.push x[1]
|
42
|
+
columns.push x[1]
|
36
43
|
else
|
37
44
|
headers.push str_humanize(x.to_s)
|
38
|
-
columns.push x
|
45
|
+
columns.push x
|
39
46
|
end
|
40
47
|
end
|
41
48
|
else
|
42
|
-
|
43
|
-
|
49
|
+
raise 'No instance method `spreadsheet_columns` found on this plain ruby object'
|
50
|
+
end
|
51
|
+
|
52
|
+
data = []
|
53
|
+
options[:data].each do |instance|
|
54
|
+
data.push columns.map{|col| col.is_a?(String) ? col : instance.instance_eval(col.to_s)}
|
44
55
|
end
|
45
56
|
|
46
57
|
headers = (options[:headers] == false ? false : headers)
|
@@ -59,19 +70,10 @@ module SpreadsheetArchitect
|
|
59
70
|
end
|
60
71
|
|
61
72
|
sheet_name = options[:sheet_name] || klass.name
|
62
|
-
|
63
|
-
if options[:data]
|
64
|
-
data = options[:data].to_a
|
65
|
-
elsif klass.ancestors.include?(ActiveRecord::Base)
|
66
|
-
data = klass.where(options[:where]).order(options[:order]).to_a
|
67
|
-
else
|
68
|
-
# object must have a to_a method
|
69
|
-
data = klass.to_a
|
70
|
-
end
|
71
73
|
|
72
74
|
types = (options[:types] || []).flatten
|
73
75
|
|
74
|
-
return {headers: headers,
|
76
|
+
return {headers: headers, header_style: header_style, row_style: row_style, types: types, sheet_name: sheet_name, data: data}
|
75
77
|
end
|
76
78
|
end
|
77
79
|
|
@@ -82,8 +84,8 @@ module SpreadsheetArchitect
|
|
82
84
|
CSV.generate do |csv|
|
83
85
|
csv << options[:headers] if options[:headers]
|
84
86
|
|
85
|
-
options[:data].each do |
|
86
|
-
csv <<
|
87
|
+
options[:data].each do |row_data|
|
88
|
+
csv << row_data
|
87
89
|
end
|
88
90
|
end
|
89
91
|
end
|
@@ -134,9 +136,9 @@ module SpreadsheetArchitect
|
|
134
136
|
end
|
135
137
|
end
|
136
138
|
end
|
137
|
-
options[:data].each do |
|
139
|
+
options[:data].each do |row_data|
|
138
140
|
row do
|
139
|
-
|
141
|
+
row_data.each do |y|
|
140
142
|
cell y, style: :row_style
|
141
143
|
end
|
142
144
|
end
|
@@ -159,7 +161,9 @@ module SpreadsheetArchitect
|
|
159
161
|
header_style[:b] = options[:header_style].delete(:bold)
|
160
162
|
header_style[:sz] = options[:header_style].delete(:font_size)
|
161
163
|
header_style[:i] = options[:header_style].delete(:italic)
|
162
|
-
|
164
|
+
if options[:header_style][:underline]
|
165
|
+
header_style[:u] = options[:header_style].delete(:underline)
|
166
|
+
end
|
163
167
|
header_style.delete_if{|x| x.nil?}
|
164
168
|
|
165
169
|
row_style = {}
|
@@ -172,7 +176,9 @@ module SpreadsheetArchitect
|
|
172
176
|
row_style[:b] = options[:row_style].delete(:bold)
|
173
177
|
row_style[:sz] = options[:row_style].delete(:font_size)
|
174
178
|
row_style[:i] = options[:row_style].delete(:italic)
|
175
|
-
|
179
|
+
if options[:row_style][:underline]
|
180
|
+
row_style[:u] = options[:row_style].delete(:underline)
|
181
|
+
end
|
176
182
|
row_style.delete_if{|x| x.nil?}
|
177
183
|
|
178
184
|
package = Axlsx::Package.new
|
@@ -184,8 +190,8 @@ module SpreadsheetArchitect
|
|
184
190
|
sheet.add_row options[:headers], style: package.workbook.styles.add_style(header_style)
|
185
191
|
end
|
186
192
|
|
187
|
-
options[:data].each do |
|
188
|
-
sheet.add_row
|
193
|
+
options[:data].each do |row_data|
|
194
|
+
sheet.add_row row_data, style: package.workbook.styles.add_style(row_style), types: options[:types]
|
189
195
|
end
|
190
196
|
end
|
191
197
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spreadsheet_architect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weston Ganger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: axlsx
|