cloudxls-rails 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ # CloudXLSRails::CSVStream
2
+ module CloudXLSRails
3
+ class CSVStream
4
+ def initialize(controller, stream)
5
+ @controller = controller
6
+ @stream = stream
7
+ end
8
+
9
+ def stream!(filename)
10
+ headers = @controller.headers
11
+ headers['Last-Modified'] = Time.now.to_s
12
+ headers["Content-Type"] = "text/csv"
13
+ headers["Content-disposition"] = "attachment; filename=\"#{filename}\""
14
+ # nginx doc: Setting this to "no" will allow unbuffered responses suitable
15
+ # for Comet and HTTP streaming applications
16
+ headers['X-Accel-Buffering'] = 'no'
17
+ headers["Cache-Control"] ||= "no-cache"
18
+ headers.delete("Content-Length")
19
+
20
+ @controller.response.status = 200
21
+ # setting the body to an enumerator, rails will iterate this enumerator
22
+ @controller.response_body = @stream
23
+ end
24
+
25
+ def self.stream!(controller, scope, options)
26
+ filename = options.fetch(:doc, {}).fetch(:filename, "data.csv")
27
+ enum = CloudXLS::CSVWriter.enumerator(scope, options)
28
+ new(controller, enum).stream!(filename)
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ ActionController::Renderers.add :csv do |scope, options|
35
+ filename = options.fetch(:filename, "data-#{DateTime.now.to_s}.csv")
36
+ columns = options[:columns]
37
+
38
+ if options[:stream] == true
39
+ CloudXLSRails::CSVStream.stream!(self, scope, options)
40
+ else # no stream:
41
+ data = CloudXLS::CSVWriter.text(scope, {:columns => columns})
42
+
43
+ send_data data,
44
+ type: Mime::CSV,
45
+ disposition: "attachment; filename=#{filename}.csv"
46
+ end
47
+ end
48
+
49
+ class ActionController::Responder
50
+ def to_csv
51
+ if options[:stream] == true
52
+ CloudXLSRails::CSVStream.stream!(controller, resources.last, options)
53
+ else
54
+ controller.render({:csv => resources.last, :stream => false }.merge(options))
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,35 @@
1
+ unless defined? Mime::XLS
2
+ Mime::Type.register "application/vnd.ms-excel", :xls
3
+ end
4
+
5
+
6
+ ActionController::Renderers.add :xls do |scope, options|
7
+ columns = options.fetch(:columns, nil)
8
+
9
+ xdata = options[:data] || {}
10
+ unless (xdata.has_key?(:text) ||
11
+ xdata.has_key?(:url ) ||
12
+ xdata.has_key?(:file) )
13
+
14
+ xdata[:text] = CloudXLS::CSVWriter.text(scope, {:columns => columns})
15
+ end
16
+
17
+ xopts = {:data => xdata}
18
+ xopts[:sheet] = options[:sheet] if options[:sheet]
19
+ xopts[:doc] = options[:doc] if options[:doc]
20
+
21
+ response = CloudXLS.xpipe(xopts)
22
+ redirect_to response.url
23
+ end
24
+
25
+
26
+ # For respond_to default
27
+ class ActionController::Responder
28
+ def to_xls
29
+ if options[:stream] == true
30
+ options[:data] ||= {}
31
+ options[:data][:url] ||= controller.request.url.gsub(/xls\Z/, "csv")
32
+ end
33
+ controller.render({:xls => resources.last }.merge(options))
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ unless defined? Mime::XLSX
2
+ Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
3
+ end
4
+
5
+ ActionController::Renderers.add :xlsx do |scope, options|
6
+ columns = options.fetch(:columns, nil)
7
+
8
+ xdata = options[:data] || {}
9
+ unless (xdata.has_key?(:text) ||
10
+ xdata.has_key?(:url ) ||
11
+ xdata.has_key?(:file) )
12
+
13
+ xdata[:text] = CloudXLS::CSVWriter.text(scope, {:columns => columns})
14
+ end
15
+
16
+ xopts = {:data => xdata}
17
+ xopts[:sheet] = options[:sheet] if options[:sheet]
18
+ xopts[:doc] = options[:doc] || {}
19
+ xopts[:doc][:format] = 'xlsx'
20
+
21
+ response = CloudXLS.xpipe(xopts)
22
+ redirect_to response.url
23
+ end
24
+
25
+ # For respond_to default
26
+ class ActionController::Responder
27
+ def to_xlsx
28
+ if options[:stream] == true
29
+ options[:data] ||= {}
30
+ options[:data][:url] ||= controller.request.url.gsub(/xlsx\Z/, "csv")
31
+ end
32
+ controller.render({:xlsx => resources.last }.merge(options))
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module CloudXLSRails
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
@@ -1,6 +1,10 @@
1
1
  require 'cloudxls' # cloudxls gem
2
+
3
+ require 'action_controller'
2
4
  require 'cloudxls-rails/version'
3
- require 'cloudxls-rails/action_controller'
5
+ require 'cloudxls-rails/handlers/csv'
6
+ require 'cloudxls-rails/handlers/xls'
7
+ require 'cloudxls-rails/handlers/xlsx'
4
8
 
5
9
  module CloudXLSRails
6
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudxls-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -120,7 +120,9 @@ files:
120
120
  - Rakefile
121
121
  - cloudxls-rails.gemspec
122
122
  - lib/cloudxls-rails.rb
123
- - lib/cloudxls-rails/action_controller.rb
123
+ - lib/cloudxls-rails/handlers/csv.rb
124
+ - lib/cloudxls-rails/handlers/xls.rb
125
+ - lib/cloudxls-rails/handlers/xlsx.rb
124
126
  - lib/cloudxls-rails/version.rb
125
127
  - spec/ci.sh
126
128
  - spec/csv_writer_spec.rb
@@ -160,8 +162,6 @@ files:
160
162
  - spec/test_app/config/locales/en.yml
161
163
  - spec/test_app/config/routes.rb
162
164
  - spec/test_app/db/migrate/20120717192452_create_posts.rb
163
- - spec/test_app/db/schema.rb
164
- - spec/test_app/db/test.sqlite3
165
165
  - spec/test_app/lib/assets/.gitkeep
166
166
  - spec/test_app/log/.gitkeep
167
167
  - spec/test_app/public/404.html
@@ -233,8 +233,6 @@ test_files:
233
233
  - spec/test_app/config/locales/en.yml
234
234
  - spec/test_app/config/routes.rb
235
235
  - spec/test_app/db/migrate/20120717192452_create_posts.rb
236
- - spec/test_app/db/schema.rb
237
- - spec/test_app/db/test.sqlite3
238
236
  - spec/test_app/lib/assets/.gitkeep
239
237
  - spec/test_app/log/.gitkeep
240
238
  - spec/test_app/public/404.html
@@ -1,95 +0,0 @@
1
- require 'csv'
2
- require 'action_controller'
3
-
4
- unless defined? Mime::XLS
5
- Mime::Type.register "application/vnd.ms-excel", :xls
6
- end
7
-
8
- unless defined? Mime::XLSX
9
- Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
10
- end
11
-
12
- ActionController::Renderers.add :csv do |scope, options|
13
- filename = options.fetch(:filename, 'data.csv')
14
- columns = options[:columns]
15
-
16
- if options.fetch(:stream, false)
17
- # streaming response
18
- headers["Content-Type"] = "text/csv"
19
- headers["Content-disposition"] = "attachment; filename=\"#{filename}\""
20
- headers["Cache-Control"] ||= "no-cache"
21
- headers.delete("Content-Length")
22
- response.status = 200
23
-
24
- stream = CloudXLS::CSVWriter.enumerator(scope, {:columns => columns})
25
- self.response_body = stream
26
- else # no stream:
27
- data = CloudXLS::CSVWriter.text(scope, {:columns => columns})
28
-
29
- send_data data,
30
- type: Mime::CSV,
31
- disposition: "attachment; filename=#{filename}.csv"
32
- end
33
- end
34
-
35
- ActionController::Renderers.add :xls do |scope, options|
36
- columns = options.fetch(:columns, nil)
37
-
38
- xdata = options[:data] || {}
39
- unless (xdata.has_key?(:text) ||
40
- xdata.has_key?(:url ) ||
41
- xdata.has_key?(:file) )
42
-
43
- xdata[:text] = CloudXLS::CSVWriter.text(scope, {:columns => columns})
44
- end
45
-
46
- xopts = {:data => xdata}
47
- xopts[:sheet] = options[:sheet] if options[:sheet]
48
- xopts[:doc] = options[:doc] if options[:doc]
49
-
50
- response = CloudXLS.xpipe(xopts)
51
- redirect_to response.url
52
- end
53
-
54
- ActionController::Renderers.add :xlsx do |scope, options|
55
- columns = options.fetch(:columns, nil)
56
-
57
- xdata = options[:data] || {}
58
- unless (xdata.has_key?(:text) ||
59
- xdata.has_key?(:url ) ||
60
- xdata.has_key?(:file) )
61
-
62
- xdata[:text] = CloudXLS::CSVWriter.text(scope, {:columns => columns})
63
- end
64
-
65
- xopts = {:data => xdata}
66
- xopts[:sheet] = options[:sheet] if options[:sheet]
67
- xopts[:doc] = options[:doc] || {}
68
- xopts[:doc][:format] = 'xlsx'
69
-
70
- response = CloudXLS.xpipe(xopts)
71
- redirect_to response.url
72
- end
73
-
74
- # For respond_to default
75
- class ActionController::Responder
76
- def to_csv
77
- controller.render({:csv => resources.last, :stream => false }.merge(options))
78
- end
79
-
80
- def to_xls
81
- if options[:stream] == true
82
- options[:data] ||= {}
83
- options[:data][:url] ||= controller.request.url.gsub(/xls\Z/, "csv")
84
- end
85
- controller.render({:xls => resources.last }.merge(options))
86
- end
87
-
88
- def to_xlsx
89
- if options[:stream] == true
90
- options[:data] ||= {}
91
- options[:data][:url] ||= controller.request.url.gsub(/xlsx\Z/, "csv")
92
- end
93
- controller.render({:xlsx => resources.last }.merge(options))
94
- end
95
- end
@@ -1,28 +0,0 @@
1
- # encoding: UTF-8
2
- # This file is auto-generated from the current state of the database. Instead
3
- # of editing this file, please use the migrations feature of Active Record to
4
- # incrementally modify your database, and then regenerate this schema definition.
5
- #
6
- # Note that this schema.rb definition is the authoritative source for your
7
- # database schema. If you need to create the application database on another
8
- # system, you should be using db:schema:load, not running all the migrations
9
- # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
- # you'll amass, the slower it'll run and the greater likelihood for issues).
11
- #
12
- # It's strongly recommended that you check this file into your version control system.
13
-
14
- ActiveRecord::Schema.define(version: 20120717192452) do
15
-
16
- create_table "posts", force: true do |t|
17
- t.string "title"
18
- t.integer "visits"
19
- t.float "conversion_rate"
20
- t.date "published_on"
21
- t.datetime "expired_at"
22
- t.boolean "published"
23
- t.datetime "unix_timestamp"
24
- t.datetime "created_at"
25
- t.datetime "updated_at"
26
- end
27
-
28
- end
Binary file