axlsx_rails 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -11,5 +11,6 @@ guard 'rspec', :version => 2 do
11
11
  watch(%r{spec/dummy/app/controllers/.+\.rb$}) { ["spec/axlsx_renderer_spec.rb", "spec/axlsx_builder_spec.rb", "axlsx_request_spec.rb"] }
12
12
  watch('lib/axlsx_rails/action_controller.rb') { ["spec/axlsx_renderer_spec.rb", "spec/axlsx_request_spec.rb"] }
13
13
  watch('lib/axlsx_rails/template_handler.rb') { "spec/axlsx_builder_spec.rb" }
14
+ watch(%r{spec/dummy/app/mailers/.+\.rb$}) { "spec/axlsx_mailer_spec.rb" }
14
15
  watch(%r{spec/dummy/app/views/.+\.erb}) { "spec/axlsx_request_spec.rb" }
15
16
  end
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  Axlsx-Rails — Axlsx templates for Rails views
2
2
  ===================================================
3
3
 
4
+ [![Gem
5
+ Version](https://badge.fury.io/rb/axlsx_rails.png)](http://badge.fury.io/rb/axlsx_rails)
4
6
  [![Build Status](https://secure.travis-ci.org/straydogstudio/axlsx_rails.png?branch=master)](http://travis-ci.org/straydogstudio/axlsx_rails)
5
7
  [![Dependency Status](https://gemnasium.com/straydogstudio/axlsx_rails.png?branch=master)](https://gemnasium.com/straydogstudio/axlsx_rails)
8
+ [![Coverage
9
+ Status](https://coveralls.io/repos/straydogstudio/axlsx_rails/badge.png)](https://coveralls.io/r/straydogstudio/axlsx_rails)
6
10
 
7
11
  ##Installation
8
12
 
@@ -14,11 +18,11 @@ gem 'axlsx_rails'
14
18
 
15
19
  ##Requirements
16
20
 
17
- * Rails 3.1, but it has only been tested on 3.2.6+
21
+ * Rails 3.1, tested on 3.1, 3.2, and 4.0
18
22
 
19
23
  ##Usage
20
24
 
21
- Axlsx-Rails provides a renderer and a template handler. It adds the :xlsx format and parses .xlsx.axslx templates.
25
+ Axlsx-Rails provides a renderer and a template handler. It adds the :xlsx format and parses .xlsx.axslx templates. This lets you take all the [Axlsx](https://github.com/randym/axlsx) code out of your controller or model and place it inside the template, where view code belongs! **See [this blog post](http://axlsx.blog.randym.net/2012/08/excel-on-rails-like-pro-with-axlsxrails.html) for a more complete walkthrough.**
22
26
 
23
27
  ###Controller
24
28
 
@@ -43,6 +47,7 @@ format.xlsx {
43
47
  response.headers['Content-Disposition'] = 'attachment; filename="my_new_filename.xlsx"'
44
48
  }
45
49
  ```
50
+
46
51
  Or:
47
52
 
48
53
  ```ruby
@@ -56,7 +61,9 @@ format.xlsx {
56
61
 
57
62
  ###Template
58
63
 
59
- Use the .xlsx.axlsx extension (sorry if your lysdexic!) In the template, use xlsx_package variable, which is set with Axlsx::Package.new:
64
+ This is where you place all your [Axlsx](https://github.com/randym/axlsx) specific markup. Add worksheets, fill content, merge cells, add styles. See the [Axlsx examples](https://github.com/randym/axlsx/tree/master/examples/example.rb) page to see what you can do.
65
+
66
+ Use the .xlsx.axlsx extension ([watch out for typos!](#troubleshooting)) In the template, use xlsx_package variable, which is set with Axlsx::Package.new:
60
67
 
61
68
  ```ruby
62
69
  wb = xlsx_package.workbook
@@ -103,6 +110,32 @@ wb.add_worksheet(name: "Cover Sheet") do |sheet|
103
110
  end
104
111
  ```
105
112
 
113
+ ####Mailers
114
+
115
+ To use an xlsx template to render a mail attachment, use the following syntax:
116
+
117
+ ```ruby
118
+ class UserMailer < ActionMailer::Base
119
+ def export(users)
120
+ xlsx = render handlers: [:axlsx], template: "users/export", locals: {users: users}
121
+ attachments["Users.xlsx"] = {mime_type: Mime::XLSX, content: xlsx}
122
+ ...
123
+ end
124
+ end
125
+ ```
126
+
127
+ If the template (`users/export`) can refer to only one file (the xlsx.axlsx template), you do not need to specify `handlers`.
128
+
129
+ ##Troubleshooting
130
+ ### Mispellings
131
+ It is easy to get the spelling wrong in the extension name, the format.xlsx statement, or in a render call. If you get the following error in particular:
132
+
133
+ ```ruby
134
+ uninitialized constant Mime::XSLX
135
+ ```
136
+
137
+ it means you have used `format.xslx` instead of `format.xlsx`, or something similar.
138
+
106
139
  ##Dependencies
107
140
 
108
141
  - [Axlsx](https://github.com/randym/axlsx)
@@ -111,22 +144,54 @@ end
111
144
 
112
145
  * [Noel Peden](https://github.com/straydogstudio)
113
146
 
147
+ ##Contributors
148
+
149
+ * [randym](https://github.com/randym)
150
+ * [sugi](https://github.com/sugi)
151
+ * [envek](https://github.com/envek)
152
+ * [engwan](https://github.com/engwan)
153
+ * [maxd](https://github.com/maxd)
154
+
114
155
  ##Change log
115
156
 
116
- - **December 6, 2012**: 0.1.3 release
117
- - Fix for absolute template paths
157
+ **October 11, 2013**
158
+
159
+ - Handle (and test) respond_to override
160
+
161
+ **October 4, 2013**
162
+
163
+ - Added coveralls
164
+ - Raised testing to axlsx 2.0.1, roo 1.12.2, and rubyzip 1.0.0
165
+
166
+ **July 25, 2013**
167
+
168
+ - Documentation improved
169
+ - Testing for generating partial in mailer
170
+
171
+ **January 18, 2013**: 0.1.4 release
172
+
173
+ - Now supports Rails 4 (thanks [Envek](https://github.com/Envek))
174
+ - If you call render :xlsx on a request without :xlsx format, it should force the :xlsx format. Works on Rails 3.2+.
175
+
176
+ **December 6, 2012**: 0.1.3 release
177
+
178
+ - Fix for absolute template paths
179
+
180
+ **July 25, 2012**: 0.1.2 release
181
+
182
+ - Partials tested
183
+
184
+ **July 19, 2012**: 0.1.1 release
185
+
186
+ - Travis-ci added (thanks [randym](https://github.com/randym))
187
+ - render statements and filename tests fixes (thanks [engwan](https://github.com/engwan))
118
188
 
119
- - **July 25, 2012**: 0.1.2 release
120
- - Partials tested
189
+ **July 17, 2012**: 0.1.0 release
121
190
 
122
- - **July 19, 2012**: 0.1.1 release
123
- - Travis-ci added (thanks [randym](https://github.com/randym))
124
- - render statements and filename tests fixes (thanks [engwan](https://github.com/engwan))
191
+ - Tests completed
192
+ - Acts_as_xlsx tested, example in docs
125
193
 
126
- - **July 17, 2012**: 0.1.0 release
127
- - Tests completed
128
- - Acts_as_xlsx tested, example in docs
194
+ **July 12, 2012**: 0.0.1 release
129
195
 
130
- - **July 12, 2012**: 0.0.1 release
131
- - Initial posting.
132
- - It works, but there are no tests! Bad programmer!
196
+ - Initial posting.
197
+ - It works, but there are no tests! Bad programmer!
@@ -24,6 +24,10 @@ end
24
24
  # For respond_to default
25
25
  class ActionController::Responder
26
26
  def to_xlsx
27
- controller.render :xlsx => controller.action_name
27
+ if @default_response
28
+ @default_response.call(options)
29
+ else
30
+ controller.render({:xlsx => controller.action_name}.merge(options))
31
+ end
28
32
  end
29
33
  end
@@ -1,3 +1,3 @@
1
1
  module AxlsxRails
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -24,11 +24,11 @@ describe 'Axlsx template handler' do
24
24
  xlsx_package, wb = nil
25
25
  eval( AB.call template )
26
26
  xlsx_package.serialize('/tmp/axlsx_temp.xlsx')
27
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
27
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
28
28
  wb.cell(2,3).should == 'c'
29
29
  end
30
30
 
31
31
  #TODO:
32
32
  # Test if author field is set - does roo parse that?
33
33
  end
34
- end
34
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Mailer", type: :request do
4
+ before :each do
5
+ @user = User.create name: 'Elmer', last_name: 'Fudd', address: '1234 Somewhere, Over NY 11111', email: 'elmer@fudd.com'
6
+ end
7
+
8
+ it "attaches an xlsx file" do
9
+ visit "/users/#{@user.id}/send_instructions"
10
+ last_email = ActionMailer::Base.deliveries.last
11
+ last_email.to.should == [@user.email]
12
+ last_email.attachments.first.should be
13
+ last_email.attachments.first.content_type.should == Mime::XLSX.to_s + "; charset=UTF-8"
14
+ end
15
+ end
@@ -1,4 +1,4 @@
1
- require'spec_helper'
1
+ require 'spec_helper'
2
2
  describe 'Axlsx request', :type => :request do
3
3
 
4
4
  it "has a working dummy app" do
@@ -11,7 +11,7 @@ describe 'Axlsx request', :type => :request do
11
11
  page.response_headers['Content-Type'].should == Mime::XLSX.to_s + "; charset=utf-8"
12
12
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
13
13
  wb = nil
14
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
14
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
15
15
  wb.cell(2,1).should == 'Untie!'
16
16
  end
17
17
 
@@ -23,7 +23,19 @@ describe 'Axlsx request', :type => :request do
23
23
 
24
24
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
25
25
  wb = nil
26
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
26
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
27
+ wb.cell(2,1).should == 'Untie!'
28
+ end
29
+
30
+ it "downloads an excel file from respond_to while specifying filename in direct format" do
31
+ visit '/useheader.xlsx?set_direct=true'
32
+
33
+ page.response_headers['Content-Type'].should == Mime::XLSX.to_s + "; charset=utf-8"
34
+ page.response_headers['Content-Disposition'].should include("filename=\"filename_test.xlsx\"")
35
+
36
+ File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
37
+ wb = nil
38
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
27
39
  wb.cell(2,1).should == 'Untie!'
28
40
  end
29
41
 
@@ -35,7 +47,7 @@ describe 'Axlsx request', :type => :request do
35
47
 
36
48
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
37
49
  wb = nil
38
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
50
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
39
51
  wb.cell(2,1).should == 'Untie!'
40
52
  end
41
53
 
@@ -47,7 +59,7 @@ describe 'Axlsx request', :type => :request do
47
59
  page.response_headers['Content-Type'].should == Mime::XLSX.to_s + "; charset=utf-8"
48
60
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
49
61
  wb = nil
50
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
62
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
51
63
  wb.cell(3,2).should == 'Bugs'
52
64
  end
53
65
 
@@ -56,7 +68,7 @@ describe 'Axlsx request', :type => :request do
56
68
  page.response_headers['Content-Type'].should == Mime::XLSX.to_s + "; charset=utf-8"
57
69
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
58
70
  wb = nil
59
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
71
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
60
72
  wb.cell(1,1,wb.sheets[0]).should == 'Cover'
61
73
  wb.cell(2,1,wb.sheets[1]).should == "Untie!"
62
74
  end
@@ -70,7 +82,7 @@ describe 'Axlsx request', :type => :request do
70
82
  page.response_headers['Content-Type'].should == Mime::XLSX.to_s + "; charset=utf-8"
71
83
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
72
84
  wb = nil
73
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
85
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
74
86
  wb.cell(1,1).should == 'Bugs'
75
87
  wb.cell(2,1).should == 'Carrots'
76
88
  wb.cell(3,1).should == 'Celery'
@@ -87,10 +99,22 @@ describe 'Axlsx request', :type => :request do
87
99
  page.response_headers['Content-Type'].should == Mime::XLSX.to_s
88
100
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
89
101
  wb = nil
90
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
102
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
91
103
  wb.cell(2,2).should == 'Bugs'
92
104
  end
93
105
 
106
+ it "uses respond_with" do
107
+ User.destroy_all
108
+ @user = User.create name: 'Responder', last_name: 'Bunny', address: '1234 Right Turn, Albuquerque NM 22222', email: 'bugs@bunny.com'
109
+ expect {
110
+ visit "/users/#{@user.id}.xlsx"
111
+ }.to_not raise_error
112
+ File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
113
+ wb = nil
114
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
115
+ wb.cell(2,1).should == 'Untie!'
116
+ end
117
+
94
118
  unless Rails.version < '3.2'
95
119
  it "handles missing format with render :xlsx" do
96
120
  visit '/another'
@@ -100,7 +124,7 @@ describe 'Axlsx request', :type => :request do
100
124
 
101
125
  File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(page.source) }
102
126
  wb = nil
103
- expect{ wb = Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
127
+ expect{ wb = Roo::Excelx.new('/tmp/axlsx_temp.xlsx') }.to_not raise_error
104
128
  wb.cell(2,1).should == 'Untie!'
105
129
  end
106
130
  end
data/spec/ci.sh CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env sh
2
- cd spec/dummy && bundle install --without debug && bundle exec rake db:create && bundle exec rake db:migrate && cd ../../ && bundle exec rake spec
2
+ cd spec/dummy && bundle install --without debug && bundle exec rake db:create && bundle exec rake db:migrate && cd ../../ && bundle exec rspec spec
@@ -28,8 +28,11 @@ class HomeController < ApplicationController
28
28
  def useheader
29
29
  respond_to do |format|
30
30
  format.xlsx {
31
- render xlsx: "useheader", disposition: "attachment", filename: "filename_test.xlsx"
32
- # response.headers['Content-Disposition'] = "attachment; filename=\"filename_test.xlsx\""
31
+ if params[:set_direct]
32
+ response.headers['Content-Disposition'] = "attachment; filename=\"filename_test.xlsx\""
33
+ else
34
+ render xlsx: "useheader", disposition: "attachment", filename: "filename_test.xlsx"
35
+ end
33
36
  }
34
37
  end
35
38
  end
@@ -1,4 +1,6 @@
1
1
  class UsersController < ApplicationController
2
+ respond_to :xlsx, :html
3
+
2
4
  # GET /users
3
5
  # GET /users.json
4
6
  def index
@@ -9,4 +11,17 @@ class UsersController < ApplicationController
9
11
  format.xlsx
10
12
  end
11
13
  end
14
+
15
+ def show
16
+ @user = User.find(params[:id])
17
+ respond_with(@user) do |format|
18
+ format.xlsx { render "respond_with.xlsx.axlsx" }
19
+ end
20
+ end
21
+
22
+ def send_instructions
23
+ @user = User.find(params[:user_id])
24
+ @user.send_instructions
25
+ render text: "Email sent"
26
+ end
12
27
  end
@@ -0,0 +1,26 @@
1
+ class Notifier < ActionMailer::Base
2
+ default :from => 'noreply@company.com'
3
+ # default :from => "Didier Leser - CESAM Nature <didier.leser@cesam-nature.com>"
4
+
5
+ def instructions(user)
6
+ @user = user
7
+
8
+ # normal syntax
9
+ xlsx = render handlers: [:axlsx], template: 'users/mailers/instructions', layout: false
10
+
11
+ # using render_to_string. sometimes not available, but why??
12
+ # xlsx = render_to_string handlers: [:axlsx], template: 'users/mailers/instructions', layout: false
13
+
14
+ # creating own view
15
+ # av = ActionView::Base.new()
16
+ # av.view_paths = ActionController::Base.view_paths
17
+ # av.extend ApplicationHelper
18
+ # av.assign user: @user
19
+ # xlsx = av.render handlers: [:axlsx], template: "users/mailers/instructions"
20
+
21
+ attachments["user_#{user.id}.pdf"] = {mime_type: Mime::XLSX, content: xlsx}
22
+ mail :to => user.email, :subject => 'Instructions'
23
+ end
24
+
25
+
26
+ end
@@ -4,4 +4,8 @@ class User < ActiveRecord::Base
4
4
  attr_accessible :address, :email, :last_name, :name
5
5
 
6
6
  has_many :likes
7
+
8
+ def send_instructions
9
+ Notifier.instructions(self).deliver
10
+ end
7
11
  end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
5
+ </head>
6
+ <body>
7
+ <h1>Instructions</h1>
8
+ <p>
9
+ You have successfully signed up to example.com,
10
+ your username is: <%= @user.email %>.<br/>
11
+ </p>
12
+ <p>Thanks for joining and have a great day!</p>
13
+ </body>
14
+ </html>
@@ -0,0 +1,5 @@
1
+ wb = xlsx_package.workbook
2
+ wb.add_worksheet(name: "Instructions") do |sheet|
3
+ sheet.add_row [@user.id, @user.name, @user.email]
4
+ sheet.add_row ['', 'Instructions', '']
5
+ end
@@ -0,0 +1,8 @@
1
+ wb = xlsx_package.workbook
2
+ style_shout = wb.styles.add_style sz: 16, b: true, alignment: { horizontal: :center }
3
+ wb.add_worksheet(name: "Foobar") do |sheet|
4
+ sheet.add_row ['Bad', 'spellers', 'of', 'the', 'world', '...']
5
+ sheet.add_row ['Untie!']
6
+ sheet.merge_cells("A2:E2")
7
+ sheet["A2"].style = style_shout
8
+ end
@@ -34,4 +34,6 @@ Dummy::Application.configure do
34
34
 
35
35
  # Expands the lines which load the assets
36
36
  config.assets.debug = true
37
+
38
+ config.eager_load = false
37
39
  end
@@ -64,4 +64,6 @@ Dummy::Application.configure do
64
64
  # Log the query plan for queries taking more than this (works
65
65
  # with SQLite, MySQL, and PostgreSQL)
66
66
  # config.active_record.auto_explain_threshold_in_seconds = 0.5
67
+
68
+ config.eager_load = true
67
69
  end
@@ -1,5 +1,6 @@
1
1
  Dummy::Application.configure do
2
2
  # Settings specified here will take precedence over those in config/application.rb
3
+ config.eager_load = false if Rails.version >= '4.0'
3
4
 
4
5
  # The test environment is used exclusively to run your application's
5
6
  # test suite. You never need to work with it otherwise. Remember that
@@ -12,7 +13,7 @@ Dummy::Application.configure do
12
13
  config.static_cache_control = "public, max-age=3600"
13
14
 
14
15
  # Log error messages when you accidentally call methods on nil
15
- config.whiny_nils = true
16
+ # config.whiny_nils = true
16
17
 
17
18
  # Show full error reports and disable caching
18
19
  config.consider_all_requests_local = true
@@ -34,4 +35,6 @@ Dummy::Application.configure do
34
35
 
35
36
  # Print deprecation notices to the stderr
36
37
  config.active_support.deprecation = :stderr
38
+
39
+ config.eager_load = false
37
40
  end