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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5b2acbbf961f1c2c7216f2b2ed75ad9953f1826f99f36604789b313d04060f7
4
- data.tar.gz: df4789796475f6efcd7086fb2d5f3cd511accd9b6ad2da83b6fa56a1e5d6ad27
3
+ metadata.gz: 718e8a3321e9c3b9395148e5f71c6f0742c23705b59b8fd0f2ea861629f44785
4
+ data.tar.gz: 19bce14fdc4f4602c438fbacdc1cc3ec05b3ea5a7157580875dc12727403b4c1
5
5
  SHA512:
6
- metadata.gz: da5001060fb995daaef3e4fcdef4552235c5215ebef4d491dc66a144cffd47a2ceb45a95411589e8614fa624f5ec5f5c1c034124b896395cfa9472fe414ad98b
7
- data.tar.gz: 5b10db7588dd21ff51f9145c90242fe7ef38cde9634bac5725744597000b82d6fe4d18cccf9f51bb8fe518216ee1d7c8539bee680763804bbcd4277f8fb39b55
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- decisive (0.5.0)
4
+ decisive (0.5.1)
5
5
  actionview
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- decisive (0.5.0)
4
+ decisive (0.5.1)
5
5
  actionview
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- decisive (0.5.0)
4
+ decisive (0.5.1)
5
5
  actionview
6
6
 
7
7
  GEM
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.register_template_handler" do
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 controller.respond_to?(:new_controller_thread) # has AC::Live mixed in
21
- begin
22
- context.each do |row|
23
- response.stream.write row.to_csv(force_quotes: true)
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
- raise if Rails.env.test? # WTF WTF without this the stream isn't closed in test mode??? WTF WTF
26
- ensure
27
- response.stream.close
32
+ ""
33
+ else
34
+ context.to_csv(force_quotes: true)
28
35
  end
29
- ""
36
+
30
37
  else
31
- context.to_csv(force_quotes: true)
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
@@ -1,3 +1,3 @@
1
1
  module Decisive
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
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.5.1
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: 2019-10-09 00:00:00.000000000 Z
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