spreadsheet_architect 1.0.3 → 1.0.4
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 +38 -15
- data/lib/spreadsheet_architect.rb +24 -38
- 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: 64aee8164ace0ace168a8468abd18bee0fa75ab4
|
4
|
+
data.tar.gz: 340b7cb21f836d507c11a16b870b2774fa209fd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 545b1ebbd6c0cf59a1841a4e60b3a61c5de90d6fdc8a8b8fa8f370833b1e52abf0e0610e001b888b2a61743540d948b1ec68c2aedfb421121e694ca78f0082ab
|
7
|
+
data.tar.gz: 5adc741b06b52e70e6c3c73c192a36920631ffeca5c13791cf8d8eec9adf34a6097cfa1dad74f62c56d71e6afda03a9020f9125bff1260bb266073c4d6d5dd89
|
data/README.md
CHANGED
@@ -47,39 +47,57 @@ end
|
|
47
47
|
|
48
48
|
# Usage
|
49
49
|
|
50
|
-
### Method 1: Controller for
|
50
|
+
### Method 1: Controller (for Rails)
|
51
51
|
```ruby
|
52
|
+
|
52
53
|
class PostsController < ActionController::Base
|
54
|
+
respond_to :html, :xlsx, :ods, :csv
|
55
|
+
|
56
|
+
# Using respond_with
|
53
57
|
def index
|
54
58
|
@posts = Post.order(published_at: :asc)
|
55
|
-
|
59
|
+
|
60
|
+
respond_with @posts
|
61
|
+
end
|
62
|
+
|
63
|
+
# Using respond_with with custom options
|
64
|
+
def index
|
65
|
+
@posts = Post.order(published_at: :asc)
|
66
|
+
|
67
|
+
if ['xlsx','ods','csv'].include?(request.format)
|
68
|
+
respond_with @posts.to_xlsx(row_style: {bold: true}), filename: 'Posts'
|
69
|
+
else
|
70
|
+
respond_with @posts
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Using responders
|
75
|
+
def index
|
76
|
+
@posts = Post.order(published_at: :asc)
|
77
|
+
|
56
78
|
respond_to do |format|
|
57
79
|
format.html
|
58
|
-
format.xlsx { render xlsx: @posts
|
59
|
-
format.ods { render ods: @posts
|
60
|
-
format.csv
|
80
|
+
format.xlsx { render xlsx: @posts }
|
81
|
+
format.ods { render ods: @posts }
|
82
|
+
format.csv{ render csv: @posts }
|
61
83
|
end
|
62
84
|
end
|
63
|
-
end
|
64
|
-
```
|
65
85
|
|
66
|
-
|
67
|
-
```ruby
|
68
|
-
class PostsController < ActionController::Base
|
86
|
+
# Using responders with custom options
|
69
87
|
def index
|
70
|
-
|
88
|
+
@posts = Post.order(published_at: :asc)
|
71
89
|
|
72
90
|
respond_to do |format|
|
73
91
|
format.html
|
74
|
-
format.xlsx { render xlsx:
|
75
|
-
format.ods { render ods: Post.
|
76
|
-
format.csv
|
92
|
+
format.xlsx { render xlsx: @posts.to_xlsx(headers: false) }
|
93
|
+
format.ods { render ods: Post.to_odf(data: @posts) }
|
94
|
+
format.csv{ render csv: @posts.to_csv(headers: false), file_name: 'articles' }
|
77
95
|
end
|
78
96
|
end
|
79
97
|
end
|
80
98
|
```
|
81
99
|
|
82
|
-
### Method
|
100
|
+
### Method 2: Save to a file manually
|
83
101
|
```ruby
|
84
102
|
File.open('path/to/file.xlsx') do |f|
|
85
103
|
f.write{ Post.order(published_at: :asc).to_xlsx }
|
@@ -90,6 +108,11 @@ end
|
|
90
108
|
File.open('path/to/file.csv') do |f|
|
91
109
|
f.write{ Post.order(published_at: :asc).to_csv }
|
92
110
|
end
|
111
|
+
|
112
|
+
# Ex. with plain ruby class
|
113
|
+
File.open('path/to/file.xlsx') do |f|
|
114
|
+
f.write{ Post.to_xlsx(data: posts_array) }
|
115
|
+
end
|
93
116
|
```
|
94
117
|
|
95
118
|
|
@@ -10,8 +10,8 @@ module SpreadsheetArchitect
|
|
10
10
|
base.send :extend, ClassMethods
|
11
11
|
end
|
12
12
|
|
13
|
-
module
|
14
|
-
def
|
13
|
+
module Helpers
|
14
|
+
def self.str_humanize(str, capitalize = true)
|
15
15
|
str = str.sub(/\A_+/, '').gsub(/[_\.]/,' ').sub(' rescue nil','')
|
16
16
|
if capitalize
|
17
17
|
str = str.gsub(/(\A|\ )\w/){|x| x.upcase}
|
@@ -19,22 +19,22 @@ module SpreadsheetArchitect
|
|
19
19
|
str
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
if
|
24
|
-
the_column_names = (
|
25
|
-
headers = the_column_names.map{|x|
|
22
|
+
def self.get_options(options={}, klass)
|
23
|
+
if klass.ancestors.include?(ActiveRecord::Base) && !klass.respond_to?(:spreadsheet_columns) && !options[:spreadsheet_columns]
|
24
|
+
the_column_names = (klass.column_names - ["id","created_at","updated_at","deleted_at"])
|
25
|
+
headers = the_column_names.map{|x| str_humanize(x)}
|
26
26
|
columns = the_column_names.map{|x| x.to_s}
|
27
|
-
elsif options[:spreadsheet_columns] ||
|
27
|
+
elsif options[:spreadsheet_columns] || klass.respond_to?(:spreadsheet_columns)
|
28
28
|
headers = []
|
29
29
|
columns = []
|
30
30
|
|
31
|
-
array = options[:spreadsheet_columns] ||
|
31
|
+
array = options[:spreadsheet_columns] || klass.spreadsheet_columns || []
|
32
32
|
array.each do |x|
|
33
33
|
if x.is_a?(Array)
|
34
34
|
headers.push x[0].to_s
|
35
35
|
columns.push x[1].to_s
|
36
36
|
else
|
37
|
-
headers.push
|
37
|
+
headers.push str_humanize(x.to_s)
|
38
38
|
columns.push x.to_s
|
39
39
|
end
|
40
40
|
end
|
@@ -58,51 +58,38 @@ module SpreadsheetArchitect
|
|
58
58
|
row_style.merge!(options[:row_style])
|
59
59
|
end
|
60
60
|
|
61
|
-
sheet_name = options[:sheet_name] ||
|
61
|
+
sheet_name = options[:sheet_name] || klass.name
|
62
62
|
|
63
63
|
if options[:data]
|
64
64
|
data = options[:data].to_a
|
65
|
-
elsif
|
66
|
-
data = where(options[:where]).order(options[:order]).to_a
|
65
|
+
elsif klass.ancestors.include?(ActiveRecord::Base)
|
66
|
+
data = klass.where(options[:where]).order(options[:order]).to_a
|
67
67
|
else
|
68
68
|
# object must have a to_a method
|
69
|
-
data =
|
69
|
+
data = klass.to_a
|
70
70
|
end
|
71
71
|
|
72
72
|
types = (options[:types] || []).flatten
|
73
73
|
|
74
74
|
return {headers: headers, columns: columns, header_style: header_style, row_style: row_style, types: types, sheet_name: sheet_name, data: data}
|
75
75
|
end
|
76
|
+
end
|
76
77
|
|
77
|
-
|
78
|
-
row_data = []
|
79
|
-
the_columns.each do |col|
|
80
|
-
col.split('.').each_with_index do |x,i|
|
81
|
-
if i == 0
|
82
|
-
col = instance.instance_eval(x)
|
83
|
-
else
|
84
|
-
col = col.instance_eval(x)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
row_data.push col.to_s
|
88
|
-
end
|
89
|
-
return row_data
|
90
|
-
end
|
91
|
-
|
78
|
+
module ClassMethods
|
92
79
|
def to_csv(opts={})
|
93
|
-
options =
|
80
|
+
options = SpreadsheetArchitect::Helpers.get_options(opts, self)
|
94
81
|
|
95
82
|
CSV.generate do |csv|
|
96
83
|
csv << options[:headers] if options[:headers]
|
97
84
|
|
98
|
-
options[:data].each do |
|
99
|
-
csv <<
|
85
|
+
options[:data].each do |instance|
|
86
|
+
csv << options[:columns].map{|col| instance.instance_eval(col)}
|
100
87
|
end
|
101
88
|
end
|
102
89
|
end
|
103
90
|
|
104
91
|
def to_ods(opts={})
|
105
|
-
options =
|
92
|
+
options = SpreadsheetArchitect::Helpers.get_options(opts, self)
|
106
93
|
|
107
94
|
spreadsheet = ODF::Spreadsheet.new
|
108
95
|
|
@@ -139,7 +126,6 @@ module SpreadsheetArchitect
|
|
139
126
|
end
|
140
127
|
end
|
141
128
|
|
142
|
-
this_class = self
|
143
129
|
spreadsheet.table options[:sheet_name] do
|
144
130
|
if options[:headers]
|
145
131
|
row do
|
@@ -148,9 +134,9 @@ module SpreadsheetArchitect
|
|
148
134
|
end
|
149
135
|
end
|
150
136
|
end
|
151
|
-
options[:data].each do |
|
137
|
+
options[:data].each do |instance|
|
152
138
|
row do
|
153
|
-
|
139
|
+
options[:columns].map{|col| instance.instance_eval(col)}.each do |y|
|
154
140
|
cell y, style: :row_style
|
155
141
|
end
|
156
142
|
end
|
@@ -161,7 +147,7 @@ module SpreadsheetArchitect
|
|
161
147
|
end
|
162
148
|
|
163
149
|
def to_xlsx(opts={})
|
164
|
-
options =
|
150
|
+
options = SpreadsheetArchitect::Helpers.get_options(opts, self)
|
165
151
|
|
166
152
|
header_style = {}
|
167
153
|
header_style[:fg_color] = options[:header_style].delete(:color)
|
@@ -198,8 +184,8 @@ module SpreadsheetArchitect
|
|
198
184
|
sheet.add_row options[:headers], style: package.workbook.styles.add_style(header_style)
|
199
185
|
end
|
200
186
|
|
201
|
-
options[:data].each do |
|
202
|
-
sheet.add_row
|
187
|
+
options[:data].each do |instance|
|
188
|
+
sheet.add_row options[:columns].map{|col| instance.instance_eval(col)}, style: package.workbook.styles.add_style(row_style), types: options[:types]
|
203
189
|
end
|
204
190
|
end
|
205
191
|
|
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.0.4
|
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-
|
11
|
+
date: 2016-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: axlsx
|