cloudxls-rails 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cloudxls-rails/action_controller.rb +36 -7
- data/lib/cloudxls-rails/csv_writer.rb +6 -1
- data/lib/cloudxls-rails/version.rb +1 -1
- data/spec/integration_spec.rb +38 -3
- data/spec/test_app/app/controllers/posts_controller.rb +14 -2
- data/spec/test_app/app/views/posts/successful_redirect.html.erb +1 -0
- data/spec/test_app/config/routes.rb +5 -1
- metadata +3 -1
@@ -13,7 +13,7 @@ ActionController::Renderers.add :csv do |scope, options|
|
|
13
13
|
filename = options.fetch(:filename, 'data.csv')
|
14
14
|
columns = options[:columns]
|
15
15
|
|
16
|
-
if options.fetch(:stream, false)
|
16
|
+
if options.fetch(:stream, false)
|
17
17
|
# streaming response
|
18
18
|
headers["Content-Type"] = "text/csv"
|
19
19
|
headers["Content-disposition"] = "attachment; filename=\"#{filename}\""
|
@@ -35,18 +35,39 @@ end
|
|
35
35
|
ActionController::Renderers.add :xls do |scope, options|
|
36
36
|
columns = options.fetch(:columns, nil)
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
xdata = options[:data] || {}
|
39
|
+
unless (xdata.has_key?(:text) ||
|
40
|
+
xdata.has_key?(:url ) ||
|
41
|
+
xdata.has_key?(:file) )
|
40
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)
|
41
51
|
redirect_to response.url
|
42
52
|
end
|
43
53
|
|
44
54
|
ActionController::Renderers.add :xlsx do |scope, options|
|
45
55
|
columns = options.fetch(:columns, nil)
|
46
56
|
|
47
|
-
|
48
|
-
|
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'
|
49
69
|
|
70
|
+
response = CloudXLS.xpipe(xopts)
|
50
71
|
redirect_to response.url
|
51
72
|
end
|
52
73
|
|
@@ -57,10 +78,18 @@ class ActionController::Responder
|
|
57
78
|
end
|
58
79
|
|
59
80
|
def to_xls
|
60
|
-
|
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))
|
61
86
|
end
|
62
87
|
|
63
88
|
def to_xlsx
|
64
|
-
|
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))
|
65
94
|
end
|
66
95
|
end
|
@@ -50,7 +50,12 @@ module CloudXLS
|
|
50
50
|
|
51
51
|
Enumerator.new do |row|
|
52
52
|
if options[:skip_headers] != true
|
53
|
-
|
53
|
+
if scope.respond_to?(:column_names)
|
54
|
+
columns ||= scope.column_names
|
55
|
+
end
|
56
|
+
if columns
|
57
|
+
row << csv_titles(columns, :titleize).to_csv
|
58
|
+
end
|
54
59
|
end
|
55
60
|
|
56
61
|
enum = scope_enumerator(scope)
|
data/spec/integration_spec.rb
CHANGED
@@ -27,8 +27,43 @@ describe 'Request', :type => :request do
|
|
27
27
|
].join("\n")
|
28
28
|
end
|
29
29
|
|
30
|
-
it "/posts.
|
31
|
-
|
32
|
-
|
30
|
+
it "/posts/stream.csv streams csv" do
|
31
|
+
visit '/posts/stream.csv'
|
32
|
+
page.should have_content [
|
33
|
+
"Title,Visits,Conversion Rate,Published On,Published,Expired At",
|
34
|
+
"hello world,12032,0.24,2013-12-24,false,2013-12-25T12:30:30.000+0000"
|
35
|
+
].join("\n")
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
describe "posts/stream" do
|
40
|
+
it "/posts/stream.csv should xpipe with csv url" do
|
41
|
+
visit '/posts/stream.csv'
|
42
|
+
page.should have_content [
|
43
|
+
"Title,Visits,Conversion Rate,Published On,Published,Expired At",
|
44
|
+
"hello world,12032,0.24,2013-12-24,false,2013-12-25T12:30:30.000+0000"
|
45
|
+
].join("\n")
|
46
|
+
end
|
47
|
+
|
48
|
+
it "/posts/stream.xlsx has a working csv export" do
|
49
|
+
CloudXLS.should_receive(:xpipe) { |options|
|
50
|
+
!options[:data][:url].ends_with?("posts/stream.csv") &&
|
51
|
+
options[:doc][:format] == "xlsx" &&
|
52
|
+
options.keys.length == 2
|
53
|
+
}.and_return(OpenStruct.new(:url => "/successful_redirect"))
|
54
|
+
|
55
|
+
visit '/posts/stream.xlsx'
|
56
|
+
page.should have_content("OK")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "/posts/stream.xls has a working csv export" do
|
60
|
+
CloudXLS.should_receive(:xpipe) { |options|
|
61
|
+
!options[:data][:url].end_with?("posts/stream.csv") &&
|
62
|
+
options.keys.length == 1
|
63
|
+
}.and_return(OpenStruct.new(:url => "/successful_redirect"))
|
64
|
+
|
65
|
+
visit '/posts/stream.xls'
|
66
|
+
page.should have_content("OK")
|
67
|
+
end
|
33
68
|
end
|
34
69
|
end
|
@@ -2,11 +2,23 @@ class PostsController < ApplicationController
|
|
2
2
|
respond_to :csv, :xls, :xlsx, :html
|
3
3
|
|
4
4
|
def index
|
5
|
-
respond_with(Post.all, :columns =>
|
5
|
+
respond_with(Post.all, :columns => export_attributes)
|
6
|
+
end
|
7
|
+
|
8
|
+
def stream
|
9
|
+
respond_with(Post.all, :stream => true, :columns => export_attributes)
|
6
10
|
end
|
7
11
|
|
8
12
|
# Used for stub/mocking a redirect request
|
9
13
|
def successful_redirect
|
10
|
-
|
14
|
+
# make rails 3.1 happy with a template
|
15
|
+
# /views/posts/successful_redirect.html.erb
|
16
|
+
render :text => "OK"
|
11
17
|
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
def export_attributes
|
21
|
+
%w[title visits conversion_rate published_on published expired_at]
|
22
|
+
end
|
23
|
+
|
12
24
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
OK
|
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.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- spec/test_app/app/models/post.rb
|
144
144
|
- spec/test_app/app/views/layouts/application.html.erb
|
145
145
|
- spec/test_app/app/views/posts/index.html.erb
|
146
|
+
- spec/test_app/app/views/posts/successful_redirect.html.erb
|
146
147
|
- spec/test_app/config.ru
|
147
148
|
- spec/test_app/config/application.rb
|
148
149
|
- spec/test_app/config/boot.rb
|
@@ -215,6 +216,7 @@ test_files:
|
|
215
216
|
- spec/test_app/app/models/post.rb
|
216
217
|
- spec/test_app/app/views/layouts/application.html.erb
|
217
218
|
- spec/test_app/app/views/posts/index.html.erb
|
219
|
+
- spec/test_app/app/views/posts/successful_redirect.html.erb
|
218
220
|
- spec/test_app/config.ru
|
219
221
|
- spec/test_app/config/application.rb
|
220
222
|
- spec/test_app/config/boot.rb
|