decisive 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/decisive.gemspec +2 -0
- data/gemfiles/rails_5.1.gemfile.lock +1 -1
- data/gemfiles/rails_5.2.gemfile.lock +1 -1
- data/gemfiles/rails_6.0.gemfile.lock +1 -1
- data/lib/decisive.rb +2 -1
- data/lib/decisive/template_handler.rb +69 -10
- data/lib/decisive/version.rb +1 -1
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 718e8a3321e9c3b9395148e5f71c6f0742c23705b59b8fd0f2ea861629f44785
|
4
|
+
data.tar.gz: 19bce14fdc4f4602c438fbacdc1cc3ec05b3ea5a7157580875dc12727403b4c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d864b74616e2e8e2582482ff20ca57e99b93e06f12a742766857129f3ab640782fa1cf1cdca43a647d52796b85fff169222d5862b3438b0f7d9be119cddc0cff
|
7
|
+
data.tar.gz: c98047d4c0a9371a6efd7a3fe923c41d1c4308ef819f9017f33bc9d03e42d5c24f477619e8c1795c3d9f8b04d5be88824f1c9988c4b9c9328d71b56ceeaf29e6
|
data/decisive.gemspec
CHANGED
@@ -24,9 +24,11 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
|
26
26
|
spec.add_dependency "actionview"
|
27
|
+
spec.add_dependency "spreadsheet"
|
27
28
|
|
28
29
|
spec.add_development_dependency "bundler"
|
29
30
|
spec.add_development_dependency "appraisal"
|
30
31
|
spec.add_development_dependency "rake"
|
31
32
|
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
spec.add_development_dependency "simple-spreadsheet"
|
32
34
|
end
|
data/lib/decisive.rb
CHANGED
@@ -3,8 +3,9 @@ require "decisive/template_handler"
|
|
3
3
|
|
4
4
|
module Decisive
|
5
5
|
class Rails < ::Rails::Engine
|
6
|
-
initializer "decisive.
|
6
|
+
initializer "decisive.register_template_handler_and_xls_mime_type" do
|
7
7
|
TemplateHandler.register
|
8
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
8
9
|
end
|
9
10
|
end if defined?(Rails)
|
10
11
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require "csv"
|
2
2
|
require "action_view"
|
3
3
|
require "active_support/core_ext/string/inflections"
|
4
|
+
require "spreadsheet"
|
4
5
|
|
5
6
|
module Decisive
|
6
7
|
class TemplateHandler
|
@@ -13,22 +14,29 @@ module Decisive
|
|
13
14
|
extend Decisive::DSL
|
14
15
|
context = (#{source})
|
15
16
|
|
16
|
-
response.headers["Content-Type"] = "text/csv"
|
17
17
|
response.headers["Content-Transfer-Encoding"] = "binary"
|
18
18
|
response.headers["Content-Disposition"] = %(attachment; filename="\#{context.filename}")
|
19
19
|
|
20
|
-
if
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
if context.csv?
|
21
|
+
response.headers["Content-Type"] = "text/csv"
|
22
|
+
|
23
|
+
if controller.respond_to?(:new_controller_thread) # has AC::Live mixed in
|
24
|
+
begin
|
25
|
+
context.each do |row|
|
26
|
+
response.stream.write row.to_csv(force_quotes: true)
|
27
|
+
end
|
28
|
+
raise if Rails.env.test? # WTF WTF without this the stream isn't closed in test mode??? WTF WTF
|
29
|
+
ensure
|
30
|
+
response.stream.close
|
24
31
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
32
|
+
""
|
33
|
+
else
|
34
|
+
context.to_csv(force_quotes: true)
|
28
35
|
end
|
29
|
-
|
36
|
+
|
30
37
|
else
|
31
|
-
|
38
|
+
response.headers["Content-Type"] = "application/vnd.ms-excel"
|
39
|
+
context.to_xls
|
32
40
|
end
|
33
41
|
RUBY
|
34
42
|
end
|
@@ -56,6 +64,49 @@ module Decisive
|
|
56
64
|
RenderContext.new(records, filename, block)
|
57
65
|
end
|
58
66
|
end
|
67
|
+
|
68
|
+
def xls worksheets, filename:, &block
|
69
|
+
XLSContext.new(worksheets, filename, block)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class XLSContext < Struct.new(:worksheets, :filename, :block)
|
74
|
+
def to_xls
|
75
|
+
to_string(render(Spreadsheet::Workbook.new))
|
76
|
+
end
|
77
|
+
|
78
|
+
def csv?
|
79
|
+
false
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def render xls
|
85
|
+
worksheets.each do |name, enumerable|
|
86
|
+
sheet = xls.create_worksheet(name: name)
|
87
|
+
|
88
|
+
rows = to_array(enumerable)
|
89
|
+
|
90
|
+
rows.each.with_index do |row, index|
|
91
|
+
sheet.row(index).concat row
|
92
|
+
end
|
93
|
+
end
|
94
|
+
xls
|
95
|
+
end
|
96
|
+
|
97
|
+
def to_array records
|
98
|
+
context = RenderContext.new(records, nil, block)
|
99
|
+
context.send(:header) + context.send(:body)
|
100
|
+
end
|
101
|
+
|
102
|
+
def to_string xls
|
103
|
+
io = StringIO.new
|
104
|
+
xls.write(io)
|
105
|
+
io.rewind
|
106
|
+
string = io.read
|
107
|
+
string.force_encoding(Encoding::ASCII_8BIT)
|
108
|
+
string
|
109
|
+
end
|
59
110
|
end
|
60
111
|
|
61
112
|
class StreamContext < Struct.new(:columns, :records, :filename)
|
@@ -83,6 +134,10 @@ module Decisive
|
|
83
134
|
end
|
84
135
|
end
|
85
136
|
|
137
|
+
def csv?
|
138
|
+
true
|
139
|
+
end
|
140
|
+
|
86
141
|
private
|
87
142
|
|
88
143
|
def header
|
@@ -97,6 +152,10 @@ module Decisive
|
|
97
152
|
end.join
|
98
153
|
end
|
99
154
|
|
155
|
+
def csv?
|
156
|
+
true
|
157
|
+
end
|
158
|
+
|
100
159
|
private
|
101
160
|
|
102
161
|
def header
|
data/lib/decisive/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decisive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionview
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: spreadsheet
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +94,20 @@ dependencies:
|
|
80
94
|
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simple-spreadsheet
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description: DSL for rendering CSVs from an array of objects
|
84
112
|
email:
|
85
113
|
- micah@botandrose.com
|