|
| 1 | +require 'rack/test' |
1 | 2 | require 'action_controller'
|
| 3 | + |
2 | 4 | unless defined? Mime::DOCX
|
3 |
| - Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx |
| 5 | + Mime::Type.register 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', :docx |
4 | 6 | end
|
5 | 7 |
|
6 | 8 | ActionController::Renderers.add :docx do |filename, options|
|
7 |
| - unless formats.include?(:docx) || Rails.version < '3.2' |
8 |
| - formats[0] = :docx |
9 |
| - end |
| 9 | + formats[0] = :docx unless formats.include?(:docx) || Rails.version < '3.2' |
10 | 10 |
|
| 11 | + # This is ugly and should be solved with regular file utils |
11 | 12 | if options[:template] == action_name
|
12 |
| - if filename =~ /^([^\/]+)\/(.+)$/ |
| 13 | + if filename =~ %r{^([^\/]+)/(.+)$} |
13 | 14 | options[:prefixes] ||= []
|
14 | 15 | options[:prefixes].unshift $1
|
15 | 16 | options[:template] = $2
|
|
19 | 20 | end
|
20 | 21 |
|
21 | 22 | # disposition / filename
|
22 |
| - disposition = options.delete(:disposition) || 'attachment' |
| 23 | + disposition = options.delete(:disposition) || 'attachment' |
23 | 24 | if file_name = options.delete(:filename)
|
24 |
| - file_name += ".docx" unless file_name =~ /\.docx$/ |
| 25 | + file_name += '.docx' unless file_name =~ /\.docx$/ |
25 | 26 | else
|
26 |
| - file_name = "#{filename.gsub(/^.*\//,'')}.docx" |
| 27 | + file_name = "#{filename.gsub(/^.*\//, '')}.docx" |
27 | 28 | end
|
28 | 29 |
|
29 | 30 | # other properties
|
|
33 | 34 | content = options.delete(:content) || render_to_string(options)
|
34 | 35 |
|
35 | 36 | doc = Htmltoword::Document.create content, file_name, word_template
|
36 |
| - send_data File.read(doc.path), :filename => file_name, :type => Mime::DOCX, :disposition => disposition |
| 37 | + send_data File.read(doc.path), filename: file_name, type: Mime::DOCX, disposition: disposition |
37 | 38 | end
|
38 | 39 |
|
39 |
| -# For respond_with default |
40 |
| -begin |
41 |
| - ActionController::Responder |
42 |
| -rescue LoadError |
43 |
| -else |
44 |
| - class ActionController::Responder |
45 |
| - def to_docx |
46 |
| - if @default_response |
47 |
| - @default_response.call(options) |
48 |
| - else |
49 |
| - controller.render({:docx => controller.action_name}.merge(options)) |
| 40 | +if defined? ActionController::Responder |
| 41 | + module ActionController |
| 42 | + # For respond_with default |
| 43 | + class Responder |
| 44 | + def to_docx |
| 45 | + if @default_response |
| 46 | + @default_response.call(options) |
| 47 | + else |
| 48 | + controller.render({ docx: controller.action_name }.merge(options)) |
| 49 | + end |
50 | 50 | end
|
51 | 51 | end
|
52 | 52 | end
|
|
0 commit comments