Skip to content

Commit e17f40b

Browse files
author
Mads Buus Westmark
committed
syntax cleanup
1 parent c31e1fb commit e17f40b

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Example of _my_partial.docx.erb
8181
# Add mime-type in /config/initializers/mime_types.rb:
8282
Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx
8383

84-
# Add docx reponder in your controler
84+
# Add docx responder in your controller
8585
def show
8686
respond_to do |format|
8787
format.docx do

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
require "bundler/gem_tasks"
22
require 'rspec/core/rake_task'
33
task :default => :spec
4-
RSpec::Core::RakeTask.new
4+
RSpec::Core::RakeTask.new

lib/htmltoword/action_controller.rb

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
require 'rack/test'
12
require 'action_controller'
3+
24
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
46
end
57

68
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'
1010

11+
# This is ugly and should be solved with regular file utils
1112
if options[:template] == action_name
12-
if filename =~ /^([^\/]+)\/(.+)$/
13+
if filename =~ %r{^([^\/]+)/(.+)$}
1314
options[:prefixes] ||= []
1415
options[:prefixes].unshift $1
1516
options[:template] = $2
@@ -19,11 +20,11 @@
1920
end
2021

2122
# disposition / filename
22-
disposition = options.delete(:disposition) || 'attachment'
23+
disposition = options.delete(:disposition) || 'attachment'
2324
if file_name = options.delete(:filename)
24-
file_name += ".docx" unless file_name =~ /\.docx$/
25+
file_name += '.docx' unless file_name =~ /\.docx$/
2526
else
26-
file_name = "#{filename.gsub(/^.*\//,'')}.docx"
27+
file_name = "#{filename.gsub(/^.*\//, '')}.docx"
2728
end
2829

2930
# other properties
@@ -33,20 +34,19 @@
3334
content = options.delete(:content) || render_to_string(options)
3435

3536
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
3738
end
3839

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
5050
end
5151
end
5252
end

spec/spec_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def compare_resulting_wordml_with_expected(html, resulting_wordml)
66
source = Nokogiri::HTML(html.gsub(/>\s+</, "><"))
7-
xslt = Nokogiri::XSLT( File.read(Htmltoword::Document.default_xslt_template))
7+
xslt = Nokogiri::XSLT(File.read(Htmltoword::Document.default_xslt_template))
88
result = xslt.transform(source)
99
if compare_content_of_body?(resulting_wordml)
1010
result.at("//w:sectPr").remove

0 commit comments

Comments
 (0)